vue.config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const path = require('path');
  2. const getFirstHost = () => {
  3. let host = window.location.host;
  4. host = host.split('.');
  5. host = host.length == 3 ? host[1] : host[0];
  6. return process.env.NODE_ENV == 'development' ? 'huaml' : host;
  7. };
  8. const isProduction = process.env.NODE_ENV === 'production';
  9. function resolve(dir) {
  10. return path.join(__dirname, dir);
  11. }
  12. // const hostUrl = `${window.location.protocol}//static.${getFirstHost()}.com`;
  13. const hostUrl = `https://static.huaml.com`;
  14. const cdn = {
  15. dev: {
  16. css: [`${hostUrl}/cdn/element-ui/2.13.0/theme-chalk/index.css`]
  17. },
  18. pro: {
  19. css: [
  20. `${hostUrl}/cdn/nprogress/0.2.0/nprogress.min.css`,
  21. `${hostUrl}/cdn/quill/2.0.0-dev.3/quill.core.min.css`,
  22. `${hostUrl}/cdn/viewerjs/1.3.7/viewer.min.css`,
  23. `${hostUrl}/cdn/element-ui/2.13.0/theme-chalk/index.css`
  24. ],
  25. js: [
  26. `${hostUrl}/cdn/vue/2.6.10/vue.min.js`,
  27. `${hostUrl}/cdn/vue-router/3.1.3/vue-router.min.js`,
  28. `${hostUrl}/cdn/vuex/3.1.1/vuex.min.js`,
  29. `${hostUrl}/cdn/clipboard.js/2.0.4/clipboard.min.js`,
  30. `${hostUrl}/cdn/axios/0.19.0-beta.1/axios.min.js`,
  31. `${hostUrl}/cdn/nprogress/0.2.0/nprogress.min.js`,
  32. `${hostUrl}/cdn/Mock.js/1.0.1-beta3/mock-min.js`,
  33. `${hostUrl}/cdn/quill/2.0.0-dev.3/quill.min.js`,
  34. `${hostUrl}/cdn/element-ui/2.13.0/index.js`,
  35. `${hostUrl}/cdn/viewerjs/1.3.7/viewer.min.js`,
  36. `${hostUrl}/cdn/echarts/4.4.0-rc.1/echarts.min.js`,
  37. `${hostUrl}/cdn/npm/vue-echarts@4.0.2.js`
  38. // https://cdn.bootcss.com
  39. // 'https://cdn.jsdelivr.net/npm/vue-echarts@4.0.2'
  40. ]
  41. }
  42. };
  43. module.exports = {
  44. publicPath: '/',
  45. lintOnSave: true,
  46. productionSourceMap: false,
  47. parallel: require('os').cpus().length > 1,
  48. css: {
  49. extract: isProduction,
  50. sourceMap: false,
  51. loaderOptions: {
  52. stylus: {
  53. import: '~@/assets/css/common.styl'
  54. },
  55. sass: {
  56. prependData: `@import "~@/assets/css/base.scss";`
  57. }
  58. }
  59. },
  60. devServer: {
  61. port: 7777,
  62. open: false,
  63. compress: false,
  64. overlay: {
  65. warnings: false,
  66. errors: true
  67. },
  68. proxy: {
  69. '/dev': {
  70. target: 'http://api.b.huaml.com',
  71. changeOrigin: true,
  72. pathRewrite: { '^/dev': '' }
  73. },
  74. '/pro': {
  75. target: 'http://api.b.huaml.com',
  76. changeOrigin: true,
  77. pathRewrite: { '^/pro': '/api' }
  78. }
  79. }
  80. },
  81. chainWebpack: config => {
  82. // 移除 prefetch 插件
  83. config.plugins.delete('prefetch');
  84. // 移除 preload 插件
  85. // config.plugins.delete('preload');
  86. // svg
  87. config.module.rule('svg').uses.clear();
  88. config.module
  89. .rule('svg-sprite-loader')
  90. .test(/.svg$/)
  91. .exclude.add(/node_modules/)
  92. .end()
  93. .use('svg-sprite-loader')
  94. .loader('svg-sprite-loader')
  95. .options({
  96. symbolId: '[name]'
  97. });
  98. // 去掉元素之间空格
  99. config.module
  100. .rule('vue')
  101. .use('vue-loader')
  102. .loader('vue-loader')
  103. .tap(options => {
  104. options.compilerOptions.preserveWhitespace = true;
  105. return options;
  106. })
  107. .end();
  108. // 引入cdn
  109. config.plugin('html').tap(args => {
  110. args[0].cdn = cdn[isProduction ? 'pro' : 'dev'];
  111. return args;
  112. });
  113. if (isProduction) {
  114. config.performance.set('hints', false);
  115. config.optimization.splitChunks({
  116. chunks: 'all',
  117. cacheGroups: {
  118. libs: {
  119. name: 'chunk-libs',
  120. test: /[\\/]node_modules[\\/]/,
  121. priority: 10,
  122. chunks: 'initial'
  123. },
  124. commons: {
  125. name: 'chunk-cool',
  126. test: resolve('src/cool'),
  127. minChunks: 3,
  128. priority: 5,
  129. reuseExistingChunk: true
  130. }
  131. }
  132. });
  133. }
  134. },
  135. configureWebpack: config => {
  136. if (isProduction) {
  137. config.externals = {
  138. vue: 'Vue',
  139. 'vue-router': 'VueRouter',
  140. 'element-ui': 'ELEMENT',
  141. vuex: 'Vuex',
  142. axios: 'axios',
  143. mockjs: 'Mock',
  144. nprogress: 'NProgress',
  145. quill: 'Quill',
  146. viewer: 'Viewer',
  147. echarts: 'echarts',
  148. 'vue-echarts': 'VueECharts'
  149. };
  150. }
  151. }
  152. };