vue.config.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const AutoCodePlugin = require('./webpack/plugins/auto.code.plugin')
  5. const Timestamp = new Date().getTime()
  6. function resolve(dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. const name = defaultSettings.title || '后台管理' // page title
  10. // If your port is set to 80,
  11. // use administrator privileges to execute the command line.
  12. // For example, Mac: sudo npm run
  13. // You can change the port by the following method:
  14. // port = 9527 npm run dev OR npm run dev --port = 9527
  15. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
  16. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  17. module.exports = {
  18. /**
  19. * You will need to set publicPath if you plan to deploy your site under a sub path,
  20. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  21. * then publicPath should be set to "/bar/".
  22. * In most cases please use '/' !!!
  23. * Detail: https://cli.vuejs.org/config/#publicpath
  24. */
  25. publicPath: '/',
  26. outputDir: 'dist',
  27. assetsDir: 'static',
  28. // false可以取消eslint
  29. lintOnSave: process.env.NODE_ENV === 'development',
  30. productionSourceMap: false,
  31. devServer: {
  32. port: port,
  33. open: true,
  34. overlay: {
  35. warnings: false,
  36. errors: true
  37. }
  38. },
  39. configureWebpack: {
  40. // provide the app's title in webpack's name field, so that
  41. // it can be accessed in index.html to inject the correct title.
  42. name: name,
  43. resolve: {
  44. alias: {
  45. '@': resolve('src')
  46. }
  47. },
  48. // externals: {
  49. // vue: 'Vue'
  50. // },
  51. output: { // 输出重构 打包编译后的 文件名称 【模块名称.版本号.时间戳】
  52. filename: `[name].${process.env.VUE_APP_VERSION}.${Timestamp}.js`,
  53. chunkFilename: `[name].${process.env.VUE_APP_VERSION}.${Timestamp}.js`
  54. },
  55. plugins: [
  56. // 自动化编码补丁
  57. new AutoCodePlugin(
  58. [
  59. {
  60. /** ********* 路由自动化注册 ***********/
  61. // 文件监听等级
  62. maxlevel: 1,
  63. // 监听./src/router/*下的文件夹
  64. inPath: resolve('src/views'),
  65. // 自动在./src/router/目录下生成index.js
  66. outPath: resolve('src/views/index.js'),
  67. // 模板
  68. // fileName: 文件夹名称
  69. // filePath: 文件夹路径
  70. templateEach: (fileName, filePath) => {
  71. return `${fileName}: () => import( /* webpackChunkName: "pages/${fileName}" */"./${fileName}/index.vue"),`
  72. },
  73. /**
  74. * 输出模板
  75. * template: 模板名称
  76. * modules: 模板模块名称
  77. */
  78. out: (template, modules) => {
  79. return `
  80. /* eslint-disable */
  81. /**
  82. * @author jiinfo-team
  83. * @desc 页面自动化注册
  84. * @important 此文件禁止手动修改!
  85. */
  86. // 路由配置
  87. import Layout from '@/layout'
  88. const menusList = {
  89. ${template}
  90. Layout
  91. };
  92. export default menusList;
  93. `
  94. }
  95. /** ********* 路由自动化注册 ***********/
  96. },
  97. {
  98. /** ********* 组件自动化注册 ***********/
  99. // 文件监听等级
  100. maxlevel: 1,
  101. // 监听./src/router/*下的文件夹
  102. inPath: resolve('src/components'),
  103. // 自动在./src/router/目录下生成index.js
  104. outPath: resolve('src/components/index.js'),
  105. // 模板
  106. // fileName: 文件夹名称
  107. // filePath: 文件夹路径
  108. templateEach: (fileName, filePath) => {
  109. // chunk名称
  110. return `Vue.component('${fileName}', () => import(/* webpackChunkName: "components${fileName}" */ './${fileName}/index.vue'));`
  111. },
  112. /**
  113. * 输出模板
  114. * template: 模板名称
  115. * modules: 模板模块名称
  116. */
  117. out: (template, modules) => {
  118. return `
  119. /* eslint-disable */
  120. /**
  121. * @author Jiinfo-team
  122. * @desc 组件自动化注册
  123. * @important 此文件禁止手动修改!
  124. */
  125. ${template}
  126. import Vue from 'vue'
  127. `
  128. },
  129. // 自动新建index入口文件
  130. addIndex: [{
  131. // 默认路由
  132. state: 'file',
  133. name: (fileName, filePath) => `${fileName}.vue`,
  134. template: (fileName, filePath) => {
  135. return `
  136. <template>
  137. <div>
  138. 组件 ocj-${fileName}
  139. </div>
  140. </template>
  141. <script>
  142. export default {
  143. name: 'com-${fileName}'
  144. };
  145. </script>
  146. `
  147. }
  148. }
  149. ]
  150. /** ********* 组件自动化注册 ***********/
  151. }
  152. ]
  153. )
  154. ]
  155. },
  156. chainWebpack(config) {
  157. config.plugins.delete('preload') // TODO: need test
  158. config.plugins.delete('prefetch') // TODO: need test
  159. // set svg-sprite-loader
  160. config.module
  161. .rule('svg')
  162. .exclude.add(resolve('src/icons'))
  163. .end()
  164. config.module
  165. .rule('icons')
  166. .test(/\.svg$/)
  167. .include.add(resolve('src/icons'))
  168. .end()
  169. .use('svg-sprite-loader')
  170. .loader('svg-sprite-loader')
  171. .options({
  172. symbolId: 'icon-[name]'
  173. })
  174. .end()
  175. // set preserveWhitespace
  176. config.module
  177. .rule('vue')
  178. .use('vue-loader')
  179. .loader('vue-loader')
  180. .tap(options => {
  181. options.compilerOptions.preserveWhitespace = true
  182. return options
  183. })
  184. .end()
  185. config
  186. // https://webpack.js.org/configuration/devtool/#development
  187. .when(process.env.NODE_ENV === 'development',
  188. config => config.devtool('cheap-source-map')
  189. )
  190. config
  191. .when(process.env.NODE_ENV !== 'development',
  192. config => {
  193. config
  194. .plugin('ScriptExtHtmlWebpackPlugin')
  195. .after('html')
  196. .use('script-ext-html-webpack-plugin', [{
  197. // `runtime` must same as runtimeChunk name. default is `runtime`
  198. inline: /runtime\..*\.js$/
  199. }])
  200. .end()
  201. config
  202. .optimization.splitChunks({
  203. chunks: 'all',
  204. cacheGroups: {
  205. libs: {
  206. name: 'chunk-libs',
  207. test: /[\\/]node_modules[\\/]/,
  208. priority: 10,
  209. chunks: 'initial' // only package third parties that are initially dependent
  210. },
  211. elementUI: {
  212. name: 'chunk-elementUI', // split elementUI into a single package
  213. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  214. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  215. },
  216. commons: {
  217. name: 'chunk-commons',
  218. test: resolve('src/components'), // can customize your rules
  219. minChunks: 3, // minimum common number
  220. priority: 5,
  221. reuseExistingChunk: true
  222. }
  223. }
  224. })
  225. config.optimization.runtimeChunk('single')
  226. }
  227. )
  228. }
  229. }