vue.config.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const { merge } = require('webpack-merge')
  2. const { resolve } = require('path')
  3. const moment = require('moment')
  4. const { defineConfig } = require('@vue/cli-service')
  5. const CompressionPlugin = require('compression-webpack-plugin')
  6. const tsImportPluginFactory = require('ts-import-plugin')
  7. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
  8. module.exports = defineConfig({
  9. transpileDependencies: true,
  10. productionSourceMap: false, // 打包取消.map
  11. publicPath: './',
  12. pages: {
  13. indexPath: {
  14. entry: process.env.VUE_APP_ROOT + 'main.ts',
  15. template: process.env.VUE_APP_ROOT + 'index.html',
  16. filename: 'index.html',
  17. title: process.env.VUE_APP_TITLE,
  18. meta: {
  19. revised: moment(new Date()).format('YYYYMMDDHHmmss') // 打包生成版本号
  20. }
  21. }
  22. },
  23. css: {
  24. extract: true, // 是否使用css分离插件 ExtractTextPlugin
  25. sourceMap: false,
  26. },
  27. chainWebpack: (config) => {
  28. config.resolve.alias
  29. .set('@' + process.env.VUE_APP_ENV, resolve(__dirname, process.env.VUE_APP_ROOT))
  30. config.module
  31. .rule('ts')
  32. .use('ts-loader')
  33. .tap((options) => {
  34. options = merge(options, {
  35. transpileOnly: true,
  36. getCustomTransformers: () => ({
  37. before: [
  38. // vant 按需引入
  39. tsImportPluginFactory({
  40. libraryName: 'vant',
  41. libraryDirectory: 'es',
  42. style: true
  43. })
  44. ],
  45. compilerOptions: {
  46. module: 'es2015'
  47. }
  48. })
  49. })
  50. return options;
  51. })
  52. config.plugin('compressionPlugin')
  53. .use(new CompressionPlugin({
  54. test: /\.js$|\.html$|\.css/, // 匹配文件名
  55. threshold: 10240, // 对超过10k的数据压缩
  56. }))
  57. config.plugin('fork-ts-checker')
  58. .use(new ForkTsCheckerWebpackPlugin({
  59. typescript: {
  60. memoryLimit: 4096,
  61. },
  62. }))
  63. }
  64. })