简体   繁体   English

Webpack 代码拆分 - 通过请求单个块自动加载所有块?

[英]Webpack code splitting - Automatically load all chunks by requesting a single chunk?

I'm using Webpack 4.x and have the following in my config:我正在使用 Webpack 4.x 并在我的配置中包含以下内容:

splitChunks: {
    chunks: 'all',
},
runtimeChunk: true

This results in three chunks in my project:这导致我的项目分为三个部分:

  • app.prod.js应用程序.prod.js
  • 1.app.prod.js 1.app.prod.js
  • 2.app.prod.js 2.app.prod.js

I'd like to load all three scripts using just one initial request.我想只使用一个初始请求加载所有三个脚本。 The reason for this is that I'd like to use the "preload" feature in Electron for security reasons, which accepts a single script.这样做的原因是出于安全原因,我想在 Electron 中使用“预加载”功能,它接受单个脚本。

Is there a way to have the initial app.prod.js require/import the additional chunks automatically?有没有办法让初始app.prod.js自动要求/导入额外的块?

Webpack will automatically split chunks based on these conditions: Webpack将根据这些条件自动拆分块

  • New chunk can be shared OR modules are from the node_modules folder可以共享新块或模块来自 node_modules 文件夹
  • New chunk would be bigger than 20kb (before min+gz)新块将大于 20kb (在 min+gz 之前)
  • Maximum number of parallel requests when loading chunks on demand would be lower or equal to 30按需加载块时的最大并行请求数将低于或等于 30
  • Maximum number of parallel requests at initial page load would be lower or equal to 30初始页面加载时的最大并行请求数将低于或等于 30

When trying to fulfill the last two conditions, bigger chunks are preferred.当试图满足最后两个条件时,首选更大的块。

So check the size of the chunks and raise it if you need to generate only one chunk.因此,如果您只需要生成一个块,请检查块的大小并提高它。

splitChunks: {
  chunks: 'async',
  minSize: 35000,    // => HERE      Raise minSize of Chunk
}

I Hope that works我希望这行得通

It is not possible to automatically load all chunks by requesting a single chunk using Webpack. Chunks are created and loaded independently, and each chunk must be explicitly requested using the import() function.不可能通过使用 Webpack 请求单个块来自动加载所有块。块是独立创建和加载的,必须使用import() function 显式请求每个块。

If you want to load all of the chunks in your application using a single request, you could consider bundling all of your code into a single file using the optimization.concatenateModules option in Webpack. This will allow you to create a single file that contains all of your code, which you can then load using a single request.如果您想使用单个请求加载应用程序中的所有块,您可以考虑使用 Webpack 中的optimization.concatenateModules选项将所有代码捆绑到一个文件中。这将允许您创建一个包含所有代码的文件您的代码,然后您可以使用单个请求加载这些代码。

module.exports = {
  // Other options...
  optimization: {
    concatenateModules: true
  }
};

Yes, you can use the import() function to dynamically import additional chunks in your initial script.是的,您可以使用import() function 在初始脚本中动态导入其他块。

Here's an example of how you can do this:以下是如何执行此操作的示例:

import(/* webpackChunkName: "chunk-1" */ './chunk-1').then(({ default: chunk1 }) => {
  // Use chunk1 here
 });

import(/* webpackChunkName: "chunk-2" */ './chunk-2').then(({ default: chunk2 }) => {
  // Use chunk2 here
});

You can also use the async/await syntax to make the code look cleaner:您还可以使用 async/await 语法使代码看起来更简洁:

async function importChunks() {
  const chunk1 = await import(/* webpackChunkName: "chunk-1" */ './chunk-1');
  const chunk2 = await import(/* webpackChunkName: "chunk-2" */ './chunk-2');

  // Use chunk1 and chunk2 here
}

Note that the webpackChunkName comment is optional, but it can be useful for giving the chunk a more descriptive name in the output.请注意, webpackChunkName注释是可选的,但它对于在 output 中为块赋予更具描述性的名称很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM