【问题标题】:Can I bundle the common module when bundling in webpack?webpack打包的时候可以打包通用模块吗?
【发布时间】:2017-07-05 02:19:57
【问题描述】:

关于 webpack 的问题。当使用 webpack 捆绑模块时,根据入口点的位置运行应用程序。此时,当入口点设置为多个时,会创建一个按入口点数量构建的文件。

如果bundle是按入口打包成Webpack的,可以打包成bundle A和B。这个时候我在A bundle中使用了一个叫HELLO的模块,在B模块中使用了一个叫HELLO的模块。

此时与webpack捆绑时,只单独捆绑常用的模块(HELLO模块)。

我可以制作 A 包、B 包、H 包吗?我想在 A bundle 中调用 H bundle 并使其可用。

下面是我的 webpack 代码。我使用多个条目来多个捆绑

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"]
    }
};

【问题讨论】:

    标签: javascript node.js typescript module webpack


    【解决方案1】:
    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 将生成 A.js、B.js 和 H.js,其中 H.js 将具有 A 和 B 使用的共享模块。但是在包含 A 和 B 之前,必须先通过 script 标签包含 H

    这样的?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 2011-03-14
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多