Webpack 2-禁止块文件 [英] Webpack 2 - Suppress chunk files

查看:274
本文介绍了Webpack 2-禁止块文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们将此输出放置在负载均衡器上(不使用粘性)时,我们需要将这些文件的输出放置在没有块的位置(两个散列都没有).

这是用于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生成三个文件?

解决方案

发生了什么事?

但是令我惊讶的是,我注意到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;
  }
})

传递minChunks属性功能

webpack/issues/2855#issuecomment-239606760

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

As we are putting this output on a load balancer (not using sticky), we need to place the output of these files without chunks (neither hashes).

These are the main two files for webpack configuration.

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)
            }
        })
    ]
});

But for my surprise, I noticed webpack is producing extra files. Can you see those ones with numbers? (From 0 to 19). I'm not sure from where they're coming from, and every content of those starts with webpackJsonp.

Is there a way to disable this chunk feature and just produce the three files from entry?

解决方案

What is happening?

But for my surprise, I noticed webpack is producing extra files. Can you see those ones with numbers? (From 0 to 19)

output.chunkFilename

This option determines the name of non-entry chunk files. By default [id].js is used or a value inferred from output.filename (name is replaced with id):

Non-entry chunks (external)

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

Entry-chunks from webpack.config.entry

./dist/app.js
./dist/vendor.js
...

How to fix it?

Currently CommonChunkPlugin only receives modules imported from entry chunks.

webpack/issues/4392

Workarounds / hacks

This concept may be used to obtain implicit common vendor chunks:

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

这篇关于Webpack 2-禁止块文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆