简体   繁体   English

如何配置webpack的自动代码拆分功能以加载相对于构建输出文件夹的模块?

[英]How can I configure webpack's automatic code splitting to load modules relative to the build output folder?

I'm using Webpack 2's automatic code splitting to divide up my application into multiple output files. 我正在使用Webpack 2的自动代码拆分功能,将我的应用程序分成多个输出文件。 My webpack.config.js file is: 我的webpack.config.js文件是:

const path = require('path');

module.exports = {
  entry: './app.js',
  output: {
    filename: '[name].js',
    path: path.join(__dirname, 'build'),
  }
}

This correctly builds my app.js file into the build folder along with the other module files. 这样可以将我的app.js文件与其他模块文件正确构建到build文件夹中。 The build directory has the following files: 构建目录包含以下文件:

main.js
0.js

My issue is that when main.js requests 0.js , it does so at the root of my application, rather than relative to its current location. 我的问题是,当main.js请求0.js ,它是在应用程序的根目录而不是相对于其当前位置进行的。 So it's basically trying to load localhost:8000/0.js when it should be loading localhost:8000/build/0.js . 因此,它基本上应该在应该加载localhost:8000/build/0.js时尝试加载localhost:8000/0.js How can I configure my project so that my project correctly loads split modules relative to my main entry point's path? 如何配置项目,以便项目相对于主入口点的路径正确加载拆分的模块?

By default Webpack will load modules from the root directory. 默认情况下,Webpack将从根目录加载模块。 You can configure what folder Webpack will load from by declaring the __webpack_public_path__ variable in your entry point. 您可以通过在入口点声明__webpack_public_path__变量来配置Webpack将从哪个文件夹加载。 So, for this scenario, you could use: 因此,对于这种情况,您可以使用:

__webpack_public_path__ = 'build/';

Webpack will then load modules from the build folder. 然后,Webpack将从build文件夹加载模块。 However this is still relative to the root. 但是,这仍然是相对于根的。 In order to tell Webpack to load scripts relative to the entry point, you can dynamically configure __webpack_public_path__ to the directory of your entry point script with: 为了告诉Webpack加载相对于入口点的脚本,您可以使用以下方法将__webpack_public_path__动态配置为入口点脚本的目录:

const scriptURL = document.currentScript.src;
__webpack_public_path__ = scriptURL.slice(0, scriptURL.lastIndexOf('/') + 1);

document.currentScript is well supported in evergreen browsers , but is not supported in Internet Explorer. 常绿浏览器很好地支持 document.currentScript ,但Internet Explorer不支持。

UPDATE: 更新:

Code splitting works mostly if you have multiple entry points, in this case you can load part of your code on demand. 如果您有多个入口点,那么代码拆分通常会起作用,在这种情况下,您可以按需加载部分代码。 (eg in requestidlecallback or in setTimeout, doesn't matter). (例如,在requestidlecallback或setTimeout中,都没有关系)。

But usually it's used for routing when you wouldn't like to load logic for settings on / , but would like to load it on /settings . 但通常它是用于路由,当你不希望加载逻辑上的设置/ ,但希望其装载到/settings

So you can add something like 所以你可以添加类似

// goes to `/settings`
button.addEventListener('click', () => {
  require.ensure([], () => {
    require('./settings')
  })
})

Have you checked require.ensure() ? 您是否检查了require.ensure()

webpack statically parses for require.ensure() in the code while building and adds the modules here into a separate chunk. webpack在构建时会静态解析代码中的require.ensure()并将此处的模块添加到单独的块中。 This new chunk is loaded on demand by webpack through jsonp. Webpack通过jsonp按需加载此新块。

https://webpack.js.org/guides/code-splitting-require/#require-ensure- https://webpack.js.org/guides/code-splitting-require/#require-ensure-

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

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