vue.config.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  3. * @description cli配置
  4. */
  5. const path = require('path')
  6. const {
  7. publicPath,
  8. assetsDir,
  9. outputDir,
  10. lintOnSave,
  11. transpileDependencies,
  12. title,
  13. abbreviation,
  14. devPort,
  15. providePlugin,
  16. build7z,
  17. donation,
  18. } = require('./src/config')
  19. const { webpackBarName, webpackBanner, donationConsole } = require('zx-layouts')
  20. if (donation) donationConsole()
  21. const { version, author } = require('./package.json')
  22. const Webpack = require('webpack')
  23. const WebpackBar = require('webpackbar')
  24. const FileManagerPlugin = require('filemanager-webpack-plugin')
  25. const dayjs = require('dayjs')
  26. const date = dayjs().format('YYYY_M_D')
  27. const time = dayjs().format('YYYY-M-D HH:mm:ss')
  28. process.env.VUE_APP_TITLE = title || '沪上阿姨'
  29. process.env.VUE_APP_AUTHOR = author || 'chuzhixin 1204505056@qq.com'
  30. process.env.VUE_APP_UPDATE_TIME = time
  31. process.env.VUE_APP_VERSION = version
  32. const minify =
  33. process.env.NODE_ENV === 'development'
  34. ? false
  35. : {
  36. collapseWhitespace: true,
  37. removeComments: true,
  38. removeRedundantAttributes: true,
  39. removeScriptTypeAttributes: true,
  40. removeStyleLinkTypeAttributes: true,
  41. useShortDoctype: true,
  42. minifyCSS: true,
  43. minifyJS: true,
  44. }
  45. /*const mockServer = () => {
  46. if (process.env.NODE_ENV === 'development') return require('./mock')
  47. else ''
  48. }*/
  49. function resolve(dir) {
  50. return path.join(__dirname, dir)
  51. }
  52. module.exports = {
  53. publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  54. assetsDir: 'static',
  55. outputDir,
  56. lintOnSave: false,
  57. transpileDependencies,
  58. devServer: {
  59. overlay: {
  60. // 让浏览器 overlay 同时显示警告和错误
  61. warnings: true,
  62. errors: true,
  63. },
  64. host: 'localhost',
  65. port: 8080, // 端口号
  66. https: false, // https:{type:Boolean}
  67. open: false, //配置后自动启动浏览器
  68. hotOnly: true, // 热更新
  69. // proxy: 'http://localhost:8080' // 配置跨域处理,只有一个代理
  70. proxy: {
  71. //配置多个代理
  72. '/apis': {
  73. // 测试环境
  74. // target: 'https://oapi.shpr.top',
  75. target: 'http://127.0.0.1:8014/', // 接口域名
  76. changeOrigin: true, //是否跨域
  77. pathRewrite: {
  78. '^/apis': '', //需要rewrite重写的,
  79. },
  80. },
  81. },
  82. },
  83. /* configureWebpack() {
  84. return {
  85. resolve: {
  86. alias: {
  87. 'vue$': 'vue/dist/胜利ue.esm.js',
  88. '@': resolve('src'),
  89. },
  90. },
  91. plugins: [
  92. new Webpack.ProvidePlugin(providePlugin),
  93. new WebpackBar({
  94. name: webpackBarName,
  95. }),
  96. ],
  97. }
  98. },*/
  99. chainWebpack(config) {
  100. config.plugins.delete('preload')
  101. config.plugins.delete('prefetch')
  102. config.module
  103. .rule('svg')
  104. .exclude.add(resolve('src/remixIcon'))
  105. .add(resolve('src/colorfulIcon'))
  106. .end()
  107. config.module
  108. .rule('remixIcon')
  109. .test(/\.svg$/)
  110. .include.add(resolve('src/remixIcon'))
  111. .end()
  112. .use('svg-sprite-loader')
  113. .loader('svg-sprite-loader')
  114. .options({ symbolId: 'remix-icon-[name]' })
  115. .end()
  116. config.module
  117. .rule('colorfulIcon')
  118. .test(/\.svg$/)
  119. .include.add(resolve('src/colorfulIcon'))
  120. .end()
  121. .use('svg-sprite-loader')
  122. .loader('svg-sprite-loader')
  123. .options({ symbolId: 'colorful-icon-[name]' })
  124. .end()
  125. /* config.when(process.env.NODE_ENV === "development", (config) => {
  126. config.devtool("source-map");
  127. }); */
  128. config.when(process.env.NODE_ENV !== 'development', (config) => {
  129. config.performance.set('hints', false)
  130. config.devtool('none')
  131. config.optimization.splitChunks({
  132. chunks: 'all',
  133. cacheGroups: {
  134. libs: {
  135. name: 'chunk-libs',
  136. test: /[\\/]node_modules[\\/]/,
  137. priority: 10,
  138. chunks: 'initial',
  139. },
  140. elementUI: {
  141. name: 'chunk-elementUI',
  142. priority: 20,
  143. test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
  144. },
  145. fortawesome: {
  146. name: 'chunk-fortawesome',
  147. priority: 20,
  148. test: /[\\/]node_modules[\\/]_?@fortawesome(.*)/,
  149. },
  150. },
  151. })
  152. config
  153. .plugin('banner')
  154. .use(Webpack.BannerPlugin, [`${webpackBanner}${time}`])
  155. .end()
  156. config.module
  157. .rule('images')
  158. .use('image-webpack-loader')
  159. .loader('image-webpack-loader')
  160. .options({
  161. bypassOnDebug: true,
  162. })
  163. .end()
  164. })
  165. if (build7z) {
  166. config.when(process.env.NODE_ENV === 'production', (config) => {
  167. config
  168. .plugin('fileManager')
  169. .use(FileManagerPlugin, [
  170. {
  171. onEnd: {
  172. delete: [`./${outputDir}/video`, `./${outputDir}/data`],
  173. archive: [
  174. {
  175. source: `./${outputDir}`,
  176. destination: `./${outputDir}/${abbreviation}_${outputDir}_${date}.7z`,
  177. },
  178. ],
  179. },
  180. },
  181. ])
  182. .end()
  183. })
  184. }
  185. },
  186. runtimeCompiler: true,
  187. productionSourceMap: false,
  188. css: {
  189. requireModuleExtension: true,
  190. sourceMap: true,
  191. loaderOptions: {
  192. scss: {
  193. /*sass-loader 8.0语法 */
  194. //prependData: '@import "~@/styles/variables.scss";',
  195. /*sass-loader 9.0写法,感谢github用户 shaonialife*/
  196. additionalData(content, loaderContext) {
  197. const { resourcePath, rootContext } = loaderContext
  198. const relativePath = path.relative(rootContext, resourcePath)
  199. if (
  200. relativePath.replace(/\\/g, '/') !== 'src/styles/variables.scss'
  201. ) {
  202. return '@import "~@/styles/variables.scss";' + content
  203. }
  204. return content
  205. },
  206. },
  207. },
  208. },
  209. }