rspack.base.config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { type RspackPluginFunction, rspack } from '@rspack/core';
  2. import { VueLoaderPlugin } from 'vue-loader';
  3. import path from 'path';
  4. import { isProd } from './plugin/getEnv';
  5. import sassEmbedded from 'sass-embedded';
  6. // 目标浏览器配置
  7. const targets = ['last 2 versions', '> 0.2%', 'not dead', 'Firefox ESR'];
  8. // 基础配置
  9. export const baseConfig = {
  10. entry: {
  11. main: './src/main.ts',
  12. },
  13. resolve: {
  14. extensions: ['...', '.ts', '.vue'],
  15. alias: {
  16. '@': path.resolve(__dirname, './src'),
  17. },
  18. },
  19. module: {
  20. rules: [
  21. {
  22. test: /\.vue$/,
  23. loader: 'vue-loader',
  24. options: {
  25. experimentalInlineMatchResource: true,
  26. },
  27. },
  28. {
  29. test: /\.(js|ts)$/,
  30. use: [
  31. {
  32. loader: 'builtin:swc-loader',
  33. options: {
  34. jsc: {
  35. parser: {
  36. syntax: 'typescript',
  37. },
  38. },
  39. env: { targets },
  40. },
  41. },
  42. ],
  43. },
  44. {
  45. test: /\.jsx$/,
  46. use: {
  47. loader: 'builtin:swc-loader',
  48. options: {
  49. jsc: {
  50. parser: {
  51. syntax: 'ecmascript',
  52. jsx: true,
  53. },
  54. },
  55. },
  56. },
  57. type: 'javascript/auto',
  58. },
  59. {
  60. test: /\.tsx$/,
  61. use: {
  62. loader: 'builtin:swc-loader',
  63. options: {
  64. jsc: {
  65. parser: {
  66. syntax: 'typescript',
  67. tsx: true,
  68. },
  69. },
  70. },
  71. },
  72. type: 'javascript/auto',
  73. },
  74. {
  75. test: /\.svg/,
  76. type: 'asset/resource',
  77. },
  78. {
  79. test: /\.(png|jpe?g|gif)$/i,
  80. type: 'asset/resource',
  81. },
  82. {
  83. oneOf: [
  84. {
  85. test: /\.css$/i,
  86. use: [
  87. isProd() ? rspack.CssExtractRspackPlugin.loader : 'style-loader',
  88. 'css-loader',
  89. ],
  90. type: 'javascript/auto',
  91. },
  92. {
  93. test: /\.(sass|scss)$/,
  94. use: [
  95. isProd() ? rspack.CssExtractRspackPlugin.loader : 'style-loader',
  96. 'css-loader',
  97. {
  98. loader: 'sass-loader',
  99. options: {
  100. api: 'modern-compiler',
  101. implementation: sassEmbedded,
  102. },
  103. },
  104. ],
  105. type: 'javascript/auto',
  106. },
  107. ],
  108. },
  109. ],
  110. },
  111. plugins: [
  112. new rspack.HtmlRspackPlugin({
  113. template: './index.html',
  114. }),
  115. require('unplugin-auto-import/rspack').default({
  116. include: [
  117. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  118. /\.vue$/,
  119. /\.vue\?vue/, // .vue
  120. /\.md$/, // .md
  121. ],
  122. imports: [
  123. 'vue',
  124. 'vue-router',
  125. // 可额外添加需要 autoImport 的组件
  126. {
  127. // '@/hooks/web/useI18n': ['useI18n'],
  128. },
  129. ],
  130. dts: 'src/types/auto-imports.d.ts',
  131. // resolvers: [ElementPlusResolver()],
  132. eslintrc: {
  133. enabled: false, // Default `false`
  134. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  135. globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  136. },
  137. }),
  138. new rspack.DefinePlugin({
  139. // __VUE_OPTIONS_API__: true,
  140. // __VUE_PROD_DEVTOOLS__: false,
  141. API_BASE_URL: JSON.stringify(process.env.API_BASE_URL),
  142. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  143. }),
  144. new VueLoaderPlugin() as RspackPluginFunction,
  145. ],
  146. optimization: {
  147. minimizer: [
  148. new rspack.SwcJsMinimizerRspackPlugin(),
  149. new rspack.LightningCssMinimizerRspackPlugin({
  150. minimizerOptions: { targets },
  151. }),
  152. ],
  153. },
  154. experiments: {
  155. css: true,
  156. },
  157. };