ChainWebpack (configuration)
config . optimization . minimizer(“terser”)。 Click ((args)= & gt;; {
? args[0]. terser options . compress . drop _ console = true
? Return parameter
})
}
By configuring vue-cli, some unnecessary packages will be excluded from the package file.
For example, let webpack unpack vue xlsx and element?
First find vue.config.js, and then add external files, as shown below:
When we run the package again, we will find that the package size has been greatly reduced: there are no more than three packages in the package target file.
? But we still need to use these packages, which can be accessed through CDN.
When developing the environment, file resources can still be taken out from the local node_modules, but external resources are only needed when the project is online. At this point we can use environmental variables to distinguish. Details are as follows:
In the vue.config.js file:
Let external = {}
Let cdn = {css: [], js: []}
Constisproduction = process.env.node _ env = =' production'//Judge whether it is a production environment.
if (isProduction) {
? External = {
? /**
? * external object attribute analysis:
? * "package name": "name introduced in the project"
*/
? vue': 'Vue ',
? Element -ui':' element',
? xlsx': 'XLSX '
? }
? cdn = {
css: [
? /element-ui/lib/theme-challenge/index.css'//element-uicss style sheet
],
js: [
? // vue must first!
? /vue@2.6. 12/dist/vue.js ',// vuejs
? /element-ui/lib/index.js ',// element-ui js
? /NPM/xlsx @ 0. 16.6/dist/xlsx . full . min . js ',// xlsx
]
? }
}
Then inject index.html through html-webpack-plugin:
ChainWebpack (configuration)
? config.plugin('preload ')。 Click (() =>[
{
? Rel:' preload',
? fileBlacklist: [/\。 map$/,/hot-update\。 js$/,/runtime\..*\.js$/],
? Include "initial"
}
? ])
? //Inject cdn variables (executed when packaging)
? config.plugin('html ')。 tap(args = & gt; {
Parameter [0]. Cdn = cdn // Configure CDN for the plug-in.
Return parameter
? })
? //Omit others ...
}
Find public/index.html and inject css and js in turn by configuring CDN Config.
Modify the contents of the title as follows:
& lt! -introduction style->;
? & lt% for (varcss of htmlwebpackplugin.options.cdn.css) {%>
& ltlink rel = " stylesheet " href = " & lt%=css% >& gt
& lt% } % & gt
& lt! -introduce JS-& gt;;
& lt% for (varjs of htmlwebpackplugin.options.cdn.js) {%>
? & ltscript src = " & lt%=js% >& gt& lt/script & gt;
& lt% } % & gt