vue.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. target: 'http://192.168.0.166:6001/gw-admin/', //鲁阳的
  45. changeOrigin: true,
  46. ws: true,
  47. // logLevel:'debug',
  48. pathRewrite: { // 重命名
  49. '^/dev-api': ''
  50. },
  51. onProxyReq: function (proxyReq, req, res, options) {
  52. if (req.body) {
  53. const reg = new RegExp('application/json')
  54. if (reg.test(proxyReq.getHeader('Content-Type'))) {
  55. const bodyData = JSON.stringify(req.body)
  56. proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData))
  57. proxyReq.write(bodyData)
  58. }
  59. }
  60. }
  61. }
  62. }
  63. },
  64. /* 指定node_modules目录中需要做babel转译的依赖库 */
  65. transpileDependencies: [
  66. 'element-ui', 'vuedraggable',
  67. ],
  68. css: {
  69. loaderOptions: {
  70. scss: {
  71. /* 自动引入全局scss文件 */
  72. prependData: `
  73. @import "./src/styles/global.scss";
  74. `
  75. }
  76. }
  77. },
  78. configureWebpack: (config) => {
  79. config.devtool = 'source-map'
  80. config.output.libraryExport = 'default' /* 解决import UMD打包文件时, 组件install方法执行报错的问题!! */
  81. if (IS_PROD && buildProdFlag) { /* 仅生产环境使用 */
  82. /* CDN打包,需要修改index.html加入CDN资源 */
  83. console.log('==========================================')
  84. config.externals = {
  85. 'vue': 'Vue',
  86. 'element-ui': 'ELEMENT',
  87. //'quill': 'Quill',
  88. }
  89. } else {
  90. // config.externals = {
  91. // 'vue': 'Vue',
  92. // 'element-ui': 'ELEMENT',
  93. // 'axios': 'axios',
  94. // }
  95. }
  96. },
  97. chainWebpack: config => {
  98. /* 配置svg图标自动加载 begin */
  99. config.module
  100. .rule('svg')
  101. .exclude.add(resolve('src/icons'))
  102. .end()
  103. config.module
  104. .rule('icons')
  105. .test(/\.svg$/)
  106. .include.add(resolve('src/icons'))
  107. .end()
  108. .use('svg-sprite-loader')
  109. .loader('svg-sprite-loader')
  110. .options({
  111. symbolId: 'icon-[name]'
  112. })
  113. /* 配置svg图标自动加载 end */
  114. },
  115. }