Skip to content

uni-ku/unplugin-keep-comments

Repository files navigation

Unplugin Keep Comments

在构建产物中保留指定的注释

github stars npm version npm downloads JSDocs License

☕️ 背景

构建工具通常会移除代码中的普通注释,但是在某些场景下,我们希望在构建产物中保留特定的注释, 例如 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

// tsdown.config.ts

import keepComments from "@uni-ku/unplugin-keep-comments/rolldown";
import { defineConfig } from "tsdown";

export default defineConfig({
  // ...
  plugins: [
    keepComments({
      comments: [
        /^\/\/ *#if/,
        /^\/\/ *#endif/
      ]
    })
  ]
});

Unbuild

// 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

About

💬 在构建产物中保留指定的注释

Resources

License

Stars

Watchers

Forks

Packages

No packages published