记录一次vue2打包配置,cdn引入,gzip压缩

文章详细配置了一个Vue项目的Webpack设置,包括使用compression-webpack-plugin进行gzip压缩以及uglifyjs-webpack-plugin进行代码混淆优化。在生产环境中,还设置了cdn引用以提升加载速度,并提供了详细的环境变量判断和外部库的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用到的插件
compression-webpack-plugin
uglifyjs-webpack-plugin

// vue.config.js
'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const CompressionWebpackPlugin = require('compression-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const name = defaultSettings.title || 'vue Admin Template' // page title
// gzip压缩文件配置
const productionGzipExtensions = ['js','css'];
// 判断是否是生产环境
const isProduction = process.env.NODE_ENV !== "development";
// 本地环境是否需要使用cdn
const devNeedCdn = false;
// cdn 配置
const cdn = {
  externals: {
    'vue': 'Vue',
    'vuex': 'Vuex',
    'vue-router': 'VueRouter',
    'axios': 'axios',
    "moment/moment": "moment",
    'element-ui': 'ELEMENT',
    "echarts": "echarts",
    "@wangeditor/editor": "wangEditor",
  },
  css: [
    'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.12/theme-chalk/index.min.css',
    'https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css',
  ],
  js: [
    'https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.min.js',
    'https://cdn.bootcdn.net/ajax/libs/vuex/3.1.0/vuex.min.js',
    'https://cdn.bootcdn.net/ajax/libs/vue-router/3.0.6/vue-router.min.js',
    'https://cdn.bootcdn.net/ajax/libs/axios/0.18.1/axios.min.js',
    'https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.4/moment.min.js',
    'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.12/locale/zh-CN.min.js',

    'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.10/index.min.js',
    'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.10/locale/zh-CN.min.js',

    'https://cdn.jsdelivr.net/npm/echarts@4.9.0/dist/echarts.min.js',
    'https://unpkg.com/@wangeditor/editor@latest/dist/index.js',
  ]
};

// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following methods:
// port = 9528 npm run dev OR npm run dev --port = 9528
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  // lintOnSave: process.env.NODE_ENV === 'development',
  lintOnSave: false,
  productionSourceMap: false,
  configureWebpack(config) {
    config.name = name;
    config.resolve.alias = {
      '@': resolve('src')
    };
    // 若是生产环境,则执行以下代码
    if (isProduction) {
      config.externals = cdn.externals;

      config.plugins.push(
        new CompressionWebpackPlugin({
          algorithm: 'gzip',
          test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
          threshold: 10240, // 只有大小大于该值的资源会被处理 10240
          minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
          // deleteOriginalAssets: false, // 删除原文件
        })
      );
      config.plugins.push(
        new UglifyJsPlugin({
            uglifyOptions: {
              //生产环境自动删除console
              compress: {
                  drop_debugger: true,
                  drop_console: true,
                  pure_funcs: ['console.log']
              }
            },
            sourceMap: false,
            parallel: true
        })
      );
    }
  },
  chainWebpack(config) {
    // ============注入cdn start============
    if (isProduction || devNeedCdn) {
      config.plugin('html').tap((arg) => {
        arg[0].cdn = cdn
        return arg
      });
    }
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single')
        }
      )
  }
}

<% for (var i in htmlWebpackPlugin.options.cdn&&htmlWebpackPlugin.options.cdn.css) { %>
    <link href="<%= htmlWebpackPlugin.options.cdn.css[i] %>" rel="preload" as="style" />
<% } %>
 <% for (var i in htmlWebpackPlugin.options.cdn&&htmlWebpackPlugin.options.cdn.js) { %>
  	<script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"></script>
 <% } %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值