简体   繁体   中英

webpack production setup - vendors output js always has different chunk hash even though nothing's changed

The follow production webpack.config file mostly works (builds, runs ok), but one nagging problem. I set up vendor and app js outputs precisely to separate vendor code (since those don't change very often). However, with the .config setup, even if there is no change, I always get a different chunk hash for vendor and app.js. I must be setting this wrong somehow but I have no idea what it could be. Please help!

var path = require('path');
var webpack = require('webpack');
var node_modules_dir = path.resolve(__dirname, 'node_modules');
var AssetsPlugin = require('assets-webpack-plugin');
var ChunkManifestPlugin = require('chunk-manifest-webpack-plugin');
var app_dir = path.join(__dirname, 'app');

var config = {
    entry: {
      vendors: ['react', 'react-dom', 'moment'],
      app: path.resolve(__dirname, 'app/Main.js')
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].[chunkhash].js',
        chunkFilename: '[name].[chunkhash].js'
    },
    module: {
        loaders: [{
              test: /\.jsx?$/, // A regexp to test the require path. accepts either js or jsx
              exclude: [node_modules_dir],
              loader: 'babel', // The module to load. "babel" is short for "babel-loader"
            },
            // LESS
            {
              test: /\.less$/,
              loader: 'style!css!less'
            },
            {
              test: /\.css$/, // Only .css files
              loader: 'style!css' // Run both loaders
            },
            {
              test: /\.(png|jpg)$/,
              loader: 'url?limit=25000'
            },
            { test: /\.gif$/, loader: "url-loader?mimetype=image/png" },
            { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&minetype=application/font-woff" },
            { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader?name=[name].[ext]" }
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: "vendors",
            minChunks: Infinity
        }),
        new AssetsPlugin({
            filename: 'webpack-assets.js',
            path: path.resolve(__dirname, 'dist'),
            processOutput: function (assets) {
                return 'window.staticMap = ' + JSON.stringify(assets);
            }
        }),
        new ChunkManifestPlugin({
            filename: "chunk-manifest.json",
            manifestVariable: "webpackManifest"
        }),
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin(),
        new webpack.DefinePlugin({
          'process.env': {
            'NODE_ENV': '"production"'
          },
          '__appSettings': {
              'apiGatewayDomain': '"http://localhost:8088"',
              'enableDevTools': false,
              'enableReduxLogger': false
          },
            __version: JSON.stringify(require(__dirname +"/package.json").version)
        }),
        new webpack.ProvidePlugin({
            'fetch': 'imports?this=>global!exports?global.fetch!isomorphic-fetch',
            'configureApplicationStore': 'exports?configureStore!' +  app_dir + '/global/redux/ConfigureStore.prod.js'
          })
    ]
};

module.exports = config;

If there are other glaring issues with this setup, please let me know as well. Thanks!

use the "key sort" to fixed vendor. you entry just like this:

entry: {
  a_vendors: ['react', 'react-dom', 'moment'],
  z_app: path.resolve(__dirname, 'app/Main.js')
},

the vendor will fixed.

more detail:

https://github.com/webpack/webpack/issues/1315#issuecomment-247269598 https://github.com/webpack/webpack/pull/2998

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM