@ampproject/rollup-plugin-closure-compiler

中文文档
0.27.0发布于 2021/08/11Apache-2.0TypeScriptCommonJS近 30 天下载 4.6万

Rollup + Google Closure Compiler

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Automatic Closure Configuration
  4. Security disclosures
  5. Code of conduct
  6. License

rollup-plugin-closure-compiler

Leverage Closure Compiler to minify and optimize JavaScript with Rollup.

Generally Closure Compiler will produce superior minification than other projects, but historically has been more difficult to use. The goal of this plugin is to reduce this friction.

Installation

npm install @ampproject/rollup-plugin-closure-compiler --save-dev

Usage

Invoke Closure Compiler from your Rollup configuration.

// rollup.config.js
import compiler from '@ampproject/rollup-plugin-closure-compiler';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'iife',
  },
  plugins: [
    compiler(),
  ],
}

If you would like to provide additional flags and options to Closure Compiler, pass them via key-value pairs.

// rollup.config.js
import compiler from '@ampproject/rollup-plugin-closure-compiler';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'iife',
  },
  plugins: [
    compiler({
      formatting: 'PRETTY_PRINT'
    }),
  ],
}

Automatic Closure Configuration

This plugin will modify the enable the assume_function_wrapper output option for Closure Compiler when es format is specifed to Rollup. Note: This is overrideable via passed flags and options.

// rollup.config.js
import compiler from '@ampproject/rollup-plugin-closure-compiler';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'es',
  },
  plugins: [
    compiler(),
  ],
}

If your Rollup configuration outputs an IIFE format bundle with a specified name, this plugin will add an extern to ensure the name does not get mangled. Note: This is overrideable via passed flags and options.

// rollup.config.js
import compiler from '@ampproject/rollup-plugin-closure-compiler';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'iife',
    name: 'MyAwesomeThing'
  },
  plugins: [
    compiler(),
  ],
}

Security disclosures

The AMP Project accepts responsible security disclosures through the Google Application Security program.

Code of conduct

The AMP Project strives for a positive and growing project community that provides a safe environment for everyone. All members, committers and volunteers in the community are required to act according to the code of conduct.

License

rollup-plugin-closure-compiler is licensed under the Apache License, Version 2.0.

版本与发布记录

@ampproject/rollup-plugin-closure-compiler 共发布 61 个版本,首次发布于 2018/06/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.27.0latest2021/08/115 年前
0.26.02020/06/026 年前
0.25.22020/04/146 年前
0.25.12020/04/146 年前
0.25.02020/04/146 年前
0.24.02020/03/076 年前

其他发布通道:next → 0.5.0-alpha.2,beta → 0.9.0-beta.7。

关于 @ampproject/rollup-plugin-closure-compiler 的常见问题

@ampproject/rollup-plugin-closure-compiler 是什么?

Rollup + Google Closure Compiler

如何安装 @ampproject/rollup-plugin-closure-compiler?

在项目目录运行 npm install @ampproject/rollup-plugin-closure-compiler,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:rollup(>=1.27)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ampproject/rollup-plugin-closure-compiler 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ampproject/rollup-plugin-closure-compiler 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ampproject/rollup-plugin-closure-compiler 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ampproject/rollup-plugin-closure-compiler 可以商用吗?

@ampproject/rollup-plugin-closure-compiler 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@ampproject/rollup-plugin-closure-compiler 的下载量有多少?

根据 npm 官方下载统计,@ampproject/rollup-plugin-closure-compiler 在 2026/10/06 – 2026/11/04 这 30 天内被下载 46,077 次,日均约 1,536 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ampproject/rollup-plugin-closure-compiler 还在维护吗?

最新版本 0.27.0 发布于 2021/08/11(5 年前);至今共发布 61 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/06/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。