简体   繁体   中英

Can I bundle the common module when bundling in webpack?

Question about webpack. When bundling modules with webpack Runs the application based on location at the entry point. At this time, when the entry point is set to multiple, a file that is built by the number of entry points is created.

If bundles are bundled into Web packs on an entry basis, they can be bundled into bundles A and B. At this time, I used a module called HELLO in A bundle and a module called HELLO in B module.

At this time, when bundling with the web pack, only the modules used in common (HELLO module) are bundled separately.

Can I make A bundle, B bundle, H bundle? What I want to call H bundle in A bundle and make it available.

Below is my webpack code. I using multiple entries to multiple bundle

module.exports = {
    devtool: 'source-map',
    entry: ['./public/js/index.ts', './public/js/admin/member.ts'],
    output: {
        path: 'dist/public/js',
        filename: '[name].js'
    },
    module: {
        loaders: [{
            test: /\.tsx?$/,
            loader: 'ts-loader',
            options: {
                transpileOnly: true
            }
        }]
    },
    resolve: {
        extensions: [".ts", ".js"]
    }
};
module.exports = {
  devtool: 'source-map',
  entry: {
    A: ['./public/js/index.ts'],
    B: ['./public/js/admin/member.ts']
  },
  output: {
    path: 'dist/public/js',
    filename: '[name].js'
  },
  module: {
    loaders: [{
        test: /\.tsx?$/,
        loader: 'ts-loader',
        options: {
            transpileOnly: true
        }
    }]
  },
  resolve: {
    extensions: [".ts", ".js"]
  },
  plugins: [
     new CommonsChunkPlugin({
     name: 'H',
     minChunks: 2
   })
};

webpack will make A.js, B.js, and H.js where H.js will have shared modules that A and B use. But H would have to be included first via script tag before including A and B

Something like this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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