简体   繁体   English

如何构建从另一个入口点包导入模块的webpack包?

[英]How can I build a webpack bundle that imports a module from another entry point bundle?

I'm trying to generate a second webpack bundle that's dependent on another bundle. 我正在尝试生成第二个依赖于另一个bundle的webpack包。 Every page needs bundle-one, but only some pages need bundle-two. 每个页面都需要bundle-one,但只有一些页面需要bundle-two。

For instance, let's say I have the following entry point scripts (these are trivial examples, just using them to get the point across): 例如,假设我有以下入口点脚本(这些是简单的示例,只是使用它们来解决问题):

bundle-one.js 维管束one.js

import $ from 'jquery';
$(document).data('key', 'value');

bundle-two.js 维管束two.js

import $ from 'jquery';
const value = $(document).data('key');

I know that I can use CommonsChunkPlugin to generate a commons.js file containing the WebPack loader and jQuery, but that would require loading both commons.js and bundle-one.js on every page, even when I don't need to load bundle-two.js. 我知道我可以使用CommonsChunkPlugin生成包含WebPack加载器和jQuery的commons.js文件,但这需要在每个页面上加载commons.js和bundle-one.js,即使我不需要加载bundle -two.js.

So, basically: is there a way to build bundle-one.js as the "main" JavaScript bundle for all my pages, and then have bundle-two.js setup to load jQuery from bundle-one.js? 所以,基本上:有没有办法将bundle-one.js构建为我所有页面的“主”JavaScript包,然后使用bundle-two.js设置从bundle-one.js加载jQuery?

Option 1 选项1

Have two separate Webpack configs, one for each bundle. 有两个单独的Webpack配置,每个捆绑一个。 In your first bundle, expose jQuery as a global variable when you first require it using the expose-loader : 在第一个包中, 当您第一次使用expose-loader 需要它时jQuery公开为全局变量

loaders: [
    { test: require.resolve('jquery'), loader: 'expose?jQuery!expose?$' }
]

Then, in your second bundle config, tell Webpack that jQuery is "external" and shouldn't be bundled in with the rest of the code: 然后,在你的第二个bundle配置中, 告诉Webpack jQuery是“外部的” ,不应该与其余的代码捆绑在一起:

{
    externals: {
        // require("jquery") is external and available
        //  on the global var jQuery
        "jquery": "jQuery"
    }
}

This way the first bundle exposes jQuery as a global variable, then the second bundle looks for that global variable instead of including the source. 这样第一个bundle将jQuery暴露为全局变量,然后第二个bundle查找该全局变量而不是包含源。

Option 2 选项2

I'm not sure if this will work, but the CommonsChunkPlugin documentation says you can specify the name config option as an existing chunk. 我不确定这是否可行,但CommonsChunkPlugin文档说您可以将name配置选项指定为现有块。 You try setting the name to your bundle1 entry point chunk name, and in theory jQuery (and other libs required across all chunks) will be built into bundle 1, and not bundle 2. 您尝试将名称设置为bundle1入口点块名称,理论上jQuery(以及所有块中需要的其他库)将构建到bundle 1中,而不是bundle 2。

You can do this in the following way using the provide plugin - 您可以使用提供插件以下列方式执行此操作 -

//webpack.config.js
module.exports = {
  entry: './index.js',
  output: { 
    filename: '[name].js' 
  },
  externals: {
    jquery: 'jQuery'
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
    })
  ]
};

This can be useful for refactoring legacy code with a lot of different files referencing $. 这对于使用引用$的许多不同文件重构遗留代码非常有用。

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

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