vue.config.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 || 81 // 端口
  46. // vue.mqttConfig.js说明
  47. // 官方vue.mqttConfig.js文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
  48. // 这里只列一部分,具体配置参考文档
  49. module.exports = {
  50. transpileDependencies: ['@zumer/snapdom'],
  51. // 部署生产环境和开发环境下的URL。
  52. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  53. publicPath: process.env.NODE_ENV === "production" ? "./" : "./",
  54. // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
  55. outputDir: 'dist',
  56. // 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
  57. assetsDir: 'static',
  58. // 是否开启eslint保存检测,有效值:ture | false | 'error'
  59. lintOnSave: process.env.NODE_ENV === 'development',
  60. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  61. productionSourceMap: false,
  62. // webpack-dev-server 相关配置
  63. devServer: {
  64. host: '0.0.0.0',
  65. port: port,
  66. open: true,
  67. proxy: {
  68. // detail: https://cli.vuejs.org/config/#devserver-proxy
  69. [process.env.VUE_APP_BASE_API]: {
  70. target: `http://localhost:8080`,
  71. changeOrigin: true,
  72. pathRewrite: {
  73. ['^' + process.env.VUE_APP_BASE_API]: ''
  74. }
  75. }
  76. },
  77. disableHostCheck: true,
  78. },
  79. //修改了sass版本从1.32.0 升级至1.39.0 以解决页面小图标偶发性乱码问题 以下css代码为同步增加
  80. css: {
  81. loaderOptions: {
  82. sass: {
  83. sassOptions: {
  84. outputStyle: 'expanded'
  85. }
  86. }
  87. }
  88. },
  89. configureWebpack: {
  90. name: name,
  91. resolve: {
  92. alias: {
  93. '@': resolve('src')
  94. }
  95. }
  96. },
  97. chainWebpack(config) {
  98. config.plugins.delete('preload') // TODO: need test
  99. config.plugins.delete('prefetch') // TODO: need test
  100. // 图片转base64
  101. // const imagesRule = config.module.rule('images');
  102. // imagesRule.uses.clear() //清除原本的images loader配置
  103. // imagesRule
  104. // .test(/\.(jpg|gif|png|svg)$/)
  105. // .exclude
  106. // .add(path.join(__dirname,"../node_modules"))//去除node_modules里的图片转base64配置
  107. // .end()
  108. // .use('url-loader')
  109. // .loader('url-loader')
  110. // .options({name:"img/[name].[hash:8].[ext]",limit: 1})
  111. //标记打包时间与当前git版本信息
  112. config.plugin('html').tap(args => {
  113. args[0].buildUser = buildUser;
  114. args[0].buildTime = buildTime;
  115. args[0].buildEdition = buildEdition;
  116. // args[0].renewalEncoding = process.env.VUE_APP_RENEWAL_ENCODING;
  117. // args[0].minify.removeComments = false
  118. return args
  119. })
  120. // set svg-sprite-loader
  121. config.module
  122. .rule('svg')
  123. .exclude.add(resolve('src/assets/icons'))
  124. .end()
  125. config.module
  126. .rule('icons')
  127. .test(/\.svg$/)
  128. .include.add(resolve('src/assets/icons'))
  129. .end()
  130. .use('svg-sprite-loader')
  131. .loader('svg-sprite-loader')
  132. .options({
  133. symbolId: 'icon-[name]'
  134. })
  135. .end()
  136. config
  137. .plugin('ScriptExtHtmlWebpackPlugin')
  138. .after('html')
  139. .use('script-ext-html-webpack-plugin', [{
  140. // `runtime` must same as runtimeChunk name. default is `runtime`
  141. inline: /runtime\..*\.js$/
  142. }])
  143. .end()
  144. config.optimization.splitChunks({
  145. chunks: 'all',
  146. cacheGroups: {
  147. libs: {
  148. name: 'chunk-libs',
  149. test: /[\\/]node_modules[\\/]/,
  150. priority: 10,
  151. chunks: 'initial' // only package third parties that are initially dependent
  152. },
  153. elementUI: {
  154. name: 'chunk-elementUI', // split elementUI into a single package
  155. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  156. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  157. },
  158. echarts: {
  159. name: 'chunk-echarts',
  160. priority: 20,
  161. test: /[\\/]node_modules[\\/]_?echarts(.*)/
  162. },
  163. flv: {
  164. name: 'chunk-flv',
  165. priority: 20,
  166. test: /[\\/]node_modules[\\/]_?flv(.*)/
  167. },
  168. jsBeautify: {
  169. name: 'chunk-vue-js-beautify',
  170. priority: 20,
  171. test: /[\\/]node_modules[\\/]_?js-beautify(.*)/
  172. },
  173. jsencrypt: {
  174. name: 'chunk-vue-jsencrypt',
  175. priority: 20,
  176. test: /[\\/]node_modules[\\/]_?jsencrypt(.*)/
  177. },
  178. api: {
  179. name: 'chunk-api',
  180. test: resolve('src/api'),
  181. priority: 0,
  182. minSize: 0,
  183. minChunks: 1,
  184. enforce:true,
  185. reuseExistingChunk: true
  186. },
  187. assets: {
  188. name: 'chunk-assets',
  189. test: resolve('src/assets'),
  190. priority: 0,
  191. minSize: 0,
  192. minChunks: 1,
  193. enforce:true,
  194. reuseExistingChunk: true
  195. },
  196. commons: {
  197. name: 'chunk-commons',
  198. test: resolve('src/components'),
  199. priority: 0,
  200. minSize: 0,
  201. minChunks: 1,
  202. enforce:true,
  203. reuseExistingChunk: true
  204. },
  205. directive: {
  206. name: 'chunk-directive',
  207. test: resolve('src/directive'),
  208. priority: 0,
  209. minSize: 0,
  210. minChunks: 1,
  211. enforce:true,
  212. reuseExistingChunk: true
  213. },
  214. utils: {
  215. name: 'chunk-utils',
  216. test: resolve('src/utils'),
  217. priority: 0,
  218. minSize: 0,
  219. minChunks: 1,
  220. enforce:true,
  221. reuseExistingChunk: true
  222. },
  223. // 基础模块
  224. basicsModules: {
  225. name: 'chunk-basicsModules',
  226. test: resolve('src/views/basicsModules'),
  227. priority: 0,
  228. minSize: 0,
  229. minChunks: 1,
  230. enforce:true,
  231. reuseExistingChunk: true
  232. },
  233. // 安全教育考试
  234. chemicalManage: {
  235. name: 'chunk-safetyEducationExaminationNew',
  236. test: resolve('src/views/safetyEducationExaminationNew'),
  237. priority: 0,
  238. minSize: 0,
  239. minChunks: 1,
  240. enforce:true,
  241. reuseExistingChunk: true
  242. },
  243. }
  244. })
  245. config.optimization.runtimeChunk('single'),{
  246. from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
  247. to: './', //到根目录下
  248. }
  249. }
  250. }