vue.config.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. loaderOptions: {
  27. postcss: {
  28. postcssOptions: {
  29. config: process.env.VUE_APP_ROOT, // 自定义 postcss.config.js 文件路径,https://webpack.docschina.org/loaders/postcss-loader/#postcss-options
  30. }
  31. }
  32. }
  33. },
  34. chainWebpack: (config) => {
  35. config.resolve.alias
  36. .set('@' + process.env.VUE_APP_ENV, resolve(__dirname, process.env.VUE_APP_ROOT))
  37. config.module
  38. .rule('ts')
  39. .use('ts-loader')
  40. .tap((options) => {
  41. options = merge(options, {
  42. transpileOnly: true,
  43. getCustomTransformers: () => ({
  44. before: [
  45. // vant 按需引入
  46. tsImportPluginFactory({
  47. libraryName: 'vant',
  48. libraryDirectory: 'es',
  49. style: true
  50. })
  51. ],
  52. compilerOptions: {
  53. module: 'es2015'
  54. }
  55. })
  56. })
  57. return options;
  58. })
  59. config.plugin('compressionPlugin')
  60. .use(new CompressionPlugin({
  61. test: /\.js$|\.html$|\.css/, // 匹配文件名
  62. threshold: 10240, // 对超过10k的数据压缩
  63. }))
  64. config.plugin('fork-ts-checker')
  65. .use(new ForkTsCheckerWebpackPlugin({
  66. typescript: {
  67. memoryLimit: 4096,
  68. },
  69. }))
  70. }
  71. })