vue.config.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. 'use strict'
  2. const path = require('path')
  3. /*
  4. const ip = '192.168.1.43'
  5. const os = require('os')
  6. const fs = require("fs")
  7. //判断环境
  8. let ifaces = os.networkInterfaces()
  9. let localityIp = '', result = []
  10. for(let dev in ifaces) {
  11. ifaces[dev].forEach(function(details) {
  12. if(localityIp === '' && details.family === 'IPv4' && !details.internal) {
  13. localityIp = details.address
  14. return;
  15. }
  16. })
  17. }
  18. let getFile = localityIp == ip?false:true
  19. */
  20. /******** 获取git版本信息 ********/
  21. /*
  22. const gitHEAD = getFile ? fs.readFileSync('.git/HEAD', 'utf-8').trim() : false
  23. const ref = getFile ? gitHEAD.split(': ')[1] : false
  24. const develop = getFile ? gitHEAD.split('/')[2] : false
  25. const gitVersion = getFile ? fs.readFileSync('.git/' + ref, 'utf-8').trim() : false
  26. const buildEdition = develop && gitVersion ? develop + ': ' + gitVersion : '未配置'
  27. const myDate = new Date()
  28. const buildTime = myDate.getFullYear() +'-'+ (myDate.getMonth()+1) +'-'+ myDate.getDate() +' '+ myDate.getHours() +':'+ myDate.getMinutes();
  29. const logsList = getFile ? fs.readFileSync('.git/logs/HEAD', 'utf-8').replace(/\r\n|\r/g, "\n").split("\n") : false
  30. const buildUser = getFile ? logsList[logsList.length-2].split(" ")[2] : '未配置'
  31. //生成随机版本编码
  32. const revision = generateRandomString()
  33. process.env.VUE_APP_RENEWAL_ENCODING = revision
  34. require('fs').writeFileSync('public/version.txt', revision)
  35. function generateRandomString() {
  36. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  37. let randomString = "";
  38. for (let i = 0; i < 24; i++) {
  39. let randomIndex = Math.floor(Math.random() * chars.length);
  40. randomString += chars.charAt(randomIndex);
  41. }
  42. return randomString;
  43. }
  44. */
  45. function resolve(dir) {
  46. return path.join(__dirname, dir)
  47. }
  48. const name = process.env.VUE_APP_TITLE || '实验室安全智慧化管控系统' // 网页标题
  49. const port = process.env.port || process.env.npm_config_port || 80 // 端口
  50. // vue.mqttConfig.js说明
  51. // 官方vue.mqttConfig.js文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
  52. // 这里只列一部分,具体配置参考文档
  53. module.exports = {
  54. // 部署生产环境和开发环境下的URL。
  55. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  56. publicPath: process.env.NODE_ENV === "production" ? "./" : "./",
  57. // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
  58. outputDir: 'dist',
  59. // 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
  60. assetsDir: 'static',
  61. // 是否开启eslint保存检测,有效值:ture | false | 'error'
  62. lintOnSave: process.env.NODE_ENV === 'development',
  63. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  64. productionSourceMap: false,
  65. // webpack-dev-server 相关配置
  66. devServer: {
  67. host: '0.0.0.0',
  68. port: port,
  69. open: true,
  70. proxy: {
  71. // detail: https://cli.vuejs.org/config/#devserver-proxy
  72. [process.env.VUE_APP_BASE_API]: {
  73. target: `http://localhost:8080`,
  74. changeOrigin: true,
  75. pathRewrite: {
  76. ['^' + process.env.VUE_APP_BASE_API]: ''
  77. }
  78. }
  79. },
  80. disableHostCheck: true,
  81. },
  82. //修改了sass版本从1.32.0 升级至1.39.0 以解决页面小图标偶发性乱码问题 以下css代码为同步增加
  83. css: {
  84. loaderOptions: {
  85. sass: {
  86. sassOptions: {
  87. outputStyle: 'expanded'
  88. }
  89. }
  90. }
  91. },
  92. configureWebpack: {
  93. name: name,
  94. resolve: {
  95. alias: {
  96. '@': resolve('src')
  97. }
  98. }
  99. },
  100. chainWebpack(config) {
  101. config.plugins.delete('preload') // TODO: need test
  102. config.plugins.delete('prefetch') // TODO: need test
  103. // 图片转base64
  104. // const imagesRule = config.module.rule('images');
  105. // imagesRule.uses.clear() //清除原本的images loader配置
  106. // imagesRule
  107. // .test(/\.(jpg|gif|png|svg)$/)
  108. // .exclude
  109. // .add(path.join(__dirname,"../node_modules"))//去除node_modules里的图片转base64配置
  110. // .end()
  111. // .use('url-loader')
  112. // .loader('url-loader')
  113. // .options({name:"img/[name].[hash:8].[ext]",limit: 1})
  114. //标记打包时间与当前git版本信息
  115. // config.plugin('html').tap(args => {
  116. // args[0].buildUser = buildUser;
  117. // args[0].buildTime = buildTime;
  118. // args[0].buildEdition = buildEdition;
  119. // return args
  120. // })
  121. // set svg-sprite-loader
  122. config.module
  123. .rule('svg')
  124. .exclude.add(resolve('src/assets/icons'))
  125. .end()
  126. config.module
  127. .rule('icons')
  128. .test(/\.svg$/)
  129. .include.add(resolve('src/assets/icons'))
  130. .end()
  131. .use('svg-sprite-loader')
  132. .loader('svg-sprite-loader')
  133. .options({
  134. symbolId: 'icon-[name]'
  135. })
  136. .end()
  137. config
  138. .plugin('ScriptExtHtmlWebpackPlugin')
  139. .after('html')
  140. .use('script-ext-html-webpack-plugin', [{
  141. // `runtime` must same as runtimeChunk name. default is `runtime`
  142. inline: /runtime\..*\.js$/
  143. }])
  144. .end()
  145. config.optimization.splitChunks({
  146. chunks: 'all',
  147. cacheGroups: {
  148. libs: {
  149. name: 'chunk-libs',
  150. test: /[\\/]node_modules[\\/]/,
  151. priority: 10,
  152. chunks: 'initial' // only package third parties that are initially dependent
  153. },
  154. elementUI: {
  155. name: 'chunk-elementUI', // split elementUI into a single package
  156. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  157. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  158. },
  159. echarts: {
  160. name: 'chunk-echarts',
  161. priority: 20,
  162. test: /[\\/]node_modules[\\/]_?echarts(.*)/
  163. },
  164. flv: {
  165. name: 'chunk-flv',
  166. priority: 20,
  167. test: /[\\/]node_modules[\\/]_?flv(.*)/
  168. },
  169. jsBeautify: {
  170. name: 'chunk-vue-js-beautify',
  171. priority: 20,
  172. test: /[\\/]node_modules[\\/]_?js-beautify(.*)/
  173. },
  174. jsencrypt: {
  175. name: 'chunk-vue-jsencrypt',
  176. priority: 20,
  177. test: /[\\/]node_modules[\\/]_?jsencrypt(.*)/
  178. },
  179. api: {
  180. name: 'chunk-api',
  181. test: resolve('src/api'),
  182. priority: 0,
  183. minSize: 0,
  184. minChunks: 1,
  185. enforce:true,
  186. reuseExistingChunk: true
  187. },
  188. assets: {
  189. name: 'chunk-assets',
  190. test: resolve('src/assets'),
  191. priority: 0,
  192. minSize: 0,
  193. minChunks: 1,
  194. enforce:true,
  195. reuseExistingChunk: true
  196. },
  197. commons: {
  198. name: 'chunk-commons',
  199. test: resolve('src/components'),
  200. priority: 0,
  201. minSize: 0,
  202. minChunks: 1,
  203. enforce:true,
  204. reuseExistingChunk: true
  205. },
  206. directive: {
  207. name: 'chunk-directive',
  208. test: resolve('src/directive'),
  209. priority: 0,
  210. minSize: 0,
  211. minChunks: 1,
  212. enforce:true,
  213. reuseExistingChunk: true
  214. },
  215. utils: {
  216. name: 'chunk-utils',
  217. test: resolve('src/utils'),
  218. priority: 0,
  219. minSize: 0,
  220. minChunks: 1,
  221. enforce:true,
  222. reuseExistingChunk: true
  223. },
  224. comprehensive: {
  225. name: 'chunk-views',
  226. test: resolve('src/views'),
  227. priority: 0,
  228. minSize: 0,
  229. minChunks: 1,
  230. enforce:true,
  231. reuseExistingChunk: true
  232. },
  233. }
  234. })
  235. config.optimization.runtimeChunk('single'),{
  236. from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
  237. to: './', //到根目录下
  238. }
  239. }
  240. }