vue.config.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const path = require('path');
  2. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  3. const CopyPlugin = require('copy-webpack-plugin');
  4. const isProduction = process.env.NODE_ENV === 'production';
  5. module.exports = {
  6. chainWebpack: (config) => {
  7. // 移除 prefetch 插件
  8. config.plugins.delete('prefetch');
  9. },
  10. assetsDir: 'assets',
  11. productionSourceMap: !isProduction,
  12. // 暂时没有找到publicPath的单独配置,无法解决CDN的问题
  13. css: {
  14. sourceMap: true,
  15. },
  16. // lintOnSave: 'error',
  17. lintOnSave: false,
  18. // 避免静态资源请求时附带Cookie,最终目的是减少资源请求包的大小
  19. crossorigin: 'anonymous',
  20. // 保证资源的安全性,避免劫持后的安全问题
  21. integrity: true,
  22. devServer: {
  23. // 启动且第一次构建完成时,自动用我们的系统的默认浏览器去打开要开发的网页
  24. open: true,
  25. disableHostCheck: true,
  26. proxy: {
  27. '/phpapi': {
  28. target: process.env.VUE_APP_PHP_URL,
  29. changeOrigin: true,
  30. pathRewrite: { '^/phpapi': '' },
  31. // 另外的细节
  32. // 浏览器对Port的处理存在差异,这是同源策略中的灰色地带,即:80端口的Cookie在Chrome等其他浏览器中会发送到:8080,而Firefox不会)。
  33. // cookieDomainRewrite: '',
  34. // secure: false,
  35. },
  36. },
  37. },
  38. configureWebpack: {
  39. module: {
  40. rules: [
  41. {
  42. test: /\.s[ac]ss$/i,
  43. use: [
  44. // Compiles Sass to CSS
  45. 'sass-loader',
  46. ],
  47. },
  48. {
  49. test: /\.js$/,
  50. loader: ['babel-loader', 'eslint-loader'],
  51. exclude: file => (/node_modules/.test(file)),
  52. },
  53. ],
  54. },
  55. plugins: [
  56. new CopyPlugin([
  57. { from: path.resolve(__dirname, '.htaccess'), to: path.resolve(__dirname, 'dist') },
  58. ]),
  59. ],
  60. resolve: {
  61. mainFields: ['browser', 'module', 'main'],
  62. alias: {
  63. '@': path.resolve(__dirname, 'src'),
  64. },
  65. },
  66. optimization: {
  67. minimizer: [
  68. new UglifyJsPlugin({
  69. uglifyOptions: {
  70. warnings: false,
  71. compress: {
  72. drop_console: true, // console
  73. drop_debugger: true,
  74. pure_funcs: ['console.log'], // 移除console
  75. },
  76. output: {
  77. beautify: false, // 最紧凑的输出,不保留空格和制表符
  78. comments: false, // 删除所有注释
  79. },
  80. },
  81. sourceMap: false,
  82. parallel: true,
  83. }),
  84. ],
  85. },
  86. },
  87. };