請問webpack4怎么打包第三方類庫?
用webpack4
比如,index.js?里import了好幾個第三方類庫,比如"jquery"、"vue"、"bootstrap",
現在我只想把"jquery"和"vue"打包到?vendors.js里,請問怎么設置???
用webpack4
比如,index.js?里import了好幾個第三方類庫,比如"jquery"、"vue"、"bootstrap",
現在我只想把"jquery"和"vue"打包到?vendors.js里,請問怎么設置???
2018-06-22
舉報
2019-05-31
const?path?=?require('path'); const?VueLoaderPlugin?=?require('vue-loader/lib/plugin'); const?isDev?=?process.env.NODE_ENV?===?'development';?/*package.json里面的dev判斷*/ const?HTMLPlugin?=?require('html-webpack-plugin'); const?webpack?=?require('webpack'); //css分離打包?mini-css-extract-plugin??extract-text-webpack-plugin@next const?ExtractPlugin?=?require('extract-text-webpack-plugin'); const?config??=?{ //module.exports?=?{ ????target:?'web', ????entry:?path.join(__dirname,'./src/index.js'), ????output:?{ ????????//?filename:?"bundle.js", ????????filename:?"bundle.[hash:8].js",?/*正式環境與開發環境進行區分*/ ????????path:?path.join(__dirname,'dist'), ????}, ????module:?{ ????????rules:?[ ????????????{ ????????????????test:/\.vue$/,???/*使用vue-loader處理.vue文件*/ ????????????????loader:?'vue-loader' ????????????},/*{ ????????????????test:?/\.css$/, ????????????????use:['style-loader','css-loader'], ????????????},*/ ????????????{ ????????????????test:?/\.(jpg|jpeg|png|gif|svg)$/, ????????????????use:?[{ ????????????????????????loader:'url-loader', ????????????????????????options:?{ ????????????????????????????limit:1024, ????????????????????????????name:'[name]-picf.[ext]' ????????????????????????} ????????????????????}] ????????????},{ ????????????????test:?/\.jsx$/, ????????????????loader:?'babel-loader' ????????????} ????????] ????}, ????/*錯誤提示:asset(s)?最大限制在244K。解決:修改webpack?的性能提示配置?*/ ????performance:?{ ????????hints:'warning',???/*或者直接關閉hints:false*/ ????????//入口起點的最大體積 ????????maxEntrypointSize:?50000000, ????????//生成文件的最大體積 ????????maxAssetSize:?30000000, ????????//只給出?js?文件的性能提示 ????????assetFilter:?function(assetFilename)?{ ????????????return?assetFilename.endsWith('.js'); ????????} ????}, ????plugins:[ ????????new?VueLoaderPlugin(),?/*Vue-loader在15.*之后的版本的使用都是需要帶有VueLoaderPlugin的,還需另外單獨配置css-loader*/ ????????new?webpack.DefinePlugin({ ????????????'process.env':{ ????????????????NODE_ENV?:?isDev?'"development"':'"production"' ????????????} ????????}), ????????/*如果'"development"'不加引號,那么最后解析會變成process.env.NODE_ENV=development,沒有development這個變量?缺少引號*/ ????????new?HTMLPlugin(), ????], }; if(isDev){ ????config.devtool?=?'#cheap-module-eval-source-map';/*在瀏覽器上頁面可以調試,不加的話是vue,es6等的編譯代碼*/ ????config.devServer?=?{ ????????port:8001, ????????host:'localhost',/*既可以通過localhost:8000訪問到,又可以通過127.0.0.1:8000,別人也可以通過自己的ip訪問到*/ ????????overlay:{ ????????????errors:true?/*有任何錯誤都顯示到網頁*/ ????????}, ????????hot:true?/*原來修改東西?頁面自動刷新??現在不刷新,只更新內容*/ ????????/*historyFallback:{?//單頁應用的映射?}*/ ???????//?open:true,/*修改文件后默認打開瀏覽器*/ ????}; ????config.plugins.push( ????????new?webpack.HotModuleReplacementPlugin(), ????????new?webpack.NoEmitOnErrorsPlugin(), ????); ????config.module.rules.push({ ????????test:?/\.styl/,??/*注意這里沒有$結束符,這樣既可以匹配styl文件,又可以匹配stylus*/ ????????use:?[ ????????????'style-loader', ????????????'css-loader', ????????????{ ????????????????loader:?"postcss-loader", ????????????????options:?{ ????????????????????sourceMap:true, ????????????????} ????????????}, ????????????'stylus-loader' ????????] ????}) }else{ ????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.[chunkHash:8].css'), ????); ????config.optimization?=?{ ????????splitChunks:?{ ????????????cacheGroups:?{ ????????????????commons:?{ ????????????????????chunks:?'initial', ????????????????????minChunks:?2,?maxInitialRequests:?5, ????????????????????minSize:?0 ????????????????}, ????????????????vendor:?{ ????????????????????test:?/node_modules/, ????????????????????chunks:?'initial', ????????????????????name:?'vendor', ????????????????????priority:?10, ????????????????????enforce:?true ????????????????} ????????????} ????????}, ????????runtimeChunk:?true ????} } module.exports?=?config;2019-01-05
https://github.com/Evenyoo/vueInit
2018-06-22
找到解決方法了
https://github.com/shystruk/create-react-redux-app-structure/blob/master/webpack.config.js