vue.config.js 7.8 KB

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