简体   繁体   English

使 babel 运行时全局到不同的 webpack 包

[英]Make babel runtime global to different webpack bundles

Ecosystem details for my web application:我的 Web 应用程序的生态系统详细信息:

  • ES6 ES6
  • Babel通天塔
  • Webpack网络包

Our application has its own architecture artifact and then each widget onside the page has its own artifact.我们的应用程序有自己的架构工件,然后页面上的每个小部件都有自己的工件。 Everything uses the above mentioned ecosystem things.一切都使用上述生态系统的东西。

For simplicity sake, lets say I have artifact Arch for architecture and w1 and w2 artifact for my 2 widgets.为简单起见,假设我有用于架构的工件Arch和用于我的 2 个小部件的w1w2工件。 If I bundle all these artifacts individually they produce a bundle where the babel-runtime is included in all the three bundles.如果我单独打包所有这些工件,它们会生成一个包,其中babel-runtime包含在所有三个包中。 so when I put them together inside the web page I am basically including same piece of code ( babel-runtime ) 3 times.因此,当我将它们放在网页中时,我基本上包含了 3 次相同的代码( babel-runtime )。

Question: Is their a way to build each bundle individually but not include the babel-runtime code so that I can include babel-runtime code globally to be consumed by all the three bundle files?问题:他们是否可以单独构建每个包但不包含babel-runtime代码,以便我可以全局包含babel-runtime代码以供所有三个包文件使用?

Yes, you should use the CommonsChunckPlugin是的,你应该使用CommonsChunckPlugin

The way to use it is as follows, in your webpack.config.js file:使用方法如下,在你的 webpack.config.js 文件中:

var webpack = require('webpack');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('shared.js');

module.exports = {
  entry: {
    bundle1: '.src/fileForBundle1.js',
    bundle2: '.src/fileForBundle2.js'
  },
  output: {
    path: 'build',
    filename: '[name].js'
  },
  plugins: [
    commonsPlugin,
  ],
  module: {
    loaders: [
      // ...
    ]
  }
}

All the shared code will be in the shared.js file, you just have to include this file in every page that has a different bundle.所有共享代码都将在 shared.js 文件中,您只需将该文件包含在具有不同捆绑包的每个页面中。

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

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