Skip to content
开发文档
Configuration
模块

模块

¥Modules

SWC 可以使用 ES 模块将你的代码转译为 CommonJS 或 UMD/AMD。默认情况下,模块语句将保持不变。

¥SWC can transpile your code using ES Modules to CommonJS or UMD/AMD. By default, module statements will remain untouched.

CommonJS

要触发 CommonJS 模块,请更改 .swcrc 中的 type

¥To emit a CommonJS module, change the type in .swcrc:

.swcrc
{
  "$schema": "http://json.schemastore.org/swcrc",
  "module": {
    "type": "commonjs",
 
    // These are defaults.
    "strict": false,
    "strictMode": true,
    "lazy": false,
    "noInterop": false
  }
}

ES6

要触发 ES6 模块,请更改 .swcrc 中的 type

¥To emit a ES6 module, change the type in .swcrc:

.swcrc
{
  "module": {
    "type": "es6",
 
    // These are defaults.
    "strict": false,
    "strictMode": true,
    "lazy": false,
    "noInterop": false
  }
}

AMD

要触发 AMD 模块,请更改 .swcrc 中的 type

¥To emit an AMD module, change the type in .swcrc:

.swcrc
{
  "module": {
    "type": "amd",
    // Optional. If specified, swc emits named AMD module.
    "moduleId": "foo",
 
    // These are defaults.
    "strict": false,
    "strictMode": true,
    "lazy": false,
    "noInterop": false
  }
}

UMD

要触发 UMD 模块,请更改 .swcrc 中的 type

¥To emit an UMD module, change the type in .swcrc:

.swcrc
{
  "module": {
    "type": "umd",
    "globals": {},
 
    // These are defaults.
    "strict": false,
    "strictMode": true,
    "lazy": false,
    "noInterop": false
  }
}

共享选项

¥Shared Options

这些选项由 .swcrc 内的 commonjs / es6 / umd / amd 共享:

¥These options are shared by commonjs / es6 / umd / amd inside .swcrc:

.swcrc
{
  "module": {
    // You can specify "commonjs", "es6", "amd", "umd"
    "type": "commonjs",
    "strict": false,
    "strictMode": true,
    "lazy": false,
    "noInterop": false,
    "ignoreDynamic": false
  }
}

strict

默认为 false。默认情况下,当使用 SWC 导出时,会导出不可枚举的 __esModule 属性。在某些情况下,此属性用于确定导入是默认导出还是包含默认导出。

¥Defaults to false. By default, when using exports with SWC, a non-enumerable __esModule property is exported. In some cases, this property is used to determine if the import is the default export or if it contains the default export.

要防止导出 __esModule 属性,可以将 strict 选项设置为 true

¥To prevent the __esModule property from being exported, you can set the strict option to true.

strictMode

默认为 true。如果为 true,swc 会触发 '使用严格' 指令。

¥Defaults to true. If true, swc emits 'use strict' directive.

lazy

默认为 false。此选项将 Babel 编译的 import 语句更改为在首次使用导入的绑定时延迟评估。这可以缩短模块的初始加载时间,因为有时完全不需要预先评估依赖。在实现库模块时尤其如此。

¥Defaults to false. This option changes Babel's compiled import statements to be lazily evaluated when their imported bindings are used for the first time. This can improve the initial load time of your module because evaluating dependencies upfront is sometimes entirely unnecessary. This is especially the case when implementing a library module.

lazy 的值有一些可能的影响:

¥The value of lazy has a few possible effects:

  • false - 任何导入模块都不会延迟初始化。

    ¥false - No lazy initialization of any imported module.

  • true - 不要延迟初始化本地 ./foo 导入,而是延迟初始化 foo 依赖。本地路径更有可能具有循环依赖,如果延迟加载可能会破坏,因此默认情况下它们不是延迟加载的,而独立模块之间的依赖很少是循环的。

    ¥true - Do not lazy-initialize local ./foo imports, but lazy-init foo dependencies. Local paths are much more likely to have circular dependencies, which may break if loaded lazily, so they are not lazy by default, whereas dependencies between independent modules are rarely cyclical.

  • Array<string> - 延迟初始化源与给定字符串之一匹配的所有导入。

    ¥Array<string> - Lazy-initialize all imports with source matching one of the given strings.

导入永远不能偷懒的两种情况是:

¥The two cases where imports can never be lazy are:

  • import "foo"; 副作用导入自动是非惰性的,因为它们的存在意味着对以后的启动初始化没有任何约束。

    ¥import "foo"; Side-effect imports are automatically non-lazy since their very existence means that there is no binding to later kick-off initialization.

  • export from "foo" 重新导出所有名称需要预先执行,否则无法知道需要导出哪些名称。

    ¥export from "foo" Re-exporting all names requires up-front execution because otherwise there is no way to know what names need to be exported.

noInterop

默认为 false。默认情况下,当使用 swc 导出时,会导出不可枚举的 __esModule 属性。然后,此属性用于确定导入是否是默认导出,或者是否包含默认导出。

¥Defaults to false. By default, when using exports with swc a non-enumerable __esModule property is exported. This property is then used to determine if the import is the default export or if it contains the default export.

如果不需要自动展开默认值,你可以将 noInterop 选项设置为 true 以避免使用 interopRequireDefault 辅助程序(如上面的内联形式所示)。

¥In cases where the auto-unwrapping of default is not needed, you can set the noInterop option to true to avoid the usage of the interopRequireDefault helper (shown in inline form above).

ignoreDynamic

如果设置为 true,将保留动态导入。

¥If set to true, dynamic imports will be preserved.

importInterop

可能的值:

¥Possible values:

  • swc(别名:babel

    ¥swc (alias: babel)

  • node

  • none

如果 noInterop 为 true,则默认为 none,否则默认为 swc

¥Defaults to none if noInterop is true, and swc otherwise.

resolveFully

当设置为 true 时,完全解析模块 import 文件路径,包括任何以 index.js 结尾的文件路径。

¥When set to true, fully resolves module import file paths, including any that end with index.js.

Last updated on November 24, 2023