Newer
Older
smartwell_front_dz / vue.config.js
wangxitong on 11 Sep 2023 6 KB first commit
'use strict'
const path = require('path')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
const defaultSettings = require('./public/config/project.config')

function resolve(dir) {
  return path.join(__dirname, dir)
}
const name = defaultSettings.title || '管网运行监测系统' // page title
const port = process.env.port || process.env.npm_config_port || 9527 // dev port

module.exports = {
  publicPath: './',
  // publicPath: process.env.NODE_ENV === 'production'
  //   ? './'
  //   : './',
  assetsDir: 'static',
  outputDir: 'dist',
  lintOnSave: process.env.NODE_ENV === 'development', // 是否开启eslint
  productionSourceMap: false, // 不需要生产环境的 source map
  filenameHashing: true, // 文件名哈希
  // 它支持webPack-dev-server的所有选项
  devServer: {
    port: port, // 端口号
    open: true // 配置自动启动浏览器
    // before: require('./mock/mock-server.js')
  },
  configureWebpack: (config) => {
    let plugins = []
    const cesiumSourcePath = 'node_modules/mars3d-cesium/Build/Cesium/' // cesium库安装目录
    const cesiumRunPath = './mars3d-cesium/' // cesium运行时路径
    plugins = [
      // 标识cesium资源所在的主目录,cesium内部资源加载、多线程等处理时需要用到
      new webpack.DefinePlugin({
        CESIUM_BASE_URL: JSON.stringify(path.join(config.output.publicPath, cesiumRunPath))
      }),
      // Cesium相关资源目录需要拷贝到系统目录下面(部分CopyWebpackPlugin版本的语法可能没有patterns)
      new CopyWebpackPlugin({
        patterns: [
          { from: path.join(cesiumSourcePath, 'Workers'), to: path.join(config.output.path, cesiumRunPath, 'Workers') },
          { from: path.join(cesiumSourcePath, 'Assets'), to: path.join(config.output.path, cesiumRunPath, 'Assets') },
          { from: path.join(cesiumSourcePath, 'ThirdParty'), to: path.join(config.output.path, cesiumRunPath, 'ThirdParty') },
          { from: path.join(cesiumSourcePath, 'Widgets'), to: path.join(config.output.path, cesiumRunPath, 'Widgets') }
        ]
      })
    ]
    plugins.push(new NodePolyfillPlugin())
    return {
      name: name,
      resolve: {
        alias: {
          '@': resolve('src')
        }
      },
      externals: {
        'AMap': 'AMap'
        // 'mars3d-cesium': 'Cesium'
      }, // 排除使用 mars3d-cesium
      module: { unknownContextCritical: false }, // 配置加载的模块类型,cesium时必须配置
      plugins: plugins
    }
  },
  chainWebpack: config => {
    // 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.module
      .rule('images')
      .set('parser', {
        dataUrlCondition: {
          maxSize: 6 * 1024 // 超过6KB图片不内联
        }
      })
      // .include.add(resolve('src/assets/login_images')).end()
      // .use('url-loader')
      // .loader('url-loader')
      // .options({ name: 'img/[name].[hash:8].[ext]', limit: 1 })
      .end()
    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          // runtime 代码由于只是驱动不同路由页面的关系,代码量比较少,请求 js 的时间都大于执行时间了,
          // 所以使用 script-ext-html-webpack-plugin 插件将其内链在 index.html 中比较友好。

          // config
          //   .plugin('ScriptExtHtmlWebpackPlugin')
          //   .after('html')
          //   .use('script-ext-html-webpack-plugin', [{
          //     // `runtime` must same as runtimeChunk name. default is `runtime`
          //     inline: /runtime\..*\.js$/
          //   }])
          //   .end()

          // 如果使用了某些长期不会改变的库,像 element-ui ,打包完成有 600 多 KB ,
          // 包含在默认 vendor 中显然不合适,每次用户都要加载这么大的文件体验不好,所以要单独打包:
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  minSize: 30000, // 30K,大小超过30kb的模块才会被提取
                  maxSize: 60000, // 只是提示,可以被违反,会尽量将chunk分的比maxSize小,当设为0代表能分则分,分不了不会强制
                  minChunks: 1, // 某个模块至少被多少代码块引用,才会被提取成新的chunk
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                mars3dCesium: {
                  name: 'chunk-mars3d', // split elementUI into a single package
                  priority: 30, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?mars3d-cesium(.*)/ // in order to adapt to cnpm
                },
                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')
        }
      )
  }
}