vue.config.example.js 4.0 KB

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