vue.config.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. const path = require('path')
  2. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  3. const UglifyjsWebpackPlugin = require('uglifyjs-webpack-plugin')
  4. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
  5. const port = process.env.port || process.env.npm_config_port || 8888
  6. const cdnDomian = './' // cdn域名,如果有cdn修改成对应的cdn
  7. const name = 'H5Vue' // page title
  8. const IS_PRODUCTION = process.env.NODE_ENV === 'production'
  9. const cdn = {
  10. css: [],
  11. js: [
  12. 'https://cdn.bootcss.com/vue/2.6.10/vue.min.js',
  13. 'https://cdn.bootcss.com/vue-router/3.0.3/vue-router.min.js',
  14. 'https://cdn.bootcss.com/vuex/3.1.0/vuex.min.js',
  15. 'https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.min.js',
  16. 'https://cdn.bootcss.com/js-cookie/2.2.1/js.cookie.min.js'
  17. ]
  18. }
  19. const externals = {
  20. vue: 'Vue',
  21. 'vue-router': 'VueRouter',
  22. vuex: 'Vuex',
  23. axios: 'axios',
  24. 'js-cookie': 'Cookies'
  25. }
  26. function resolve (dir) {
  27. return path.join(__dirname, dir)
  28. }
  29. module.exports = {
  30. publicPath: IS_PRODUCTION ? cdnDomian : './',
  31. outputDir: 'dist',
  32. assetsDir: 'static',
  33. lintOnSave: process.env.NODE_ENV === 'development',
  34. productionSourceMap: false,
  35. devServer: {
  36. port: port,
  37. open: true,
  38. overlay: {
  39. warnings: false,
  40. errors: true
  41. },
  42. proxy: {
  43. // change xxx-api/login => mock/login
  44. // detail: https://cli.vuejs.org/config/#devserver-proxy
  45. [process.env.VUE_APP_BASE_API]: {
  46. target: `http://127.0.0.1:${port}/mock`,
  47. changeOrigin: true,
  48. pathRewrite: {
  49. ['^' + process.env.VUE_APP_BASE_API]: ''
  50. }
  51. }
  52. },
  53. after: require('./mock/mock-server.js')
  54. },
  55. configureWebpack: {
  56. // provide the app's title in webpack's name field, so that
  57. // it can be accessed in index.html to inject the correct title.
  58. name: name,
  59. resolve: {
  60. alias: {
  61. '@': resolve('src'), // 主目录
  62. 'views': resolve('src/views'), // 页面
  63. 'components': resolve('src/components'), // 组件
  64. 'api': resolve('src/api'), // 接口
  65. 'utils': resolve('src/utils'), // 通用功能
  66. 'assets': resolve('src/assets'), // 静态资源
  67. 'style': resolve('src/style') // 通用样式
  68. }
  69. }
  70. },
  71. chainWebpack (config) {
  72. config.plugins.delete('preload') // TODO: need test
  73. config.plugins.delete('prefetch') // TODO: need test
  74. // set svg-sprite-loader
  75. config.module
  76. .rule('svg')
  77. .exclude.add(resolve('src/icons'))
  78. .end()
  79. config.module
  80. .rule('icons')
  81. .test(/\.svg$/)
  82. .include.add(resolve('src/icons'))
  83. .end()
  84. .use('svg-sprite-loader')
  85. .loader('svg-sprite-loader')
  86. .options({
  87. symbolId: 'icon-[name]'
  88. })
  89. .end()
  90. // set preserveWhitespace
  91. config.module
  92. .rule('vue')
  93. .use('vue-loader')
  94. .loader('vue-loader')
  95. .tap(options => {
  96. options.compilerOptions.preserveWhitespace = true
  97. return options
  98. })
  99. .end()
  100. config
  101. // https://webpack.js.org/configuration/devtool/#development
  102. .when(process.env.NODE_ENV === 'development',
  103. config => config.devtool('cheap-source-map')
  104. )
  105. config
  106. .when(process.env.NODE_ENV !== 'development',
  107. config => {
  108. config
  109. .plugin('ScriptExtHtmlWebpackPlugin')
  110. .after('html')
  111. .use('script-ext-html-webpack-plugin', [{
  112. // `runtime` must same as runtimeChunk name. default is `runtime`
  113. inline: /runtime\..*\.js$/
  114. }])
  115. .end()
  116. config
  117. .optimization.splitChunks({
  118. chunks: 'all',
  119. cacheGroups: {
  120. libs: {
  121. name: 'chunk-libs',
  122. test: /[\\/]node_modules[\\/]/,
  123. priority: 10,
  124. chunks: 'initial' // only package third parties that are initially dependent
  125. },
  126. commons: {
  127. name: 'chunk-commons',
  128. test: resolve('src/components'), // can customize your rules
  129. minChunks: 3, // minimum common number
  130. priority: 5,
  131. reuseExistingChunk: true
  132. }
  133. }
  134. })
  135. config.optimization.runtimeChunk('single')
  136. }
  137. )
  138. if (IS_PRODUCTION) {
  139. config.plugin('analyzer').use(BundleAnalyzerPlugin)
  140. config.plugin('html').tap(args => {
  141. args[0].cdn = cdn
  142. return args
  143. })
  144. config.externals(externals)
  145. config.plugin('html').tap(args => {
  146. args[0].minify.minifyCSS = true // 压缩html中的css
  147. return args
  148. })
  149. // gzip需要nginx进行配合
  150. config
  151. .plugin('compression')
  152. .use(CompressionWebpackPlugin)
  153. .tap(() => [
  154. {
  155. test: /\.js$|\.html$|\.css/, // 匹配文件名
  156. threshold: 10240, // 超过10k进行压缩
  157. deleteOriginalAssets: false // 是否删除源文件
  158. }
  159. ])
  160. config.optimization.minimizer([
  161. new UglifyjsWebpackPlugin({
  162. // 生产环境推荐关闭 sourcemap 防止源码泄漏
  163. // 服务端通过前端发送的行列,根据 sourcemap 转为源文件位置
  164. // sourceMap: true,
  165. uglifyOptions: {
  166. warnings: false,
  167. compress: {
  168. drop_console: true,
  169. drop_debugger: true
  170. }
  171. }
  172. })
  173. ])
  174. }
  175. },
  176. css: {
  177. // 是否使用css分离插件 ExtractTextPlugin
  178. extract: !!IS_PRODUCTION,
  179. // 开启 CSS source maps?
  180. sourceMap: false,
  181. // css预设器配置项
  182. // 启用 CSS modules for all css / pre-processor files.
  183. modules: false,
  184. loaderOptions: {
  185. sass: {
  186. data: '@import "style/_mixin.scss";@import "style/_variables.scss";@import "style/common.scss";' // 全局引入
  187. }
  188. }
  189. }
  190. }