Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split app and vendor code with Webpack

I'm starting a new project with react So I have this webpack.config.js file that's supposed to hundle 2 chunks , one for the app code and one for vendor based on this exemple from webpack documentation

webpack.config.js

const webpack = require('webpack');
module.exports = {
    entry: {
        app: './src/App.jsx',
        vendor: ['react', 'react-dom', 'whatwg-fetch']
    },
    output: {
        path: __dirname + './static/',
        filename: 'app.bundle.js'
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            filename: 'vendor.bundle.js'
        })
    ],
    module: {
        loaders: [{
            test: /.jsx$/,
            loader: 'babel-loader',
            query: {
                presets: ['react', 'es2015']
            }
        }, ]
    }
};

When I run webpack -p I can see that my two assets are supposed to be generated but there is nothing in the output directory.

Webpack output

           Asset    Size  Chunks             Chunk Names
   app.bundle.js    6 kB       0  [emitted]  app
vendor.bundle.js  108 kB       1  [emitted]  vendor
   [6] ./src/App.jsx 3.37 kB {0} [built]
   [7] ./src/IssueAdd.jsx 3.57 kB {0} [built]
   [9] ./src/IssueList.jsx 5.46 kB {0} [built]
  [10] ./src/IssueFilter.jsx 2.52 kB {0} [built]
  [20] multi react react-dom whatwg-fetch 52 bytes {1} [built]
    + 16 hidden modules

Webpack version is 3.8.1 with windows 7 dev environment. Am I missing something here?

like image 300
storm Avatar asked Nov 15 '17 14:11

storm


1 Answers

If you want two separate files to be emitted in output folder Firstly you have to make sure those files have new name every-time files are updated and one of the ways is to make use of caching [chunkhash] as suggested in webpack 3.8.1 Documentation

Include path on top of the file , so your output should be something like (You can change it accordingly):

 var path = require('path');

 output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name].[chunkhash].js'
  },

And inside your plugin you can just give name

plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor'
    }) 
]

You can read more about Webpack 3.8.1 output-filename

like image 63
Aaqib Avatar answered Nov 14 '22 15:11

Aaqib