webpack和vue3优化方案

https://www.jianshu.com/p/adc33ac846f9

https://www.cnblogs.com/ajaemp/p/12120402.html

https://blog.csdn.net/lileLife/article/details/103517037

vue.config.js下面两个配置是等同的


module.exports = {
  configureWebpack: {
      
  }
}
// OR
module.exports = {
  configureWebpack: config => {
    
  }
}

一个很好的引用百度地图的案例

https://www.dazhuanlan.com/2019/09/26/5d8c39c95395c/

 

uglify不支持es6,所以用terser

cnpm install terser

在vue.config.js里设置

const TerserPlugin = require('terser-webpack-plugin');

    configureWebpack: {
        optimization: {
            minimizer: [
                new TerserPlugin({
                    terserOptions: {
                        ecma: undefined,
                        warnings: false,
                        parse: {},
                        compress: {
                            drop_console: true,
                            drop_debugger: false,
                            pure_funcs: ['console.log'] // 移除console
                        }
                    },
                }),
            ]
        },
        resolve: {
            extensions: ['.js', '.json', '.vue', '.scss', '.html'],
        },
    },

 

去除prefetch等

 chainWebpack: config => {
        config
            .entry('index')
            .add('babel-polyfill')
            .end();
        // 配置别名
        config.resolve.alias
            .set("@", resolve("src"))
            .set("@img", resolve("src/assets/images"))
            .set("@css", resolve("src/assets/styles/css"))
            .set("@scss", resolve("src/assets/styles/scss"));
        // 生产环境配置
        if (isProduction) {
            // 删除预加载
            config.plugins.delete('preload');
            config.plugins.delete('prefetch');
            // 压缩代码
            config.optimization.minimize(true);
            // 分割代码
            config.optimization.splitChunks({
                chunks: 'all'
            })
            // 生产环境注入cdn
            config.plugin('html')
                .tap(args => {
                    args[0].cdn = cdn;
                    return args;
                });
        }
    },

 

css优化

    css: {
        // 是否使用css分离插件 ExtractTextPlugin
        extract: true,
        // 开启 CSS source maps?
        sourceMap: false,
        // css预设器配置项
        loaderOptions: {
            // pass options to sass-loader
            sass: {
                // 引入全局变量样式
                data: `
                    @import "@/stylePath/theme.scss;
                `
            }
        },
        // 启用 CSS modules for all css / pre-processor files.
        modules: false,
    },

 

Use // eslint-disable-next-line to ignore the next line.解决办法


  devServer: {
        port: 9090,
        // https: true,
        //  allowedHosts: ['mungerfans.com'],
        //    open: true, // 自动开启浏览器
        // compress: true, // 开启压缩
       overlay: {
            warnings: false,
            errors: false
        },
        lintOnSave: false
    },

参考网站

https://www.jianshu.com/p/3043d474fb86

 

部署后载静态页面路径错误
需要在vue.config里添加
publicPath: './',
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值