How to bundle vendor scripts separately and require them as needed with Webpack?

后端 未结 4 1744
我寻月下人不归
我寻月下人不归 2020-11-28 17:51

I\'m trying to do something that I believe should be possible, but I really can\'t understand how to do it just from the webpack documentation.

I am writing a JavaSc

4条回答
  •  执念已碎
    2020-11-28 18:14

    In case you're interested in bundling automatically your scripts separately from vendors ones:

    var webpack = require('webpack'),
        pkg     = require('./package.json'),  //loads npm config file
        html    = require('html-webpack-plugin');
    
    module.exports = {
      context : __dirname + '/app',
      entry   : {
        app     : __dirname + '/app/index.js',
        vendor  : Object.keys(pkg.dependencies) //get npm vendors deps from config
      },
      output  : {
        path      : __dirname + '/dist',
        filename  : 'app.min-[hash:6].js'
      },
      plugins: [
        //Finally add this line to bundle the vendor code separately
        new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.min-[hash:6].js'),
        new html({template : __dirname + '/app/index.html'})
      ]
    };
    

    You can read more about this feature in official documentation.

提交回复
热议问题