美文网首页
webpack区分打包类库代码及hash优化

webpack区分打包类库代码及hash优化

作者: kayleeWei | 来源:发表于2018-06-19 11:00 被阅读0次
    • config.output.filename这一项必须使用chunkhash(app和vendor打包后的文件名的hash值不同)
    const path = require('path')
    const HTMLPlugin = require('html-webpack-plugin')
    const webpack = require('webpack')
    const ExtractPlugin = require('extract-text-webpack-plugin')
    
    // 判断是否为dev开发模式
    const isDev = process.env.NODE_ENV === 'development'
    
    const config = {
        target: 'web',
        entry: path.join(__dirname, 'src/index.js'),
        output: {
            filename: 'bundle.[hash:8].js',
            path:path.join(__dirname, 'dist')
        },
        module: {
            rules: [
                {
                    test: /\.vue$/,
                    loader: 'vue-loader'
                },
                {
                    test: /\.jsx$/,
                    loader: 'babel-loader'
                },
                {
                    test: /\.(gif|jpg|jpeg|png|svg)$/,
                    use: [
                        {
                            loader: 'url-loader',
                            options: {
                                limit: 1024,
                                name: '[name].[ext]'
                            }
                        }
                    ]
                }
            ]
        },
        plugins: [
            new webpack.DefinePlugin({
                'process.env': {
                    NODE_ENV: isDev ? '"development"' : '"production"'
                }
            }),
            new HTMLPlugin()
        ]
    }
    
    if (isDev) {
        config.module.rules.push({
            test: /\.styl/,
            use: [
                'style-loader',
                'css-loader',
                {
                    loader: 'postcss-loader',
                    options: {
                        sourceMap: true
                    }
                },
                'stylus-loader'
            ]
        })
        config.devtool = '#cheap-module-eval-source-map'
        config.devServer = {
            port: 8000,
            host: '0.0.0.0',
            overlay: {
                errors: true
            },
            hot: true
        },
        config.plugins.push(
            new webpack.HotModuleReplacementPlugin(),
            new webpack.NoEmitOnErrorsPlugin()
        )
    } else {
    // 将vue分开打包
        config.entry = {
            app: path.join(__dirname, 'src/index.js'),
            vendor: ['vue']
        }
        config.output.filename = '[name].[chunkhash:8].js'
        config.module.rules.push({
            test: /\.styl/,
            use: ExtractPlugin.extract({
                fallback: 'style-loader',
                use: [
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            sourceMap: true
                        }
                    },
                    'stylus-loader'
                ]
            })
        })
        config.plugins.push(
            new ExtractPlugin('styles.[contentHash:8].css'),
            new webpack.optimize.CommonsChunkPlugin({
                name: 'vendor'
            }),
            // 单独打包webpack的文件
            new webpack.optimize.CommonsChunkPlugin({
                name: 'runtime'
            })
        )
    }
    
    module.exports = config
    

    相关文章

      网友评论

          本文标题:webpack区分打包类库代码及hash优化

          本文链接:https://www.haomeiwen.com/subject/jlaoeftx.html