[英]WebPack: Multiple entry points without additional chunks
I want to use WebPack to build my JavaScript application.我想使用 WebPack 来构建我的 JavaScript 应用程序。 The application is supposed to be deployed on several sites, in several different modifications.该应用程序应该以几种不同的修改形式部署在多个站点上。 Therefore, I have set up the config to use multiple entry points, one per each site:因此,我将配置设置为使用多个入口点,每个站点一个:
entry: {
s1: "s1",
s2: "s2",
s3: "s3"
},
The application also uses several dependencies, which are loaded via AMD modules (I used RequireJS before):该应用程序还使用了几个依赖项,它们是通过 AMD 模块加载的(我之前使用过 RequireJS):
c1
c2
...
Let's say that s1
requires both c1
and c2
, whereas s2
needs only c1
.假设s1
需要c1
和c2
,而s2
只需要c1
。 The build works OK, and it creates s1
, s2
and s3
as the entry points, and several chunks containing various combinations of the components, as the sites need:构建工作正常,它创建s1
、 s2
和s3
作为入口点,以及几个包含各种组件组合的块,根据站点需要:
/* s1 */
define(['c1', 'c2'], function(c1, c2) { ... }
The question I have is: What do I need to specify in the configuration in order to get each site package built as one standalone file?我的问题是:为了将每个站点 package 构建为一个独立文件,我需要在配置中指定什么? I appreciate the ability of WebPack to split the application into chunks, but right now I need every build to be one JS file (for various reasons).我很欣赏 WebPack 将应用程序分成块的能力,但现在我需要每个构建都是一个 JS 文件(出于各种原因)。
I am able to achieve that easily when I configure only a single entry point (such as entry: "s1"
), using the following:当我只配置一个入口点(例如entry: "s1"
)时,我可以使用以下命令轻松实现这一点:
webpack.optimize.LimitChunkCountPlugin({maxChunks: 1})
However, for multiple entry points, this configuration creates one additional chunk on top of all entry points.但是,对于多个入口点,此配置会在所有入口点之上创建一个额外的块。 How can I make sure each built entry point (such as s1.bundle.js
, s2.bundle.js
, ...) contains all JavaScript inside that one file, and doesn't need to load any chunks on demand?我如何确保每个构建的入口点(例如s1.bundle.js
, s2.bundle.js
,...)包含该文件中的所有 JavaScript ,并且不需要按需加载任何块?
Sounds like the easiest way is to have multiple builds (each with one entry point) rather than a single build with multiple entry points. 听起来最简单的方法是具有多个构建(每个构建都有一个入口点),而不是具有多个入口的单个构建。 If your webpack.config.js exports an array of config objects instead of a single config object, webpack will automatically do a separate build for each config. 如果您的webpack.config.js导出配置对象数组而不是单个配置对象,则webpack将自动为每个配置进行单独的构建。 So you could, for example, put your entry points in an array and loop through it, creating a config object for each. 因此,例如,您可以将入口点放在数组中并遍历数组,为每个数组创建一个配置对象。 Now each config has only a single entry point, and the LimitChunkCountPlugin
should give you the single file. 现在,每个配置只有一个入口点,并且LimitChunkCountPlugin
应该为您提供单个文件。
In Webpack 5, you can use chunkLoading: false
in your entry definition.在 Webpack 5 中,您可以在您的条目定义中使用chunkLoading: false
。
{
// ...
entry: {
s1: {
import: "s1",
chunkLoading: false
}
s2: {
import: "s2",
chunkLoading: false
}
s3: {
import: "s3",
chunkLoading: false
}
},
// ...
}
https://webpack.js.org/configuration/entry-context/#entry-descriptor https://webpack.js.org/configuration/entry-context/#entry-descriptor
Copying the code example from the link above in case the link breaks in the future:从上面的链接复制代码示例,以防将来链接断开:
module.exports = {
//...
entry: {
home: './home.js',
shared: ['react', 'react-dom', 'redux', 'react-redux'],
catalog: {
import: './catalog.js',
filename: 'pages/catalog.js',
dependOn: 'shared',
chunkLoading: false, // Disable chunks that are loaded on demand and put everything in the main chunk.
},
personal: {
import: './personal.js',
filename: 'pages/personal.js',
dependOn: 'shared',
chunkLoading: 'jsonp',
asyncChunks: true, // Create async chunks that are loaded on demand.
layer: 'name of layer', // set the layer for an entry point
},
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.