vue.config.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. plugins: [
  97. new (require('copy-webpack-plugin'))([
  98. { from: resolve('node_modules/tinymce/skins'), to: 'tinymce/skins' },
  99. { from: resolve('node_modules/tinymce/icons'), to: 'tinymce/icons' },
  100. { from: resolve('node_modules/tinymce/models'), to: 'tinymce/models' },
  101. { from: resolve('node_modules/tinymce/plugins/emoticons/js'), to: 'tinymce/plugins/emoticons/js' },
  102. { from: resolve('public/tinymce/langs'), to: 'tinymce/langs' },
  103. { from: resolve('node_modules/mathjax/es5/tex-svg.js'), to: 'mathjax/tex-svg.js' },
  104. { from: resolve('node_modules/mathjax/es5/output/svg'), to: 'mathjax/output-svg' },
  105. ])
  106. ]
  107. },
  108. chainWebpack(config) {
  109. config.plugins.delete('preload') // TODO: need test
  110. config.plugins.delete('prefetch') // TODO: need test
  111. // 图片转base64
  112. // const imagesRule = config.module.rule('images');
  113. // imagesRule.uses.clear() //清除原本的images loader配置
  114. // imagesRule
  115. // .test(/\.(jpg|gif|png|svg)$/)
  116. // .exclude
  117. // .add(path.join(__dirname,"../node_modules"))//去除node_modules里的图片转base64配置
  118. // .end()
  119. // .use('url-loader')
  120. // .loader('url-loader')
  121. // .options({name:"img/[name].[hash:8].[ext]",limit: 1})
  122. //标记打包时间与当前git版本信息
  123. config.plugin('html').tap(args => {
  124. args[0].buildUser = buildUser;
  125. args[0].buildTime = buildTime;
  126. args[0].buildEdition = buildEdition;
  127. // args[0].renewalEncoding = process.env.VUE_APP_RENEWAL_ENCODING;
  128. // args[0].minify.removeComments = false
  129. return args
  130. })
  131. // set svg-sprite-loader
  132. config.module
  133. .rule('svg')
  134. .exclude.add(resolve('src/assets/icons'))
  135. .end()
  136. config.module
  137. .rule('icons')
  138. .test(/\.svg$/)
  139. .include.add(resolve('src/assets/icons'))
  140. .end()
  141. .use('svg-sprite-loader')
  142. .loader('svg-sprite-loader')
  143. .options({
  144. symbolId: 'icon-[name]'
  145. })
  146. .end()
  147. config
  148. .plugin('ScriptExtHtmlWebpackPlugin')
  149. .after('html')
  150. .use('script-ext-html-webpack-plugin', [{
  151. // `runtime` must same as runtimeChunk name. default is `runtime`
  152. inline: /runtime\..*\.js$/
  153. }])
  154. .end()
  155. config.optimization.splitChunks({
  156. chunks: 'all',
  157. cacheGroups: {
  158. libs: {
  159. name: 'chunk-libs',
  160. test: /[\\/]node_modules[\\/]/,
  161. priority: 10,
  162. chunks: 'initial' // only package third parties that are initially dependent
  163. },
  164. elementUI: {
  165. name: 'chunk-elementUI', // split elementUI into a single package
  166. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  167. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  168. },
  169. echarts: {
  170. name: 'chunk-echarts',
  171. priority: 20,
  172. test: /[\\/]node_modules[\\/]_?echarts(.*)/
  173. },
  174. flv: {
  175. name: 'chunk-flv',
  176. priority: 20,
  177. test: /[\\/]node_modules[\\/]_?flv(.*)/
  178. },
  179. jsBeautify: {
  180. name: 'chunk-vue-js-beautify',
  181. priority: 20,
  182. test: /[\\/]node_modules[\\/]_?js-beautify(.*)/
  183. },
  184. jsencrypt: {
  185. name: 'chunk-vue-jsencrypt',
  186. priority: 20,
  187. test: /[\\/]node_modules[\\/]_?jsencrypt(.*)/
  188. },
  189. api: {
  190. name: 'chunk-api',
  191. test: resolve('src/api'),
  192. priority: 0,
  193. minSize: 0,
  194. minChunks: 1,
  195. enforce:true,
  196. reuseExistingChunk: true
  197. },
  198. assets: {
  199. name: 'chunk-assets',
  200. test: resolve('src/assets'),
  201. priority: 0,
  202. minSize: 0,
  203. minChunks: 1,
  204. enforce:true,
  205. reuseExistingChunk: true
  206. },
  207. commons: {
  208. name: 'chunk-commons',
  209. test: resolve('src/components'),
  210. priority: 0,
  211. minSize: 0,
  212. minChunks: 1,
  213. enforce:true,
  214. reuseExistingChunk: true
  215. },
  216. directive: {
  217. name: 'chunk-directive',
  218. test: resolve('src/directive'),
  219. priority: 0,
  220. minSize: 0,
  221. minChunks: 1,
  222. enforce:true,
  223. reuseExistingChunk: true
  224. },
  225. utils: {
  226. name: 'chunk-utils',
  227. test: resolve('src/utils'),
  228. priority: 0,
  229. minSize: 0,
  230. minChunks: 1,
  231. enforce:true,
  232. reuseExistingChunk: true
  233. },
  234. // 基础模块
  235. basicsModules: {
  236. name: 'chunk-basicsModules',
  237. test: resolve('src/views/basicsModules'),
  238. priority: 0,
  239. minSize: 0,
  240. minChunks: 1,
  241. enforce:true,
  242. reuseExistingChunk: true
  243. },
  244. // 安全教育考试
  245. chemicalManage: {
  246. name: 'chunk-safetyEducationExaminationNew',
  247. test: resolve('src/views/safetyEducationExaminationNew'),
  248. priority: 0,
  249. minSize: 0,
  250. minChunks: 1,
  251. enforce:true,
  252. reuseExistingChunk: true
  253. },
  254. }
  255. })
  256. config.optimization.runtimeChunk('single'),{
  257. from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
  258. to: './', //到根目录下
  259. }
  260. }
  261. }