vue.config.js 3.0 KB

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