Skip to content

显著功能

¥Notable Features

本页记录了 Rolldown 中一些值得注意的功能,这些功能在 Rollup 中没有内置的等效功能。

¥This page documents some notable features in Rolldown that do not have built-in equivalents in Rollup.

平台预设

¥Platform presets

  • 可通过 platform 选项配置。

    ¥Configurable via the platform option.

  • 默认值:'node' 表示 cjs 输​​出,否则为 'browser'

    ¥Default: 'node' for cjs output, 'browser' otherwise

  • 可能的值:browser | node | neutral

    ¥Possible values: browser | node | neutral

esbuild 的 platform 选项 类似,此选项提供了一些关于模块解析和如何处理 process.env.NODE_ENV 的合理默认值。

¥Similar to esbuild's platform option, this option provides some sensible defaults regarding module resolution and how to handle process.env.NODE_ENV.

与 esbuild 的显著差异:

¥Notable differences from esbuild:

  • 无论平台如何,默认输出格式始终为 esm

    ¥The default output format is always esm regardless of platform.

  • 当平台为 browser 时,</script> 没有转义行为。

    ¥No </script> escape behavior when platform is browser.

提示

Rolldown 在针对浏览器时不会对 Node 内置函数进行 polyfill。你可以使用 rolldown-plugin-node-polyfills 选择启用它。

¥Rolldown does not polyfill Node built-ins when targeting the browser. You can opt-in to it with rolldown-plugin-node-polyfills.

内置转换

¥Built-in transforms

Rolldown 开箱即用地支持以下转换,这些转换由 Oxc 提供支持。转换可通过 transform 选项进行配置。支持以下转换:

¥Rolldown supports the following transforms out of the box, powered by Oxc. The transform is configurable via the transform option. The following transforms are supported:

  • TypeScript

    • 当提供 tsconfig 选项时,根据 tsconfig.json 设置配置。

      ¥Sets configurations based on the tsconfig.json when the tsconfig option is provided.

    • 支持旧版装饰器和装饰器元数据。

      ¥Supported legacy decorators and decorator metadata.

  • JSX

  • 语法降低

    ¥Syntax lowering

    • 自动转换现代语法以与你定义的目标兼容。

      ¥Automatically transforms modern syntax to be compatible with your defined target.

    • 支持 向下到 ES2015

      ¥Supports down to ES2015.

CJS 支持

¥CJS support

Rolldown 开箱即用地支持混合 ESM / CJS 模块图,无需 @rollup/plugin-commonjs。它在很大程度上遵循 esbuild 的语义和 通过所有 esbuild ESM / CJS 互操作测试

¥Rolldown supports mixed ESM / CJS module graphs out of the box, without the need for @rollup/plugin-commonjs. It largely follows esbuild's semantics and passes all esbuild ESM / CJS interop tests.

详细信息请参见 打包 CJS

¥See Bundling CJS for more details.

模块解析

¥Module resolution

Rolldown 默认基于 TypeScript 和 Node.js 的行为解析模块,无需 @rollup/plugin-node-resolve

¥Rolldown resolves modules based on TypeScript and Node.js' behavior by default, without the need for @rollup/plugin-node-resolve.

当提供顶层 tsconfig 选项时,Rolldown 将在指定的 tsconfig.json 中遵循 compilerOptions.paths

¥When top-level tsconfig option is provided, Rolldown will respect compilerOptions.paths in the specified tsconfig.json.

定义

¥Define

  • 可通过 define 选项配置。

    ¥Configurable via the define option.

此功能提供了一种用常量表达式替换全局标识符的方法。与 Viteesbuild 中的相应选项保持一致。

¥This feature provides a way to replace global identifiers with constant expressions. Aligns with the respective options in Vite and esbuild.

提示@rollup/plugin-replace behaves differently

请注意,它的行为与 @rollup/plugin-replace 不同,因为替换是基于 AST 的,因此要替换的值必须是有效的标识符或成员表达式。为此,请使用内置的 replacePlugin

¥Note it behaves differently from @rollup/plugin-replace as the replacement is AST-based, so the value to be replaced must be a valid identifier or member expression. Use the built-in replacePlugin for that purpose.

注入

¥Inject

  • 可通过 inject 选项配置。

    ¥Configurable via the inject option.

此功能提供了一种用从模块导出的特定值填充全局变量的方法。此功能等同于 esbuild 的 inject 选项@rollup/plugin-inject

¥This feature provides a way to shim global variables with a specific value exported from a module. This feature is equivalent of esbuild's inject option and @rollup/plugin-inject.

CSS 打包

¥CSS bundling

  • ⚠️ 实验性

    ¥⚠️ Experimental

Rolldown 支持开箱即用地打包从 JS 导入的 CSS。请注意,此功能目前不支持 CSS 模块和最小化。

¥Rolldown supports bundling CSS imported from JS out of the box. Note this feature currently does not support CSS Modules and minification.

高级 Chunk

¥Advanced Chunks

Rolldown 允许精细地控制分块行为,类似于 webpack 的 optimization.splitChunks 功能。

¥Rolldown allows controlling the chunking behavior granularly, similar to webpack's optimization.splitChunks feature.

详细信息请参见 高级 Chunk

¥See Advanced Chunks for more details.

模块类型

¥Module types

  • ⚠️ 实验性

    ¥⚠️ Experimental

这在概念上类似于 esbuild 的 loader 选项,允许用户通过 moduleTypes 选项将文件扩展名全局关联到内置模块类型,或者在插件钩子中指定特定模块的模块类型。在 此处 中更详细地讨论了它。

¥This is conceptually similar to esbuild's loader option, allowing users to globally associate file extensions to built-in module types via the moduleTypes option, or specify module type of a specific module in plugin hooks. It is discussed in more details here.

压缩

¥Minification

最小化由 oxc-minifier 提供支持,该版本目前处于 alpha 阶段,可能仍然存在错误。我们建议在生产环境中彻底测试你的输出。

¥The minification is powered by oxc-minifier, which is currently in alpha and can still have bugs. We recommend thoroughly testing your output in production environments.

如果你更喜欢使用外部压缩器,Rolldown 与 Rollup 压缩器插件兼容,例如:

¥If you prefer an external minifier instead, Rolldown is compatible with Rollup minifier plugins, such as:

rollup-plugin-esbuild

rolldown.config.js
js
import { defineConfig } from 'rolldown';
import { minify } from 'rollup-plugin-esbuild';

export default defineConfig({
  plugins: [minify()],
});

rollup-plugin-swc3

rolldown.config.js
js
import { defineConfig } from 'rolldown';
import { minify } from 'rollup-plugin-swc3';

export default defineConfig({
  plugins: [
    minify({
      module: true,
      // swc's minify option here
      mangle: {},
      compress: {},
    }),
  ],
});