简体   繁体   English

带有动态需求的 Webpack 异步加载

[英]Webpack Async Loading with Dynamic Require

I am very new (hours) to webpack.我对 webpack 很陌生(几个小时)。

I'd like to combine async loading, which is awesome, with dynamic requires.我想将非常棒的异步加载与动态需求结合起来。 Suppose I want to asynchronously require one of two files, './DIRECTORY/FOO' or './DIRECTORY/BAR'.假设我想异步地需要两个文件之一,“./DIRECTORY/FOO”或“./DIRECTORY/BAR”。 Here's a non-dynamic version that, I think, works as I expect:这是一个非动态版本,我认为它按我的预期工作:

if (condition_holds) {
  require.ensure([], function()
                     {
                       require('./DIRECTORY/FOO');
                     });
}
else {
  require.ensure([], function()
                     {
                       require('./DIRECTORY/BAR');
                     });
}

And here's my attempt to make this dynamic:这是我尝试使这种动态:

var file_name = condition_holds ? "FOO" : "BAR";
require.ensure([], function()
                   {
                     require('./DIRECTORY/' + file_name);
                   });

However, when I use webpack to compile these pieces of code, I get very different results.但是,当我使用 webpack 编译这些代码时,我得到了非常不同的结果。 For the first (non-static), two different files are produced, one for FOO and one for BAR, and only that file is asynchronously loaded.对于第一个(非静态),生成两个不同的文件,一个用于 FOO,一个用于 BAR,并且仅异步加载该文件。 For the second, only one file is produced, which contains both FOO and BAR.对于第二个,只生成一个文件,其中包含 FOO 和 BAR。 This is not what I was expecting or want.这不是我所期待或想要的。 I'd like separate files to be produced, one for each module, and just that module to be asynchronously downloaded.我希望生成单独的文件,每个模块一个,并且只异步下载该模块。

Is this expected behavior?这是预期的行为吗? If so, why, and how can use dynamic requires but still get separate modules, each loaded separately?如果是这样,为什么以及如何使用动态需要但仍然获得单独的模块,每个模块单独加载?

Here is my webpack.config.js:这是我的 webpack.config.js:

module.exports =
{
  entry: {
  bundle: './main.js'
  },
  output: {
    path: './public/js/',
    filename: '[name].js',
    publicPath: "/js/"
  }
};

The problem is that webpack statically analyzes your source code.问题是 webpack静态分析你的源代码。 When it sees this:当它看到这个:

require('./DIRECTORY/' + file_name);

...it creates a context which is a mapping that at runtime can resolve to bundled modules. ...它创建了一个上下文,它是一个在运行时可以解析为捆绑模块的映射。 Because file_name can be anything according to static analysis (webpack doesn't actually run your code to know what's available), it determines all possible files that could be under ./DIRECTORY/ and includes them in your bundle, which is not what you want.因为根据静态分析, file_name可以是任何东西(webpack 实际上不会运行你的代码来知道什么是可用的),它确定了所有可能在./DIRECTORY/下的./DIRECTORY/并将它们包含在你的包中,这不是你想要的.

There's really two problems to solve:确实有两个问题需要解决:

  1. Getting webpack to split your files into separate chunks让 webpack 将你的文件拆分成单独的块
  2. Dynamically loading those chunks动态加载这些块

It looks like someone has created a bundle-loader that can solve both of these problems.看起来有人创建了一个可以解决这两个问题的bundle-loader

var file_name = condition_holds ? "FOO" : "BAR";
var dynamicLoad = require('bundle!./DIRECTORY/' + file_name);
dynamicLoad(function (loadedModule) {
  // Use the module
});

According to this write-up , this should split every potential file in this context into its own bundle and give you a way to asynchronously load those bundles at runtime.根据这篇文章,这应该将上下文中的每个潜在文件拆分为自己的包,并为您提供一种在运行时异步加载这些包的方法。

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

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