vue.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. function resolve(dir) {
  5. return path.join(__dirname, dir)
  6. }
  7. const name = defaultSettings.title // page title
  8. const packageName = require('./package.json').name
  9. // If your port is set to 80,
  10. // use administrator privileges to execute the command line.
  11. // For example, Mac: sudo npm run
  12. // You can change the port by the following methods:
  13. // port = 9528 npm run dev OR npm run dev --port = 9528
  14. const port = process.env.port || process.env.npm_config_port || 8080 // dev port
  15. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  16. module.exports = {
  17. /**
  18. * You will need to set publicPath if you plan to deploy your site under a sub path,
  19. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  20. * then publicPath should be set to "/bar/".
  21. * In most cases please use '/' !!!
  22. * Detail: https://cli.vuejs.org/config/#publicpath
  23. */
  24. publicPath: process.env.VUE_APP_PATH_BASE,
  25. outputDir: 'dist',
  26. assetsDir: 'static',
  27. lintOnSave: process.env.NODE_ENV === 'development',
  28. productionSourceMap: process.env.NODE_ENV === 'development',
  29. devServer: {
  30. port: port,
  31. https: false,
  32. open: true,
  33. overlay: {
  34. warnings: false,
  35. errors: true
  36. },
  37. headers: {
  38. 'Access-Control-Allow-Origin': '*'
  39. },
  40. before: require('./mock/mock-server.js')
  41. },
  42. configureWebpack: {
  43. // provide the app's title in webpack's name field, so that
  44. // it can be accessed in index.html to inject the correct title.
  45. name: name,
  46. resolve: {
  47. alias: {
  48. '@': resolve('src')
  49. }
  50. },
  51. output: {
  52. // 把子应用打包成 umd 库格式
  53. // library: `${packageName}-[name]`,
  54. library: 'app-grider-erection',
  55. libraryTarget: 'umd',
  56. jsonpFunction: `webpackJsonp_${packageName}`
  57. }
  58. },
  59. chainWebpack(config) {
  60. // it can improve the speed of the first screen, it is recommended to turn on preload
  61. config.plugin('preload').tap(() => [
  62. {
  63. rel: 'preload',
  64. // to ignore runtime.js
  65. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  66. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  67. include: 'initial'
  68. }
  69. ])
  70. // when there are many pages, it will cause too many meaningless requests
  71. config.plugins.delete('prefetch')
  72. // set svg-sprite-loader
  73. config.module
  74. .rule('svg')
  75. .exclude.add(resolve('src/icons'))
  76. .end()
  77. config.module
  78. .rule('icons')
  79. .test(/\.svg$/)
  80. .include.add(resolve('src/icons'))
  81. .end()
  82. .use('svg-sprite-loader')
  83. .loader('svg-sprite-loader')
  84. .options({
  85. symbolId: 'icon-[name]'
  86. })
  87. .end()
  88. config.when(process.env.NODE_ENV !== 'development', config => {
  89. config
  90. .plugin('ScriptExtHtmlWebpackPlugin')
  91. .after('html')
  92. .use('script-ext-html-webpack-plugin', [
  93. {
  94. // `runtime` must same as runtimeChunk name. default is `runtime`
  95. inline: /runtime\..*\.js$/
  96. }
  97. ])
  98. .end()
  99. config.optimization.splitChunks({
  100. chunks: 'all',
  101. cacheGroups: {
  102. libs: {
  103. name: 'chunk-libs',
  104. test: /[\\/]node_modules[\\/]/,
  105. priority: 10,
  106. chunks: 'initial' // only package third parties that are initially dependent
  107. },
  108. elementUI: {
  109. name: 'chunk-elementUI', // split elementUI into a single package
  110. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  111. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  112. },
  113. commons: {
  114. name: 'chunk-commons',
  115. test: resolve('src/components'), // can customize your rules
  116. minChunks: 3, // minimum common number
  117. priority: 5,
  118. reuseExistingChunk: true
  119. }
  120. }
  121. })
  122. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  123. config.optimization.runtimeChunk('single')
  124. })
  125. }
  126. }