在构建产物中保留指定的注释
构建工具通常会移除代码中的普通注释,但是在某些场景下,我们希望在构建产物中保留特定的注释, 例如 uni-app 的 条件编译 注释。
插件基于构建工具(如 ESBuild)提供的
Legal comments 机制,
为指定的注释临时添加 ! 前缀使其成为「合法注释」,这些注释在构建产物中就会被保留,从而达到预期目标。
# pnpm
pnpm add -D @uni-ku/unplugin-keep-comments
# yarn
yarn add --dev @uni-ku/unplugin-keep-comments
# npm
npm install -D @uni-ku/unplugin-keep-comments// tsdown.config.ts
import keepComments from "@uni-ku/unplugin-keep-comments/rolldown";
import { defineConfig } from "tsdown";
export default defineConfig({
// ...
plugins: [
keepComments({
comments: [
/^\/\/ *#if/,
/^\/\/ *#endif/
]
})
]
});// build.config.ts
import keepComments from "@uni-ku/unplugin-keep-comments/rollup";
import type { Plugin } from "rollup";
import { defineBuildConfig } from "unbuild";
export default defineBuildConfig({
// ...
hooks: {
"rollup:options": (_, options) => {
options.plugins.push(
keepComments({
comments: [
/^\/\/ *#if/,
/^\/\/ *#endif/
]
}) as Plugin
);
}
}
});MIT LICENSE