vue.config.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const { merge } = require('webpack-merge')
  2. const path = require('path')
  3. const moment = require('moment')
  4. const { defineConfig } = require('@vue/cli-service')
  5. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
  6. const CompressionPlugin = require('compression-webpack-plugin')
  7. const tsImportPluginFactory = require('ts-import-plugin')
  8. const convertPath = (dir) => path.resolve(__dirname, dir)
  9. const outputDir = convertPath('app/dist') // 打包输出目录
  10. module.exports = defineConfig({
  11. transpileDependencies: true,
  12. productionSourceMap: false, // 打包取消.map
  13. publicPath: './',
  14. outputDir,
  15. pages: {
  16. indexPath: {
  17. entry: process.env.VUE_APP_ROOT + 'main.ts',
  18. template: process.env.VUE_APP_ROOT + 'index.html',
  19. filename: 'index.html',
  20. title: process.env.VUE_APP_TITLE,
  21. meta: {
  22. revised: moment(new Date()).format('YYYYMMDDHHmmss') // 打包生成版本号
  23. }
  24. }
  25. },
  26. devServer: {
  27. client: {
  28. overlay: false
  29. }
  30. },
  31. css: {
  32. extract: true, // 是否使用css分离插件 ExtractTextPlugin
  33. sourceMap: false,
  34. loaderOptions: {
  35. postcss: {
  36. postcssOptions: {
  37. config: process.env.VUE_APP_ROOT, // 自定义 postcss.config.js 文件路径,https://webpack.docschina.org/loaders/postcss-loader/#postcss-options
  38. }
  39. }
  40. }
  41. },
  42. chainWebpack: (config) => {
  43. config.resolve.alias
  44. .set('@public', convertPath('public/'))
  45. .set('@pc', convertPath('src/packages/pc/'))
  46. .set('@mobile', convertPath('src/packages/mobile/'))
  47. config.module
  48. .rule('ts')
  49. .use('ts-loader')
  50. .tap((options) => {
  51. options = merge(options, {
  52. transpileOnly: true,
  53. getCustomTransformers: () => ({
  54. before: [
  55. // vant 按需引入
  56. tsImportPluginFactory({
  57. libraryName: 'vant',
  58. libraryDirectory: 'es',
  59. style: true
  60. })
  61. ],
  62. compilerOptions: {
  63. module: 'es2015'
  64. }
  65. })
  66. })
  67. return options;
  68. })
  69. config.plugin('compressionPlugin')
  70. .use(new CompressionPlugin({
  71. test: /\.js$|\.html$|\.css/, // 匹配文件名
  72. threshold: 10240, // 对超过10k的数据压缩
  73. }))
  74. config.plugin('fork-ts-checker')
  75. .use(new ForkTsCheckerWebpackPlugin({
  76. typescript: {
  77. memoryLimit: 4096,
  78. },
  79. }))
  80. },
  81. configureWebpack: (config) => {
  82. config.devtool = 'source-map'
  83. }
  84. })