Webpack 2 - 抑制块文件

时间:2017-07-25 18:58:01

标签: javascript webpack webpack-2 html-webpack-plugin commonschunkplugin

由于我们将此输出放在负载均衡器上(不使用粘性),我们需要将这些文件的输出放在没有块(没有哈希)的情况下。

这些是webpack配置的两个主要文件。

webpack.common.js

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const helpers = require('./helpers');

const STATIC_TRANSLATION_MAP = require('../TranslationMap.json');

module.exports = {
    entry: {
        app: ['./src/public/main.ts'],
        vendor: './src/public/vendor.ts',
        polyfills: './src/public/polyfills.ts'
    },
    output: {
        path: helpers.root('dist/public')
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                loaders: ['awesome-typescript-loader', 'angular2-template-loader', 'angular2-router-loader']
            },
            {
                test: /\.html$/,
                loader: 'html-loader?-minimize'
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'file-loader?name=assets/[name].[ext]'
            },
                        {
                test: /\.styl$/,
                include: helpers.root('src', 'public', 'app'),
                use: [
                    'raw-loader',
                    'stylus-loader'
                ]
            },
            {
                test: /\.styl$/,
                include: helpers.root('src', 'public'),
                exclude: helpers.root('src', 'public', 'app'),
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [
                        'css-loader',
                        'stylus-loader'
                    ]
                })
            },
            {
                test: /\.css$/,
                include: helpers.root('src', 'public', 'assets', 'css'),
                loader: 'raw-loader'
            },
            {
                test: /\.xlf$/,
                loader: 'raw-loader'
            }
        ]
    },
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.js'],
        alias: {}
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: ['app', 'vendor', 'polyfills']
        }),
        new HtmlWebpackPlugin({
            template: 'src/public/index.html'
        }),
        new webpack.DefinePlugin({
            'process.env': {
                'LOCALE_LIST': JSON.stringify(Object.keys(STATIC_TRANSLATION_MAP))
            }
        })
    ]
};

webpack.prod.js

const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const commonConfig = require('./webpack.common.js');
const helpers = require('./helpers');

const prodEnv = 'production';

module.exports = webpackMerge(commonConfig, {
    devtool: 'source-map',
    output: {
        filename: '[name].js',
        chunkFilename: '[name].js'
    },
    plugins: [
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.optimize.UglifyJsPlugin({
            mangle: {
                keep_fnames: true
            }
        }),
        new webpack.LoaderOptionsPlugin({
            htmlLoader: {
                minimize: false
            }
        }),
        new ExtractTextPlugin('[name].css'),
        new webpack.DefinePlugin({
            'process.env': {
                'ENV': JSON.stringify(prodEnv)
            }
        })
    ]
});

但令我惊讶的是,我注意到webpack正在生成额外的文件。你能看到那些有数字的吗? (从0到19)。我不确定他们来自哪里,其中的每个内容都以webpackJsonp开头。

有没有办法禁用此块功能,只生成entry中的三个文件?

enter image description here

1 个答案:

答案 0 :(得分:1)

发生了什么事?

  

但令我惊讶的是,我注意到webpack正在生成额外的文件。你能看到那些有数字的吗? (从0到19)

output.chunkFilename

此选项确定non-entry块文件的名称。 默认情况下使用[id].js或从output.filename推断的值(名称替换为id):

非入境块(外部)

./dist/[0].js
./dist/[1].js
./dist/[2].js
./dist/[3].js
...

来自webpack.config.entry

的条目块
./dist/app.js
./dist/vendor.js
...

如何解决?

  

目前,CommonChunkPlugin只接收从入口块导入的模块。

webpack/issues/4392

解决方法/黑客攻击

此概念可用于获取隐式公共供应商块:

new webpack.optimize.CommonsChunkPlugin({
  name: "vendor",
  minChunks: function (module) {
    // this assumes your vendor imports exist in the node_modules directory
    return module.context && module.context.indexOf("node_modules") !== -1;
  }
})

Passing the minChunks property a function

webpack/issues/2855#issuecomment-239606760

https://stackoverflow.com/a/39401288/6836839