vue.config.example.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * @description: 案例的打包配置
  3. * @Author: xing.heng
  4. */
  5. 'use strict'
  6. const path = require('path')
  7. const CompressionPlugin = require('compression-webpack-plugin')
  8. const webpack = require('webpack')
  9. function resolve (dir) {
  10. return path.join(__dirname, dir)
  11. }
  12. const publicPath =
  13. process.env.VUE_APP_HISTORY === 'y' ? process.env.VUE_APP_BASE + '/' : ''
  14. const JS_CDN = [
  15. publicPath + 'static/libs/vuex/vuex.min.js',
  16. publicPath + 'static/libs/vue-router/vue-router.min.js'
  17. ]
  18. const CSS_CDN = []
  19. const cdn = {
  20. css: CSS_CDN,
  21. js: JS_CDN
  22. }
  23. const port = process.env.port || process.env.npm_config_port || 7521 // dev port
  24. const plugins = [
  25. new webpack.ProvidePlugin({
  26. jQuery: 'jquery',
  27. $: 'jquery',
  28. 'window.jQuery': 'jquery'
  29. })
  30. ]
  31. module.exports = {
  32. parallel: require('os').cpus().length > 1,
  33. pages: {
  34. index: {
  35. entry: ['example/main.js'],
  36. template: 'public/index.html',
  37. filename: 'index.html',
  38. chunks: 'all'
  39. }
  40. },
  41. publicPath:
  42. process.env.VUE_APP_HISTORY === 'y' ? process.env.VUE_APP_BASE : './',
  43. outputDir: '@gcpaas/data-room-ui',
  44. assetsDir: 'static',
  45. lintOnSave: false,
  46. productionSourceMap: false,
  47. runtimeCompiler: true,
  48. devServer: {
  49. hot: true,
  50. port: port,
  51. client: {
  52. overlay: false
  53. }
  54. },
  55. configureWebpack: config => {
  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. config.cache = {
  60. type: 'filesystem',
  61. cacheDirectory: path.resolve(__dirname, '.cache')
  62. }
  63. Object.assign(config.resolve, {
  64. alias: {
  65. '@': resolve('example'),
  66. vue$: 'vue/dist/vue.common',
  67. // 大屏工程路径别名
  68. packages: resolve('packages'),
  69. '@gcpaas/data-room-ui': resolve('packages/index.js')
  70. },
  71. fallback: {
  72. path: false,
  73. fs: false
  74. }
  75. })
  76. // 如果是开发模式,忽略一些组件打包,采用cdn
  77. config.externals =
  78. process.env.NODE_ENV === 'production'
  79. ? {
  80. 'vue-router': 'VueRouter',
  81. vuex: 'Vuex'
  82. }
  83. : {}
  84. if (process.env.NODE_ENV === 'production') {
  85. return {
  86. plugins: [
  87. new CompressionPlugin({
  88. cache: true, // 开启缓存
  89. test: /\.js$|\.html$|\.css$|\.jpg$|\.jpeg$|\.png/, // 需要压缩的文件类型
  90. threshold: 10240, // 归档需要进行压缩的文件大小最小值,10K以上的进行压缩
  91. deleteOriginalAssets: false // 是否删除原文件
  92. }),
  93. ...plugins
  94. ]
  95. }
  96. } else {
  97. return {
  98. plugins: [
  99. ...plugins
  100. ]
  101. }
  102. }
  103. },
  104. chainWebpack: config => {
  105. if (process.env.NODE_ENV === 'production') {
  106. config.plugin('html-index').tap(args => {
  107. // html中添加cdn
  108. args[0].cdn = cdn
  109. // 修复 Lazy loading routes Error
  110. args[0].chunksSortMode = 'none'
  111. return args
  112. })
  113. }
  114. config.plugins.delete('prefetch-index') // 关闭prefetch
  115. config.module
  116. .rule('svg')
  117. .exclude.add(resolve('packages/assets/images/dataSourceIcon/svg'))
  118. .add(resolve('packages/assets/images/pageIcon/svg'))
  119. .add(resolve('packages/assets/images/bigScreenIcon/svg'))
  120. .add(resolve('packages/Svgs/svg'))
  121. .add(resolve('packages/assets/images/alignIcon/svg'))
  122. .end()
  123. config.module
  124. .rule('icons')
  125. .test(/\.svg$/)
  126. .include.add(resolve('packages/assets/images/dataSourceIcon/svg'))
  127. .add(resolve('packages/assets/images/pageIcon/svg'))
  128. .add(resolve('packages/assets/images/bigScreenIcon/svg'))
  129. .add(resolve('packages/Svgs/svg'))
  130. .add(resolve('packages/assets/images/alignIcon/svg'))
  131. .end()
  132. .use('svg-sprite-loader')
  133. .loader('svg-sprite-loader')
  134. .options({
  135. symbolId: 'icon-[name]'
  136. })
  137. .end()
  138. },
  139. // 在这里配置需要对node_modules中需要进行语法转义的依赖
  140. transpileDependencies: []
  141. }