vue.config.js 3.9 KB

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