Skip to content

platform

  • 类型:'node' | 'browser' | 'neutral'

    ¥Type: 'node' | 'browser' | 'neutral'

  • 默认值:如果格式为 'cjs',则为 'node',否则为 'browser'

    ¥Default: 'node' if format is 'cjs', 'browser' otherwise

代码运行的预期平台。这会影响其他选项的默认值,并提供类似于 esbuild 的平台特定预设。

¥Expected platform where the code will run. This affects default values for other options and provides platform-specific presets similar to esbuild.

示例

¥Examples

浏览器平台

¥Browser platform

js
export default {
  platform: 'browser',
  output: {
    format: 'esm',
  },
};

Node.js 平台

¥Node.js platform

js
export default {
  platform: 'node',
  output: {
    format: 'cjs',
  },
};

平台无关

¥Platform-neutral

js
export default {
  platform: 'neutral',
  output: {
    format: 'esm',
  },
};

深入探讨

¥In-depth

平台设置为模块解析和特定于环境的行为提供了合理的默认值,类似于 esbuild 的 platform 选项。

¥The platform setting provides sensible defaults for module resolution and environment-specific behavior, similar to esbuild's platform option.

'node'

针对 Node.js 环境进行了优化:

¥Optimized for Node.js environments:

  • 条件:根据输出格式包含 'node''import''require'

    ¥Conditions: Includes 'node', 'import', 'require' based on output format

  • 主要字段:['main', 'module']

    ¥Main fields: ['main', 'module']

  • 目标:Node.js 运行时行为

    ¥Target: Node.js runtime behavior

  • process.env 处理:保留 process.env.NODE_ENV 和其他 Node.js 全局变量

    ¥process.env handling: Preserves process.env.NODE_ENV and other Node.js globals

'browser'

针对浏览器环境进行了优化:

¥Optimized for browser environments:

  • 条件:包含 'browser''import''module''default'

    ¥Conditions: Includes 'browser', 'import', 'module', 'default'

  • 主要字段:['browser', 'module', 'main'] - 优先使用浏览器特定的入口点

    ¥Main fields: ['browser', 'module', 'main'] - prefers browser-specific entry points

  • 目标:浏览器运行时行为

    ¥Target: Browser runtime behavior

  • 内置插件:默认情况下,Node.js 内置模块未进行 polyfill。

    ¥Built-ins: Node.js built-in modules are not polyfilled by default

提示

对于浏览器版本,你可能需要使用 rolldown-plugin-node-polyfills 来填充 Node.js 内置函数(如果需要)。

¥For browser builds, you may want to use rolldown-plugin-node-polyfills to polyfill Node.js built-ins if needed.

'neutral'

平台无关配置:

¥Platform-agnostic configuration:

  • 默认格式:始终为 'esm'

    ¥Default format: Always 'esm'

  • 条件:仅包含格式特定的条件,不包含平台特定的条件

    ¥Conditions: Only includes format-specific conditions, no platform-specific ones

  • 主要字段:默认为空 - 依赖于 package.json 中的 "exports" 字段

    ¥Main fields: Empty by default - relies on package.json "exports" field

  • 用例:可在多种环境下运行的通用库

    ¥Use cases: Universal libraries that run in multiple environments

与 esbuild 的区别

¥Difference from esbuild

与 esbuild 的 platform 选项的显著区别:

¥Notable differences from esbuild's platform option:

  • 无论平台如何,默认输出格式始终为 'esm'(在 esbuild 中,Node.js 默认为 'cjs')。

    ¥The default output format is always 'esm' regardless of platform (in esbuild, Node.js defaults to 'cjs')

  • 平台为 'browser' 时,无自动 </script> 转义行为

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

选择平台

¥Choosing a Platform

在以下情况下使用 'browser'

¥Use 'browser' when:

  • 构建 Web 应用

    ¥Building for web applications

  • 针对支持 ES 模块的现代浏览器

    ¥Targeting modern browsers with ES modules support

  • 需要浏览器特有的包入口点

    ¥Need browser-specific package entry points

在以下情况下使用 'node'

¥Use 'node' when:

  • 构建服务器端应用

    ¥Building server-side applications

  • 创建 CLI 工具

    ¥Creating CLI tools

  • 需要 Node.js 特有的功能和模块

    ¥Need Node.js-specific features and modules

在以下情况下使用 'neutral'

¥Use 'neutral' when:

  • 构建通用库

    ¥Building universal libraries

  • 希望获得最大程度的可移植性

    ¥Want maximum portability

  • 避免特定于平台的假设

    ¥Avoiding platform-specific assumptions