webpack额外研究

187 阅读1分钟

代码分割加设置环境变量

`

const path=require('path');
const HtmlWebpackPlugin=require('html-webpack-plugin');

const webpack = require('webpack');

module.exports=env=>{

console.log('NODE_ENV: ', env.NODE_ENV) // 'local'
console.log('Production: ', env.production) // true
return {

    entry:{
        index:'./main.js',
        another:'./another.js'
    },
    output:{
        filename:'[name].bundle.js',
        path:path.resolve('./dist')
    },
    
    plugins:[
        new HtmlWebpackPlugin({
            title: '33'
        }),
        new webpack.optimize.CommonsChunkPlugin({
                name: 'common' // Specify the common bundle's name.
            })
    ]
}

} `

在package.json里 `

"build": "webpack --env.NODE_ENV=local --env.production --progress"

` 那么可以打包2个js文件并生成两个script标签,自动插入到html页面里