npm install uglifyjs-webpack-plugin@1.1.1 --save-dev
执行命名安装插件css
D:\zhangyugen@jd.com\vue\day1\html\4.从0开始学VUE\simpleplugin>npm install uglifyjs-webpack-plugin@1.1.1 --save-dev npm WARN deprecated uglify-es@3.3.9: support for ECMAScript is superseded by `uglify-js` as of v3.13.0 > uglifyjs-webpack-plugin@0.4.6 postinstall D:\zhangyugen@jd.com\vue\day1\html\4.从0开始学VUE\simpleplugin\node_modules\webpack\node_modules\uglifyjs-webpack-plugin > node lib/post_install.js npm WARN css-loader@3.6.0 requires a peer of webpack@^4.0.0 || ^5.0.0 but none is installed. You must install peer dependencies yourself. npm WARN style-loader@2.0.0 requires a peer of webpack@^4.0.0 || ^5.0.0 but none is installed. You must install peer dependencies yourself. npm WARN simpleconfig@1.0.0 No description npm WARN simpleconfig@1.0.0 No repository field. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\watchpack-chokidar2\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) + uglifyjs-webpack-plugin@1.1.1 added 42 packages from 59 contributors, updated 1 package, moved 2 packages and audited 624 packages in 17.999s 39 packages are looking for funding run `npm fund` for details found 11 vulnerabilities (2 low, 9 moderate) run `npm audit fix` to fix them, or `npm audit` for details D:\zhangyugen@jd.com\vue\day1\html\4.从0开始学VUE\simpleplugin>
安装成功,修改webpack.config.jshtml
// 须要从node依赖中引入 须要添加依赖环境 const path = require('path'); // 导入webpack内置插件 const webpack = require('webpack') // 导入HtmlWebpackPlugin插件 const HtmlWebpackPlugin = require('html-webpack-plugin') // 导入JS压缩插件 const uglifyjsWebpackPlugin = require('uglifyjs-webpack-plugin') module.exports = { // 配置源码打包位置 entry: './src/main.js', // 配置目标位置 output: { // path 只能写绝对路径 不能写相对路径 可是不要直接写死,须要动态获取文件位置 path: path.resolve(__dirname,'dist'), filename: 'bundle.js' }, module: { rules: [ { test: /\.css$/, use: [ 'style-loader', 'css-loader' ] }, { test: /\.js$/, exclude: /(node_modules|bower_components)/, use: { loader: 'babel-loader', options: { presets: ['es2015'] } } }, // 增长.vue文件的loader { test: /\.vue$/, use:['vue-loader'] } ] }, // 使用runtime-compiler resolve:{ alias:{ 'vue$': 'vue/dist/vue.esm.js' } }, // 插件 plugins:[ // 版权插件 new webpack.BannerPlugin('最终版权归彼岸舞全部!'), // index.html打包插件 new HtmlWebpackPlugin({ // 指定模板生成 否则没有id="app"的div 同时删除调用index.html中的 <script>应为会自动添加,因此不须要写 template: 'index.html' }), // JS压缩插件 new uglifyjsWebpackPlugin() ] }
执行打包vue
能够看到JS已经被压缩了,可是存在一个问题,那就是版权声明没有了,还有注释都没有了,应为这就是压缩的一部分,这个和版权插件是冲突的node