vue.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict'
  2. const path = require('path')
  3. const IS_PROD = process.env.NODE_ENV === 'production'
  4. function resolve (dir) {
  5. return path.join(__dirname, dir)
  6. }
  7. const npmConfigArgv = (process.env.npm_config_argv === undefined) ? null : JSON.parse(process.env.npm_config_argv)
  8. /*
  9. console.log('npm config: ', npmConfigArgv)
  10. const procArgv = process.argv
  11. console.log('npm config: ', procArgv)
  12. */
  13. let buildProdFlag = false
  14. if (!!npmConfigArgv) {
  15. npmConfigArgv.original.forEach(cItem => {
  16. if (cItem === 'build') {
  17. buildProdFlag = true
  18. }
  19. })
  20. }
  21. const mvdir = require('mvdir');
  22. if (IS_PROD && buildProdFlag) {
  23. mvdir('index_template/index_prod.html', 'public/index.html', { copy: true });
  24. } else {
  25. mvdir('index_template/index_dev.html', 'public/index.html', { copy: true });
  26. }
  27. module.exports = {
  28. publicPath: './',
  29. assetsDir: './',
  30. /* 开启vue运行时模板编译功能!! */
  31. runtimeCompiler: true,
  32. lintOnSave: false,
  33. productionSourceMap: false,
  34. devServer: {
  35. // port: port,
  36. open: true,
  37. overlay: {
  38. warnings: false,
  39. errors: true
  40. },
  41. proxy: {
  42. '/dev-api': {
  43. target: 'http://localhost:6001/gw-admin/',
  44. changeOrigin: true,
  45. ws: true,
  46. // logLevel:'debug',
  47. pathRewrite: { // 重命名
  48. '^/dev-api': ''
  49. },
  50. onProxyReq: function (proxyReq, req, res, options) {
  51. if (req.body) {
  52. const reg = new RegExp('application/json')
  53. if (reg.test(proxyReq.getHeader('Content-Type'))) {
  54. const bodyData = JSON.stringify(req.body)
  55. proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData))
  56. proxyReq.write(bodyData)
  57. }
  58. }
  59. }
  60. }
  61. }
  62. },
  63. /* 指定node_modules目录中需要做babel转译的依赖库 */
  64. transpileDependencies: [
  65. 'element-ui', 'vuedraggable',
  66. ],
  67. css: {
  68. loaderOptions: {
  69. scss: {
  70. /* 自动引入全局scss文件 */
  71. prependData: `
  72. @import "./src/styles/global.scss";
  73. `
  74. }
  75. }
  76. },
  77. configureWebpack: (config) => {
  78. config.devtool = 'source-map'
  79. config.output.libraryExport = 'default' /* 解决import UMD打包文件时, 组件install方法执行报错的问题!! */
  80. if (IS_PROD && buildProdFlag) { /* 仅生产环境使用 */
  81. /* CDN打包,需要修改index.html加入CDN资源 */
  82. console.log('==========================================')
  83. config.externals = {
  84. 'vue': 'Vue',
  85. 'element-ui': 'ELEMENT',
  86. //'quill': 'Quill',
  87. }
  88. } else {
  89. // config.externals = {
  90. // 'vue': 'Vue',
  91. // 'element-ui': 'ELEMENT',
  92. // 'axios': 'axios',
  93. // }
  94. }
  95. },
  96. chainWebpack: config => {
  97. /* 配置svg图标自动加载 begin */
  98. config.module
  99. .rule('svg')
  100. .exclude.add(resolve('src/icons'))
  101. .end()
  102. config.module
  103. .rule('icons')
  104. .test(/\.svg$/)
  105. .include.add(resolve('src/icons'))
  106. .end()
  107. .use('svg-sprite-loader')
  108. .loader('svg-sprite-loader')
  109. .options({
  110. symbolId: 'icon-[name]'
  111. })
  112. /* 配置svg图标自动加载 end */
  113. },
  114. }