vue.config.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * base config
  3. * author: houjiazong <houjiazong@gmail.com>
  4. * date: 2019/08/08
  5. */
  6. const path = require('path')
  7. const fs = require('fs')
  8. const child_process = require('child_process')
  9. const webpack = require('webpack')
  10. const AutoDllPlugin = require('autodll-webpack-plugin')
  11. const createThemeColorReplacerPlugin = require('./src/config/plugin.config')
  12. const PROXY_TIMEOUT = 1000 * 60 * 2
  13. function resolve (dir) {
  14. return path.join(__dirname, dir)
  15. }
  16. function fsExistsSync (path) {
  17. try {
  18. fs.accessSync(path, fs.F_OK)
  19. } catch (e) {
  20. return false
  21. }
  22. return true
  23. }
  24. function getModuleList () {
  25. return fs.readdirSync(resolve('./containers'))
  26. }
  27. const getBuildInfo = () => {
  28. const getDate = D => `${D.getFullYear() + '-' + (D.getMonth() + 1) + '-' + D.getDate() + ' ' + D.getHours() + ':' + D.getMinutes()}`
  29. const getCommitMSg = dirPath => {
  30. const commitDateObj = new Date(child_process.execSync(`cd ${dirPath} && git show -s --format=%cd`).toString())
  31. const commit = child_process.execSync(`cd ${dirPath} && git show -s --format=%H`).toString().trim()
  32. const commitUserName = child_process.execSync(`cd ${dirPath} && git show -s --format=%cn`).toString().trim()
  33. const commitDate = getDate(commitDateObj)
  34. let head = '-'
  35. const gitHEAD = path.join(dirPath, '.git/HEAD') // git 最后一次提交的 Head
  36. if (fsExistsSync(path.resolve(__dirname, gitHEAD))) {
  37. head = fs.readFileSync(path.resolve(__dirname, gitHEAD), 'utf-8').trim()
  38. }
  39. return {
  40. commit,
  41. commitUserName,
  42. commitDate,
  43. head,
  44. }
  45. }
  46. const nowDate = new Date()
  47. const buildDate = `${nowDate.getFullYear() + '-' + (nowDate.getMonth() + 1) + '-' + nowDate.getDate() + ' ' + nowDate.getHours() + ':' + nowDate.getMinutes()}`
  48. const containers = fs.readdirSync('./containers').filter(item => !/^\..*/.test(item)) // 忽略隐藏文件
  49. const gitBranch = child_process.execSync('git branch --show-current').toString()
  50. const info = {
  51. scope: getCommitMSg('./'),
  52. src: getCommitMSg('./src'),
  53. buildDate,
  54. branch: gitBranch,
  55. }
  56. containers.forEach(dir => {
  57. info[dir] = getCommitMSg(path.join('./containers', dir))
  58. })
  59. return { ...info, buildMachine: 'docker' }
  60. }
  61. const devServerCoustomConfig = fsExistsSync(resolve('./dev.server.config.js')) ? require('./dev.server.config.js') : {}
  62. module.exports = {
  63. lintOnSave: process.env.NODE_ENV !== 'production',
  64. configureWebpack: (config) => {
  65. config.plugins.push(createThemeColorReplacerPlugin())
  66. config.plugins.push(new webpack.DefinePlugin({
  67. themeColor: JSON.stringify(process.env.THEME_COLOR),
  68. theme: JSON.stringify(process.env.THEME),
  69. }))
  70. const aliasSrcDirConfig = {}
  71. const modules = getModuleList()
  72. modules.forEach(item => {
  73. aliasSrcDirConfig[`@${item}`] = resolve(`./containers/${item}`)
  74. })
  75. Object.assign(config, {
  76. resolve: {
  77. extensions: ['.js', '.vue', '.json'],
  78. alias: {
  79. '@': resolve('./src'),
  80. '~': resolve('./src'),
  81. '@@': resolve('.'),
  82. '~~': resolve('.'),
  83. '@scope': resolve('./scope'),
  84. '@containers': resolve('./containers'),
  85. ...aliasSrcDirConfig,
  86. },
  87. },
  88. })
  89. },
  90. chainWebpack: (config) => {
  91. const svgRule = config.module.rule('svg')
  92. svgRule.uses.clear()
  93. svgRule.include.add(resolve('./src/components/Icon'))
  94. svgRule.include.add(resolve('./scope/assets'))
  95. svgRule
  96. .test(/\.svg$/)
  97. .use('svg-sprite-loader')
  98. .loader('svg-sprite-loader')
  99. .options({
  100. symbolId: 'oc-[name]',
  101. })
  102. const imagesRule = config.module.rule('images')
  103. imagesRule.exclude.add(resolve('./src/components/Icon'))
  104. imagesRule.exclude.add(resolve('./scope/assets'))
  105. config.module.rule('images').test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
  106. config.plugin('define').tap((definitions) => {
  107. definitions[0]['process.env'].VUE_APP_BUILDINFO = JSON.stringify(getBuildInfo())
  108. definitions[0]['process.env'].THEME_COLOR = JSON.stringify(process.env.THEME_COLOR)
  109. definitions[0]['process.env'].THEME = JSON.stringify(process.env.THEME)
  110. definitions[0]['process.env'].BRAND = process.env.BRAND
  111. definitions[0]['process.env'].PRODUCT = process.env.PRODUCT
  112. return definitions
  113. })
  114. config
  115. .plugin('autodll')
  116. .use(new AutoDllPlugin({
  117. inject: true, // 设为 true 就把 DLL bundles 插到 index.html 里
  118. filename: '[name].[chunkhash].js',
  119. context: path.resolve(__dirname), // AutoDllPlugin 的 context 必须和 package.json 的同级目录,要不然会链接失败
  120. entry: {
  121. vendor: [
  122. 'vue',
  123. 'vue-router',
  124. 'axios',
  125. 'vuex',
  126. 'moment',
  127. 'vue-i18n',
  128. 'codemirror',
  129. 'vxe-table',
  130. 'ramda',
  131. 'lodash',
  132. 'marked',
  133. 'clipboard',
  134. 'crypto-js',
  135. 'echarts',
  136. 'v-charts',
  137. 'xterm',
  138. 'jsrsasign',
  139. 'ajv',
  140. 'socket.io-client',
  141. 'ant-design-vue',
  142. 'vue-grid-layout',
  143. ],
  144. },
  145. }))
  146. .end()
  147. if (process.env.analyzer_report) { // 是否开启打包分析
  148. config
  149. .plugin('webpack-bundle-analyzer')
  150. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  151. }
  152. },
  153. css: {
  154. loaderOptions: {
  155. less: {
  156. lessOptions: {
  157. javascriptEnabled: true,
  158. },
  159. },
  160. },
  161. },
  162. /**
  163. * 考虑到每个人的配置习惯不同,如有自定义 devServer 配置的需求请在根目录下创建 dev.server.config.js 文件
  164. * 然后使用 module.exports 导出配置即可,请勿直接修改以下配置 !!
  165. * dev.server.config.js 不进行 git 提交操作
  166. */
  167. devServer: Object.assign({
  168. overlay: {
  169. warnings: true,
  170. errors: true,
  171. },
  172. open: process.platform === 'darwin',
  173. port: 8080,
  174. proxy: {
  175. '/api': {
  176. target: 'http://localhost:30300',
  177. ws: true,
  178. changeOrigin: true,
  179. proxyTimeout: PROXY_TIMEOUT,
  180. },
  181. },
  182. watchOptions: {
  183. aggregateTimeout: 600, // 当第一个文件更改,会在重新构建前增加延迟。这个选项允许 webpack 将这段时间内进行的任何其他更改都聚合到一次重新构建里。以毫秒为单位q
  184. ignored: [/.git/, /node_modules/],
  185. },
  186. }, devServerCoustomConfig),
  187. }