rspack.base.config.ts 4.9 KB

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