CC 4.0 许可证

本节内容来自以下链接的内容,并受 CC BY 4.0 许可证的约束。

除非另有说明,否则以下内容可以假定为基于原始内容修改和删除的结果。

入口

  • 类型: string | string[] | Record<string, string | string[] | EntryDescription> | Function
  • 默认值:'./src/index.js'

entry 配置用于设置 Rspack 构建的入口模块。

单入口

如果您正在构建单页应用程序或库,您通常只需要设置一个入口点。

要设置单入口,只需将入口模块的路径作为字符串传递给 entry 配置。

rspack.config.js
module.exports = {
  entry: './src/index.js',
};

以上写法会自动将入口模块名称设置为 main,等效于以下写法

rspack.config.js
module.exports = {
  entry: {
    main: './src/index.js',
  },
};

路径类型

入口模块的路径可以是相对路径或绝对路径。

如果 entry 设置为相对路径,Rspack 将使用 上下文配置 中设置的值作为基路径,默认情况下是 Node.js 进程的当前工作目录,即 process.cwd ()

您也可以使用 Node.js 中的 路径模块 生成绝对路径并将其传递给 entry 配置

rspack.config.js
const path = require('path');

module.exports = {
  entry: path.join(__dirname, './src/index.js'),
};

入口数组

设置入口值时,除了设置为 string 外,还可以传入 string[],表示入口包含多个入口模块。

例如,以下示例将 pre.jspost.js 构建到 page 的输出中。

rspack.config.js
module.exports = {
  entry: {
    page: ['./src/pre.js', './src/post.js'],
  },
};

多个模块会根据数组定义的顺序依次执行,所以 pre.js 中的代码会先于 post.js 中的代码执行。

多入口

如果您需要一次构建多个入口,则应该将 entry 设置为一个对象,对象中的每个键对应一个入口名称。

例如,以下示例将 page1page2 构建为两个入口

rspack.config.js
module.exports = {
  entry: {
    page1: './src/page1/index.js',
    page2: './src/page2/index.js',
  },
};

入口描述对象

entry 设置为对象时,可以将入口的值设置为描述对象。描述对象可以包含以下属性

EntryDescription.import

  • 类型: string[] | string
  • 默认值:'./src/index.js'

入口模块的路径。

EntryDescription.runtime

  • 类型: false | string
  • 默认值:undefined

运行时块的名称。当 runtime 设置时,将创建一个新的运行时块。您也可以将其设置为 false 以避免创建新的运行时块。

runtime 属性用于设置运行时块的名称,例如将 main 入口块的名称设置为 'foo'

rspack.config.js
module.exports = {
  entry: {
    main: {
      import: './src/index.js',
      runtime: 'foo',
    },
  },
};

EntryDescription.chunkLoading

  • 类型: false | string | 'jsonp' | 'import-scripts' | 'require' | 'async-node' | 'import'
  • 默认值:undefined

此入口如何加载其他块。

EntryDescription.asyncChunks

  • 类型: boolean
  • 默认:true

是否为该入口创建一个按需加载的异步块。

EntryDescription.publicPath

  • 类型: 'auto' | string | (pathData: PathData, assetInfo?: AssetInfo) => string
  • 默认值:undefined

此入口引用的资源的 publicPath。

EntryDescription.baseUri

  • 类型: string
  • 默认值:undefined

此入口引用的资源的 baseURI。

EntryDescription.filename

  • 类型: string
  • 默认值:undefined

入口块的文件名。

EntryDescription.library

  • 类型: string | string[] | object
  • 默认值:undefined

由该入口生成的块作为库的格式,有关详细配置,请参阅 output.library

EntryDescription.dependOn

  • 类型: string[] | string
  • 默认值:undefined

当前入口所依赖的入口。使用 dependOn 选项,您可以将一个入口块中的模块共享到另一个入口块。

EntryDescription.wasmLoading

  • 类型: 'fetch' | 'async-node'
  • 默认值:undefined

用于设置加载 WebAssembly 模块方法的选项。默认包含的方法是 'fetch'(web/WebWorker)、'async-node'(Node.js),但插件可能会添加其他方法。

默认值可能会受到不同 target 的影响

  • 如果目标设置为 'web''webworker''electron-renderer''node-webkit',则默认为 'fetch'
  • 如果目标设置为 'node''async-node''electron-main''electron-preload',则默认为 'async-node'

EntryDescription.layer

  • 类型: string | null | undefined
  • 默认值:undefined

此入口的层级,通过层级匹配在拆分块中使相应的配置生效,rules、统计信息和外部模块。

警告

此配置仅在 experiments.layerstrue 时生效。

rspack.config.js
module.exports = {
  entry: {
    index: {
      import: './src/index.js',
      layer: 'layer-name',
    },
  },
  experiments: {
    layers: true,
  },
};

动态入口

如果传递了一个函数,则该函数将在每次 make 事件上被调用。

请注意,make 事件会在 webpack 启动时触发,以及在 监视文件更改 时每次失效时触发。

rspack.config.js
module.exports = {
  //...
  entry: () => './demo',
};

或者

rspack.config.js
module.exports = {
  //...
  entry: () => new Promise(resolve => resolve(['./demo', './demo2'])),
};

例如:您可以使用动态入口从外部来源(远程服务器、文件系统内容或数据库)获取实际的入口。

rspack.config.js
module.exports = {
  entry() {
    return fetchPathsFromSomeExternalSource(); // returns a promise that will be resolved with something like ['src/main-layout.js', 'src/admin-layout.js']
  },
};

output.library 选项结合使用:如果传递的是数组,则仅导出最后一个项目。