vue.config.example.js 4.0 KB

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