Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack优化 #147

Open
specialgirlgotoheaven opened this issue Apr 4, 2019 · 0 comments
Open

Webpack优化 #147

specialgirlgotoheaven opened this issue Apr 4, 2019 · 0 comments

Comments

@specialgirlgotoheaven
Copy link
Owner

Webpack优化

  1. webpack module属性下面加上noParse字段,
    不去解析类型jQuery这样的包。例:noParse:/jquery/
  2. webpack module属性的rules属性下面加上exclude字段,排除
    exclude:/node_modules/
  3. webpack plugin属性下面的new webpack.IgnorePlugin(/./local/,/moment/)

4.dllPlugin 动态链接库
output:{
library:’ab’,
libraryTarget:’commonjs’/’umd’/’var’
}

output:{
output:{
filename:’dll[name].js’,
path:path.reslove(__dirname,’dist’),
library:’dll[name]’
}
},
plugins:[
new webpack.DllPlugin({ //name = library
name:’dll[name]’,
path:path.resolve(__dirname,’dist’,’manifest.json’)
})
]

Html :<script src=’/_dll_react.js’></script>
plugins:[
new webpack.DllReferencePlugin({
manifest:path.resolve(__dirname,’dist’,’manifest.json’)
})
]

  1. happypack 多线程打包
    module:{
    rules:[{
    test:/.js$/,
    exclude:/node_modules/,
    include:path.resolve(‘src’),
    use:’happypack/loader?id=js’
    },{
    test:/.css$/,
    use:’Happypack/loader?id=css’
    }
    ]

}
plugins:[
new Happypack({
id:’js’,
use:[{
loader:’babel-loader’,
options:{
presets:[
‘@babel/preset-env’,
‘@babel/preset-react’
]
}
}]
}),
new Happypack({
id:’css’,
use:[‘style-loader’,’css-loader’]
})
]

Webpack自带优化
1.//import 在生产环境下 会自动去掉没有用的代码 tree-shaking
//require语法不支持tree-shaking 所以要用import
2.//scope hosting 作用域提升

Webpack插件
Compiler对象 每个webpack只有一个
Compilation 每次编译会产生,代表当前编译时的对象,编译一次就会产生一次
(包含entries,modules,chunks,assets,template(mainTemplate,chunkTemplate,hotupdateTemplate,runtimeTemplate,moduleTemplate))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant