使用webpack的'require'代码拆分多个块生成是行不通的

时间:2016-11-28 23:32:02

标签: webpack code-splitting

我有以下webpack配置:

var path = require('path'),
    webpack = require('webpack'),
    CopyWebpackPlugin = require('copy-webpack-plugin'),
    WebpackStripLoader = require('strip-loader');

const webpack_config = {
  context: path.join(__dirname, 'src'),
  entry: './lib/Polywrap',
  output: {
    path: path.join(__dirname, 'build'),
    chunkFilename: "[id].bundle.js"
    filename: '[name].js'
  },
  plugins: [],
  module: {
    resolve: {
        extensions: ['']
    },
    loaders: [
        {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            include: path.join(__dirname, './src'),
            query : {
              presets: ['es2015'],
            }
        }
    ]
  }
};

//
// Environment-specific confs
//
switch (process.env.NODE_ENV) {
  case "prod":
      console.warn("Prod mode");

      var stripLoader = {
            test: [/\.js$/],
            exclude: /node_modules/,
            loader: WebpackStripLoader.loader('console.log')
        };

      webpack_config.module.loaders.push(stripLoader);

      webpack_config.plugins.push(
          new webpack.DefinePlugin({
              'process.env': {
                  NODE_ENV: JSON.stringify('production')
              }
          })
      );

      webpack_config.plugins.push(
        new webpack.optimize.UglifyJsPlugin({
            include: /\.js$/,
            compress: {
                warnings: false
            }
        })
      );
      break;
    case "dev":
      console.warn("Dev mode");
      // Let's omit this for example's sake
      break;
}

module.exports = webpack_config;

lib/Polywrap.js包含以下代码,尝试阐明code splitting

function getLangObj(templateName) {
    return require("../langs/"+templateName);
}
console.log(getLangObj("it-IT.js"));
console.log(getLangObj("en-US.js"));

我期待发出三个文件:Polywrap和两个块。不幸的是,Polywrap只是捆绑了所有内容:../langs/内的所有js文件都是捆绑在一起的。怎么了?

这是输出:

Prod mode
Hash: f93ad402ea615e465db5
Version: webpack 1.13.3
Time: 1107ms
  Asset     Size  Chunks             Chunk Names
main.js  1.93 kB       0  [emitted]  main
   [1] ./langs ^\.\/.*$ 268 bytes {0} [built]
    + 5 hidden modules

0 个答案:

没有答案