Skip to content

Commit

Permalink
feat: add custom api, completely customize the statements that need…
Browse files Browse the repository at this point in the history
… to be removed, which will overwrite `includes`
  • Loading branch information
xiaoxian521 committed Dec 4, 2023
1 parent a37f5a5 commit a39e526
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ English | [简体中文](./README.zh_CN.md)

#### 🦾 **Configurable**:

| configuration | must | meaning | type | example |
| --------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------- | -------------------------------------------------------------------- |
| `external` | No | supported to pass external to represent certain files not to delete the specified `console` type of operation | `Array<string>` | `removeConsole({external: ["src/assets/iconfont/iconfont.js",...]})` |
| configuration | must | meaning | type | example |
| :-------------- | :--- | :----------------------------------------------------------- | :-------------- | :----------------------------------------------------------- |
| `external` | No | supported to pass external to represent certain files not to delete the specified `console` type of operation | `Array<string>` | `removeConsole({external: ["src/assets/iconfont/iconfont.js",...]})` |
| `includes` | No | supports passing `includes` to specify the type of `console` that needs to be removed,If `includes` is not passed, `console.log` will be removed by default | `Array<string>` | `removeConsole({ includes: ["log", "warn", "error", "info", ...] })` |
| `externalValue` | No | some `console` statements are reserved, if there is an incoming `externalValue` value in the `value` of the `console`, the `console ` will be preserved | `Array<string>` | `removeConsole({ externalValue: ["这个不删", "noRemove", ...] })` |
| `externalValue` | No | some `console` statements are reserved, if there is an incoming `externalValue` value in the `value` of the `console`, the `console ` will be preserved | `Array<string>` | `removeConsole({ externalValue: ["这个不删", "noRemove", ...] })` |
| custom | No | Completely customize the statements that need to be removed, which will overwrite `includes` | `Array<string>` | `removeConsole({ custom: ["console.log()", "console.warn()", "debugger", ...] })` |

## 📦 install

Expand Down
11 changes: 6 additions & 5 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

#### 🦾 **可配置**:

| **配置项名称** | **是否必须** | **含义** | **类型** | **举例** |
| --------------- | ------------ | ------------------------------------------------------------------------------------------------------ | --------------- | ----------------------------------------------------------------------- |
| `external` | | 代表某些文件不进行删除指定 `console` 类型的操作 | `Array<string>` | `removeConsole({ external: ["src/assets/iconfont/iconfont.js", ...] })` |
| `includes` | | 指定需要删除的 `console` 类型,不传 `includes` 默认删除 `console.log` | `Array<string>` | `removeConsole({ includes: ["log", "warn", "error", "info", ...] })` |
| `externalValue` | | 保留某些 `console` 语句,若 `console` `value` 中有传入的 `externalValue` 值,则此 `console` 会被保留 | `Array<string>` | `removeConsole({ externalValue: ["这个不删", "noRemove", ...] })` |
| **配置项名称** | **必传** | **含义** | **类型** | **举例** |
| :-------------- | :------- | :----------------------------------------------------------- | :-------------- | :----------------------------------------------------------- |
| `external` | | 代表某些文件不进行删除指定 `console` 类型的操作 | `Array<string>` | `removeConsole({ external: ["src/assets/iconfont/iconfont.js", ...] })` |
| `includes` | | 指定需要删除的 `console` 类型,不传 `includes` 默认删除 `console.log` | `Array<string>` | `removeConsole({ includes: ["log", "warn", "error", "info", ...] })` |
| `externalValue` | | 保留某些 `console` 语句,若 `console` `value` 中有传入的 `externalValue` 值,则此 `console` 会被保留 | `Array<string>` | `removeConsole({ externalValue: ["这个不删", "noRemove", ...] })` |
| `custom` | | 完全自定义需要删除的语句,会覆盖`includes` | `Array<string>` | `removeConsole({ custom: ["console.log()", "console.warn()", "debugger", ...] })` |

## 📦 安装

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { transforms, getAbsolutePath } from "./utils";
export default function removeConsole(
options: Partial<Options> = {}
): PluginOption {
const { external, includes, externalValue } = options || {};
const { external, includes, externalValue, custom } = options || {};
return {
name: "vite:remove-console",
apply: "build",
Expand All @@ -30,7 +30,7 @@ export default function removeConsole(
};
} else {
return {
code: transforms(_source, includes, externalValue),
code: transforms(_source, includes, externalValue, custom),
map: null
};
}
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export interface Options {
includes?: string[] | undefined;
/** Don't remove the types of console these modules */
external?: string[] | undefined;
/** Do not remove the log that contains this value */
/** Don't remove the log that contains this value */
externalValue?: string[] | undefined;
/** Completely customize the statements that need to be removed, which will overwrite `includes` */
custom?: string[] | undefined;
}
13 changes: 8 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import $T from "./transform";
export function transforms(
source: string,
includes: string[] | undefined,
externalValue: string[] | undefined
externalValue: string[] | undefined,
custom: string[] | undefined
) {
let consoles: string[] = [];
if (includes) {
includes.map(type => {
if (Array.isArray(custom) && custom?.length > 0) {
consoles = custom;
} else if (includes) {
includes?.map(type => {
consoles.push(`console.${type}()`);
});
} else {
Expand All @@ -29,8 +32,8 @@ export function transforms(
return findSource
.each((r: any) => {
let eValueString = r.value.arguments
.map((e: { value: string }) => e.value)
.join();
?.map((e: { value: string }) => e.value)
?.join();

const pattern = new RegExp(`(${externalValue.join("|")})`, "g");
if (!pattern.test(eValueString)) return r.remove();
Expand Down

0 comments on commit a39e526

Please sign in to comment.