vue.config.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. postcss: {
  87. plugins: [
  88. require('postcss-pxtorem')({ // 把px单位换算成rem单位
  89. rootValue ({ file }) {
  90. return file.indexOf('vant') !== -1 ? 37.5 : 75
  91. },
  92. // rootValue: 75, // 换算的基数(设计图750的根字体为75,如果设计图为640:则rootValue=64)
  93. propList: ['*']
  94. })
  95. ]
  96. },
  97. }
  98. },
  99. configureWebpack: {
  100. name: name,
  101. resolve: {
  102. alias: {
  103. '@': resolve('src')
  104. }
  105. }
  106. },
  107. chainWebpack(config) {
  108. config.plugins.delete('preload') // TODO: need test
  109. config.plugins.delete('prefetch') // TODO: need test
  110. // 图片转base64
  111. // const imagesRule = config.module.rule('images');
  112. // imagesRule.uses.clear() //清除原本的images loader配置
  113. // imagesRule
  114. // .test(/\.(jpg|gif|png|svg)$/)
  115. // .exclude
  116. // .add(path.join(__dirname,"../node_modules"))//去除node_modules里的图片转base64配置
  117. // .end()
  118. // .use('url-loader')
  119. // .loader('url-loader')
  120. // .options({name:"img/[name].[hash:8].[ext]",limit: 1})
  121. //标记打包时间与当前git版本信息
  122. // config.plugin('html').tap(args => {
  123. // args[0].buildUser = buildUser;
  124. // args[0].buildTime = buildTime;
  125. // args[0].buildEdition = buildEdition;
  126. // return args
  127. // })
  128. // set svg-sprite-loader
  129. config.module
  130. .rule('svg')
  131. .exclude.add(resolve('src/assets/icons'))
  132. .end()
  133. config.module
  134. .rule('icons')
  135. .test(/\.svg$/)
  136. .include.add(resolve('src/assets/icons'))
  137. .end()
  138. .use('svg-sprite-loader')
  139. .loader('svg-sprite-loader')
  140. .options({
  141. symbolId: 'icon-[name]'
  142. })
  143. .end()
  144. config
  145. .plugin('ScriptExtHtmlWebpackPlugin')
  146. .after('html')
  147. .use('script-ext-html-webpack-plugin', [{
  148. // `runtime` must same as runtimeChunk name. default is `runtime`
  149. inline: /runtime\..*\.js$/
  150. }])
  151. .end()
  152. config.optimization.splitChunks({
  153. chunks: 'all',
  154. cacheGroups: {
  155. libs: {
  156. name: 'chunk-libs',
  157. test: /[\\/]node_modules[\\/]/,
  158. priority: 10,
  159. chunks: 'initial' // only package third parties that are initially dependent
  160. },
  161. elementUI: {
  162. name: 'chunk-elementUI', // split elementUI into a single package
  163. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  164. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  165. },
  166. echarts: {
  167. name: 'chunk-echarts',
  168. priority: 20,
  169. test: /[\\/]node_modules[\\/]_?echarts(.*)/
  170. },
  171. flv: {
  172. name: 'chunk-flv',
  173. priority: 20,
  174. test: /[\\/]node_modules[\\/]_?flv(.*)/
  175. },
  176. jsBeautify: {
  177. name: 'chunk-vue-js-beautify',
  178. priority: 20,
  179. test: /[\\/]node_modules[\\/]_?js-beautify(.*)/
  180. },
  181. jsencrypt: {
  182. name: 'chunk-vue-jsencrypt',
  183. priority: 20,
  184. test: /[\\/]node_modules[\\/]_?jsencrypt(.*)/
  185. },
  186. api: {
  187. name: 'chunk-api',
  188. test: resolve('src/api'),
  189. priority: 0,
  190. minSize: 0,
  191. minChunks: 1,
  192. enforce:true,
  193. reuseExistingChunk: true
  194. },
  195. assets: {
  196. name: 'chunk-assets',
  197. test: resolve('src/assets'),
  198. priority: 0,
  199. minSize: 0,
  200. minChunks: 1,
  201. enforce:true,
  202. reuseExistingChunk: true
  203. },
  204. commons: {
  205. name: 'chunk-commons',
  206. test: resolve('src/components'),
  207. priority: 0,
  208. minSize: 0,
  209. minChunks: 1,
  210. enforce:true,
  211. reuseExistingChunk: true
  212. },
  213. directive: {
  214. name: 'chunk-directive',
  215. test: resolve('src/directive'),
  216. priority: 0,
  217. minSize: 0,
  218. minChunks: 1,
  219. enforce:true,
  220. reuseExistingChunk: true
  221. },
  222. utils: {
  223. name: 'chunk-utils',
  224. test: resolve('src/utils'),
  225. priority: 0,
  226. minSize: 0,
  227. minChunks: 1,
  228. enforce:true,
  229. reuseExistingChunk: true
  230. },
  231. comprehensive: {
  232. name: 'chunk-views',
  233. test: resolve('src/views'),
  234. priority: 0,
  235. minSize: 0,
  236. minChunks: 1,
  237. enforce:true,
  238. reuseExistingChunk: true
  239. },
  240. }
  241. })
  242. config.optimization.runtimeChunk('single'),{
  243. from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
  244. to: './', //到根目录下
  245. }
  246. }
  247. }