vue.config.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. module.exports = {
  27. devServer: {
  28. port: "8081", //代理端口
  29. open: false, //项目启动时是否自动打开浏览器,我这里设置为false,不打开,true表示打开
  30. headers: {
  31. 'Access-Control-Allow-Origin': '*',
  32. },
  33. proxy: {
  34. '/api': {
  35. //本地服务接口地址
  36. target: ip,
  37. //远程演示服务地址,可用于直接启动项目
  38. //target: 'https://saber.bladex.vip/api',
  39. ws: true,
  40. pathRewrite: {
  41. '^/api': '/'
  42. }
  43. },
  44. }
  45. },
  46. // Project deployment base
  47. // By default we assume your app will be deployed at the root of a domain,
  48. // e.g. https://www.my-app.com/
  49. // If your app is deployed at a sub-path, you will need to specify that
  50. // sub-path here. For example, if your app is deployed at
  51. // https://www.foobar.com/my-app/
  52. // then change this to '/my-app/'
  53. publicPath: BASE_URL,
  54. // tweak internal webpack configuration.
  55. // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  56. // 如果你不需要使用eslint,把lintOnSave设为false即可
  57. lintOnSave: true,
  58. chainWebpack: config => {
  59. config.resolve.alias
  60. .set('@', resolve('src')) // key,value自行定义,比如.set('@@', resolve('src/components'))
  61. .set('_c', resolve('src/components'))
  62. },
  63. // 设为false打包时不生成.map文件
  64. productionSourceMap: false,
  65. configureWebpack: config => {
  66. // 开发环境不需要gzip 生产环境时清空所有console
  67. if (process.env.NODE_ENV !== 'production') return
  68. else config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
  69. config.plugins.push(
  70. new CompressionWebpackPlugin({
  71. // 正在匹配需要压缩的文件后缀
  72. test: /\.(js|css|svg|woff|ttf|json|html)$/,
  73. // 大于10kb的会压缩
  74. threshold: 10240
  75. // 其余配置查看compression-webpack-plugin
  76. })
  77. )
  78. config.plugins.push(
  79. new GenerateAssetPlugin({
  80. filename: 'version.json',
  81. fn: (compilation, cb) => {
  82. cb(null, createServerConfig(compilation))
  83. },
  84. extraFiles: []
  85. })
  86. )
  87. config.plugins.push(
  88. new webpack.ProvidePlugin({
  89. $: 'jquery',
  90. jQuery: 'jquery',
  91. 'window.jQuery': 'jquery'
  92. })
  93. )
  94. config.output.filename = `js/[name].${Timestamp}.js`
  95. config.output.chunkFilename = `js/[name].${Timestamp}.js`
  96. }
  97. // 这里写你调用接口的基础路径,来解决跨域,如果设置了代理,那你本地开发环境的axios的baseUrl要写为 '' ,即空字符串
  98. // devServer: {
  99. // proxy: 'localhost:3000'
  100. // }
  101. }