cari

Rumah  >  Soal Jawab  >  teks badan

node.js - webpack required打包问题

不明白为什么我require了lib.js,不是已经打包进home.min.js了吗,而且home.min.js没require进lib.js写的代码 是我理解错了吗

var webpack = require('webpack'),
    path = require('path'),
    CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");

var chunks = {
    "index": './src/js/index.js'
};


module.exports = {
    entry: chunks,
    output: {
      path: './dist/js/',
      filename: '[name].js'
    },
    module: {
      loaders: [
          {
            test: /\.js[x]?$/,
            loaders: ['babel-loader?presets[]=es2015'],
            exclude: /node_modules/,
            include: path.join(__dirname, '.')
          }
        ]
    },
    // resolve: {
    //     alias: {
    //         js: path.join(__dirname,  "./src/js")
    //     },
    //     extensions: ['', '.js', '.json']
    // },
    plugins: [
      // new webpack.optimize.UglifyJsPlugin(),
      new CommonsChunkPlugin({
          name: "home.min",
          minChunks: 2,
          chunks: chunks
      }),
    ]
};
//index.js
var Hello = require("./lib/lib.js");

new Hello();
//lib.js
var Hello = require("./lib/lib.js");

new Hello();
大家讲道理大家讲道理2780 hari yang lalu602

membalas semua(1)saya akan balas

  • 高洛峰

    高洛峰2017-04-17 15:00:33

          new CommonsChunkPlugin({
              name: "home.min",
              minChunks: 2,
              chunks: chunks
          }),

    minChunks设为2,表示至少要有两个chunk引用的js模块才会被放到你的home.min.js里,你这lib.js只被index.js引用了一次,因此lib.js的内容会放在打包生成后的index.js里

    balas
    0
  • Batalbalas