From a39e5264d0c3460083de4b3ed99cb5db861b2466 Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Mon, 4 Dec 2023 13:27:31 +0800 Subject: [PATCH] feat: add `custom` api, completely customize the statements that need to be removed, which will overwrite `includes` --- README.md | 9 +++++---- README.zh_CN.md | 11 ++++++----- src/index.ts | 4 ++-- src/types.ts | 4 +++- src/utils.ts | 13 ++++++++----- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6c587f1..c56ffd7 100644 --- a/README.md +++ b/README.md @@ -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` | `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` | `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` | `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` | `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` | `removeConsole({ externalValue: ["这个不删", "noRemove", ...] })` | +| custom | No | Completely customize the statements that need to be removed, which will overwrite `includes` | `Array` | `removeConsole({ custom: ["console.log()", "console.warn()", "debugger", ...] })` | ## 📦 install diff --git a/README.zh_CN.md b/README.zh_CN.md index b4ae244..0867b10 100644 --- a/README.zh_CN.md +++ b/README.zh_CN.md @@ -16,11 +16,12 @@ #### 🦾 **可配置**: -| **配置项名称** | **是否必须** | **含义** | **类型** | **举例** | -| --------------- | ------------ | ------------------------------------------------------------------------------------------------------ | --------------- | ----------------------------------------------------------------------- | -| `external` | 否 | 代表某些文件不进行删除指定 `console` 类型的操作 | `Array` | `removeConsole({ external: ["src/assets/iconfont/iconfont.js", ...] })` | -| `includes` | 否 | 指定需要删除的 `console` 类型,不传 `includes` 默认删除 `console.log` | `Array` | `removeConsole({ includes: ["log", "warn", "error", "info", ...] })` | -| `externalValue` | 否 | 保留某些 `console` 语句,若 `console`的 `value` 中有传入的 `externalValue` 值,则此 `console` 会被保留 | `Array` | `removeConsole({ externalValue: ["这个不删", "noRemove", ...] })` | +| **配置项名称** | **必传** | **含义** | **类型** | **举例** | +| :-------------- | :------- | :----------------------------------------------------------- | :-------------- | :----------------------------------------------------------- | +| `external` | 否 | 代表某些文件不进行删除指定 `console` 类型的操作 | `Array` | `removeConsole({ external: ["src/assets/iconfont/iconfont.js", ...] })` | +| `includes` | 否 | 指定需要删除的 `console` 类型,不传 `includes` 默认删除 `console.log` | `Array` | `removeConsole({ includes: ["log", "warn", "error", "info", ...] })` | +| `externalValue` | 否 | 保留某些 `console` 语句,若 `console`的 `value` 中有传入的 `externalValue` 值,则此 `console` 会被保留 | `Array` | `removeConsole({ externalValue: ["这个不删", "noRemove", ...] })` | +| `custom` | 否 | 完全自定义需要删除的语句,会覆盖`includes` | `Array` | `removeConsole({ custom: ["console.log()", "console.warn()", "debugger", ...] })` | ## 📦 安装 diff --git a/src/index.ts b/src/index.ts index 42b15ae..5c4612a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { transforms, getAbsolutePath } from "./utils"; export default function removeConsole( options: Partial = {} ): PluginOption { - const { external, includes, externalValue } = options || {}; + const { external, includes, externalValue, custom } = options || {}; return { name: "vite:remove-console", apply: "build", @@ -30,7 +30,7 @@ export default function removeConsole( }; } else { return { - code: transforms(_source, includes, externalValue), + code: transforms(_source, includes, externalValue, custom), map: null }; } diff --git a/src/types.ts b/src/types.ts index 2f67f3c..efbf74f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; } diff --git a/src/utils.ts b/src/utils.ts index 1b66f05..bc9b6bb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 { @@ -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();