vue.config.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 'use strict'
  2. const path = require('path')
  3. function resolve(dir) {
  4. return path.join(__dirname, dir)
  5. }
  6. const name = '杰安易购商城' // page title
  7. const port = 9000 // dev port
  8. const externals = {
  9. vue: 'Vue',
  10. 'vue-router': 'VueRouter',
  11. vuex: 'Vuex',
  12. vant: 'vant',
  13. axios: 'axios'
  14. }
  15. // cdn
  16. const cdn = {
  17. // 开发环境
  18. dev: {
  19. css: [],
  20. js: [
  21. // 'https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.min.js',
  22. 'https://cdn.bootcdn.net/ajax/libs/vue-router/3.0.6/vue-router.min.js',
  23. 'https://cdn.bootcdn.net/ajax/libs/axios/0.18.0/axios.min.js',
  24. 'https://cdn.bootcdn.net/ajax/libs/vuex/3.1.1/vuex.min.js',
  25. 'https://cdn.bootcdn.net/ajax/libs/crypto-js/3.1.9/crypto-js.min.js',
  26. ]
  27. },
  28. // 生产环境
  29. build: {
  30. css: [],
  31. // 'https://cdn.jsdelivr.net/npm/vant@beta/lib/index.css'
  32. js: [
  33. 'js/vue.min.js',
  34. 'js/vue-router.min.js',
  35. "js/axios.min.js",
  36. 'js/vuex.min.js',
  37. 'js/crypto-js.min.js',
  38. 'js/vant.min.js',
  39. // 'https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.min.js',
  40. // 'https://cdn.bootcdn.net/ajax/libs/vue-router/3.0.6/vue-router.min.js',
  41. // 'https://cdn.bootcdn.net/ajax/libs/axios/0.18.0/axios.min.js',
  42. // 'https://cdn.bootcdn.net/ajax/libs/vuex/3.1.1/vuex.min.js',
  43. // 'https://cdn.bootcdn.net/ajax/libs/crypto-js/3.1.9/crypto-js.min.js',
  44. // 'https://cdn.jsdelivr.net/npm/vant@beta/lib/vant.min.js'
  45. ]
  46. // css: ['https://cdn.jsdelivr.net/npm/vant@beta/lib/index.css'],
  47. // js: [
  48. // 'https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js',
  49. // 'https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.0.6/vue-router.min.js',
  50. // 'https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js',
  51. // 'https://cdnjs.cloudflare.com/ajax/libs/vuex/3.1.1/vuex.min.js',
  52. // 'https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js',
  53. // 'https://cdn.jsdelivr.net/npm/vant@beta/lib/vant.min.js'
  54. // ]
  55. }
  56. }
  57. module.exports = {
  58. publicPath: './', // router hash 模式使用
  59. // publicPath: process.env.NODE_ENV === 'development' ? '/' : '/app/', //router history模式使用 需要区分生产环境和开发环境,不然build会报错
  60. outputDir: 'dist',
  61. assetsDir: 'static',
  62. lintOnSave: process.env.NODE_ENV === 'development',
  63. productionSourceMap: false,
  64. devServer: {
  65. proxy: {
  66. // '/api': {
  67. // target: 'http://jiean.demo.ximengnaikang.com/api/', //对应自己的接口
  68. // changeOrigin: true,
  69. // ws: true,
  70. // pathRewrite: {
  71. // '^/api': ''
  72. // }
  73. // }
  74. }
  75. },
  76. css: {
  77. loaderOptions: {
  78. less: {
  79. // 若使用 less-loader@5,请移除 lessOptions 这一级,直接配置选项。
  80. lessOptions: {
  81. modifyVars: {
  82. // 直接覆盖变量
  83. blue: '#EF4353',
  84. red: '#EF4353',
  85. 'nav-bar-icon-color': '#010101',
  86. 'nav-bar-text-color': '#010101'
  87. }
  88. }
  89. }
  90. }
  91. },
  92. devServer: {
  93. port: port,
  94. open: false,
  95. overlay: {
  96. warnings: false,
  97. errors: true
  98. }
  99. },
  100. configureWebpack: config => {
  101. // 为生产环境修改配置...
  102. if (process.env.NODE_ENV === 'production') {
  103. // externals里的模块不打包
  104. Object.assign(config, {
  105. name: name,
  106. externals: externals
  107. })
  108. }
  109. // 为开发环境修改配置...
  110. // if (process.env.NODE_ENV === 'development') {
  111. // }
  112. },
  113. chainWebpack(config) {
  114. config.plugins.delete('preload') // TODO: need test
  115. config.plugins.delete('prefetch') // TODO: need test
  116. // alias
  117. config.resolve.alias
  118. .set('@', resolve('src'))
  119. .set('assets', resolve('src/assets'))
  120. .set('api', resolve('src/api'))
  121. .set('views', resolve('src/views'))
  122. .set('components', resolve('src/components'))
  123. /**
  124. * 添加CDN参数到htmlWebpackPlugin配置中, 详见public/index.html 修改
  125. */
  126. config.plugin('html').tap(args => {
  127. if (process.env.NODE_ENV === 'production') {
  128. args[0].cdn = cdn.build
  129. }
  130. if (process.env.NODE_ENV === 'development') {
  131. args[0].cdn = cdn.dev
  132. }
  133. return args
  134. })
  135. // set preserveWhitespace
  136. config.module
  137. .rule('vue')
  138. .use('vue-loader')
  139. .loader('vue-loader')
  140. .tap(options => {
  141. options.compilerOptions.preserveWhitespace = true
  142. return options
  143. })
  144. .end()
  145. config
  146. // https://webpack.js.org/configuration/devtool/#development
  147. .when(process.env.NODE_ENV === 'development', config => config.devtool('cheap-source-map'))
  148. config.when(process.env.NODE_ENV !== 'development', config => {
  149. config
  150. .plugin('ScriptExtHtmlWebpackPlugin')
  151. .after('html')
  152. .use('script-ext-html-webpack-plugin', [
  153. {
  154. // `runtime` must same as runtimeChunk name. default is `runtime`
  155. inline: /runtime\..*\.js$/
  156. }
  157. ])
  158. .end()
  159. config.optimization.splitChunks({
  160. chunks: 'all',
  161. cacheGroups: {
  162. commons: {
  163. name: 'chunk-commons',
  164. test: resolve('src/components'), // can customize your rules
  165. minChunks: 3, // minimum common number
  166. priority: 5,
  167. reuseExistingChunk: true
  168. },
  169. libs: {
  170. name: 'chunk-libs',
  171. chunks: 'initial', // only package third parties that are initially dependent
  172. test: /[\\/]node_modules[\\/]/,
  173. priority: 10
  174. }
  175. }
  176. })
  177. config.optimization.runtimeChunk('single')
  178. })
  179. }
  180. }