rspack.base.config.ts 4.3 KB

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