配置

# 配置

# env配置

配置环境变量,这里主要是配置环境变量,以及一些插件的配置,比如插件名称,接口地址等。

# dev环境

开发的时候,默认填写插件的名称和平台ID,打包后,会自动替换成对应的插件名称和平台ID。

ENV = 'development'                         # 环境
VUE_APP_ADDON_NAME = 'gs_xhd'               # 插件名称
VUE_APP_PLAID = 16                          # 平台id
VUE_APP_BASE_API = 'https://www.xxx.com/'   # 接口地址
1
2
3
4

# prod环境

ENV = 'production'                  # 环境
VUE_APP_ADDON_NAME = 'gs_xhd'       # 插件名称
VUE_APP_BASE_API = ''               # 接口地址
1
2
3

# eslint配置

eslint 这个配置是为了解决在开发过程中,有些代码不符合规范,尽可能按照规范来写代码,这样可以避免一些不必要的错误。

# vue.config.js配置

配置中将cdn引入,默认加载怪兽系统目录的资源,以及externals配置,这样可以在开发环境中使用cdn引入,减少打包体积,减少打包时间,同时也可以在生产环境中使用externals配置,将一些库不打包进去,而是在html中引入,这样可以减少打包体积,同时也可以减少打包时间。

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

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

const name = defaultSettings.title || '加载中...' // page title
const IS_DEV = process.env.NODE_ENV === 'development'
const cdn = {
  css: [
    '/assets/lib/element-ui/index.css'
  ],
  js: [
    '/assets/js/lib/axios/axios.min.js',
    '/assets/js/lib/vue/2.7.10/vue.min.js',
    '/assets/js/lib/vue-router/vue-router.min.js',
    '/assets/js/lib/echarts/echarts.min.js',
    '/assets/js/lib/vuex/vuex.min.js',
    '/assets/lib/element-ui/index.js',
    '/assets/lib/addon/dkgs-sdk.min.js'
  ]
}
const externals = {
  // 属性名称 vue, 表示遇到 import xxx from 'vue' 这类引入 'vue'的,不去 node_modules 中找,而是去找全局变量 Vue(其他的为VueRouter、Vuex、axios、ELEMENT、echarts,注意全局变量是一个确定的值,不能修改为其他值,修改为其他大小写或者其他值会报错,若有异议可留言)
  vue: 'Vue',
  'vue-router': 'VueRouter',
  vuex: 'Vuex',
  axios: 'axios',
  'element-ui': 'ELEMENT',
  'echarts': 'echarts',
  '@form-create/element-ui': 'formCreate',
  'dkgs-sdk': 'dkgsSdk'
}

// 默认端口9528
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

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: process.env.NODE_ENV === 'development' ? '/' : '/assets/addons/' + process.env.VUE_APP_ADDON_NAME + '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    }
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    output: { // 输出重构  打包编译后的 文件名称  【模块名称.版本号.时间戳】
      filename: `static/js/[name].dkyx.js`,
      chunkFilename: `static/js/[name].dkyx.js`
    },
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  css: {
    extract: {
      filename: `static/css/[name].dkyx.css`,
      chunkFilename: `static/css/[name].dkyx.css`
    }
  },
  chainWebpack(config) {
    if (!IS_DEV) {
      config.plugin('html').tap(args => {
        args[0].cdn = cdn
        return args
      })
      // 视为一个外部库,而不将它打包进来
      config.externals(externals)
    }
    // 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
                },
                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
                },
                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')
        }
      )
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162