module.exports = {
...
mode:'none'//production:不生成source map
devtool:'source-map'//eval,source-map,inline-source-map
}
複製代碼
將react、react-dom基礎包經過cdn(內容分發網絡)引入,不打入bundle中html
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin')
module.exports = {
plugins:[
new HtmlWebpackExternalsPlugin({
externals:[
{
module:'react',
entry:'https://unpkg.com/react@16/umd/react.production.min.js',
global:'React'
},
{
module:'react-dom',
entry:'https://unpkg.com/react-dom@16/umd/react-dom.production.min.js',
global:'ReactDOM'
}
]
})
]
}
複製代碼
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
複製代碼
能夠查看生成的js文件大小小不少node
webpack 默認會自動拆分代碼塊,條件:react
module.exports = {
...
optimiztion: {
splitChunks: {
chunks:'async',//用來選擇分割哪些代碼塊,可選值有:'all'(全部代碼塊),'async'(異步代碼),'initial'(同步代碼)
minSize: 30000,//模塊的最小體積
maxSize: 0,
minChunks: 1,//模塊的最小被引用的次數
maxAsyncRequests:5,//按需加載的最大並行請求數
maxInitialRequests:3,//一個入口最大並行請求數
automaticNameDelimiter:'~',//文件名的鏈接符
automaticNameMaxLength: 30,
name:true,
cacheGroups: {//緩存組
vendors:{
test:/[\\/]node_modules[\\/]/,
priority: -10
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk:true
}
}
}
}
}
複製代碼
module.exports = {
//...
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /(react|react-dom)/,
name: 'vendors',
chunks: 'all'
}
}
}
},
}
複製代碼
chunks:['vendors',name]
module.exports = {
//分離頁面公共文件
optimization: {
splitChunks: {
minSize:0,//引用模塊大小 0:只要有引用符合要求就提取到commons文件中
cacheGroups: {
commons: {
name: 'commons',
chunks: 'all',
minChunks: 2//最小引用次數爲2次
}
}
}
},
}
複製代碼
沒用的代碼,構建時將刪除掉(必須是es6的寫法,commonjs不支持require不支持)webpack
一、代碼不會被執行,不可到達,如:es6
if(false){
console.log('將不會被執行')
}
複製代碼
二、代碼執行結果不會被用到web
function aa(){
return '結果沒有別賦值給變量'
}
複製代碼
三、代碼只會影響死變量(只寫不讀)npm