vue.config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const path = require('path')
  2. const webpack = require('webpack')
  3. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  4. const GenerateAssetPlugin = require('generate-asset-webpack-plugin')
  5. const Timestamp = new Date().getTime()
  6. const createServerConfig = function (compilation) {
  7. let config = { version: new Date().getTime() }
  8. return JSON.stringify(config)
  9. }
  10. const resolve = dir => {
  11. return path.join(__dirname, dir)
  12. }
  13. // 项目部署基础
  14. // 默认情况下,我们假设你的应用将被部署在域的根目录下,
  15. // 例如:https://www.my-app.com/
  16. // 默认:'/'
  17. // 如果您的应用程序部署在子路径中,则需要在这指定子路径
  18. // 例如:https://www.foobar.com/my-app/
  19. // 需要将它改为'/my-app/'
  20. // iview-admin线上演示打包路径: https://file.iviewui.com/admin-dist/
  21. const BASE_URL = process.env.NODE_ENV === 'production'
  22. ? '/'
  23. : '/';
  24. const ip = 'http://36.134.91.96:10001/api'
  25. //const ip = 'https://cfm.bytesail.cn/api'
  26. const ip2 = 'http://120.79.80.64:80501111'
  27. module.exports = {
  28. devServer: {
  29. port: "8081", //代理端口
  30. open: false, //项目启动时是否自动打开浏览器,我这里设置为false,不打开,true表示打开
  31. headers: {
  32. 'Access-Control-Allow-Origin': '*',
  33. },
  34. proxy: {
  35. '/cloudApi': {
  36. target: ip2,
  37. ws: true,
  38. pathRewrite: {
  39. '^/api': '/'
  40. }
  41. },
  42. '/api': {
  43. //本地服务接口地址
  44. target: ip,
  45. //远程演示服务地址,可用于直接启动项目
  46. //target: 'https://saber.bladex.vip/api',
  47. ws: true,
  48. pathRewrite: {
  49. '^/api': '/'
  50. }
  51. },
  52. }
  53. },
  54. // Project deployment base
  55. // By default we assume your app will be deployed at the root of a domain,
  56. // e.g. https://www.my-app.com/
  57. // If your app is deployed at a sub-path, you will need to specify that
  58. // sub-path here. For example, if your app is deployed at
  59. // https://www.foobar.com/my-app/
  60. // then change this to '/my-app/'
  61. publicPath: BASE_URL,
  62. // tweak internal webpack configuration.
  63. // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  64. // 如果你不需要使用eslint,把lintOnSave设为false即可
  65. lintOnSave: true,
  66. chainWebpack: config => {
  67. config.resolve.alias
  68. .set('@', resolve('src')) // key,value自行定义,比如.set('@@', resolve('src/components'))
  69. .set('_c', resolve('src/components'))
  70. },
  71. // 设为false打包时不生成.map文件
  72. productionSourceMap: false,
  73. configureWebpack: config => {
  74. // 开发环境不需要gzip 生产环境时清空所有console
  75. if (process.env.NODE_ENV !== 'production') return
  76. else config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
  77. config.plugins.push(
  78. new CompressionWebpackPlugin({
  79. // 正在匹配需要压缩的文件后缀
  80. test: /\.(js|css|svg|woff|ttf|json|html)$/,
  81. // 大于10kb的会压缩
  82. threshold: 10240
  83. // 其余配置查看compression-webpack-plugin
  84. })
  85. )
  86. config.plugins.push(
  87. new GenerateAssetPlugin({
  88. filename: 'version.json',
  89. fn: (compilation, cb) => {
  90. cb(null, createServerConfig(compilation))
  91. },
  92. extraFiles: []
  93. })
  94. )
  95. config.plugins.push(
  96. new webpack.ProvidePlugin({
  97. $: 'jquery',
  98. jQuery: 'jquery',
  99. 'window.jQuery': 'jquery'
  100. })
  101. )
  102. config.output.filename = `js/[name].${Timestamp}.js`
  103. config.output.chunkFilename = `js/[name].${Timestamp}.js`
  104. }
  105. // 这里写你调用接口的基础路径,来解决跨域,如果设置了代理,那你本地开发环境的axios的baseUrl要写为 '' ,即空字符串
  106. // devServer: {
  107. // proxy: 'localhost:3000'
  108. // }
  109. }