LoginSignup
3
2

More than 5 years have passed since last update.

gulpでwebpackのCommonsChunkPluginを使う

Last updated at Posted at 2016-03-04

なにやらハマったので覚え書き。Gruntの場合とは異なる模様。
これが正攻法なのかは不明だけど、とりあえず動いた。
ちなみに環境はWindows 7です。

gulpfile.js
var webpack = require('webpack-stream');


// requireする必要あり。ここのパスは環境によって異なるかも..。
var CommonsChunkPlugin = require('webpack-stream/node_modules/webpack/lib/optimize/CommonsChunkPlugin.js');

gulp.task('webpack', function() {
    gulp.src('./src/js/**/*.js')
      .pipe(webpack({
          entry: {
            hoge: "./src/js/hoge.js",
            foo: './src/js/foo.js'
          },
          output: {
            path: '/js/',
            publicPath: '/js/',
            filename: '[name].bundle.js',
            chunkFilename: "chunk-[id].js",
            library: 'myApp'
          },
          // これだとエラーになる!(webpack.optimizeがundefined) 
          // plugins: [ new webpack.optimize.CommonsChunkPlugin("commons.js") ], :smiley: 
          plugins: [ new CommonsChunkPlugin("commons.js") ], 
      .pipe(gulp.dest('./dist/js/'));
});
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2