rspack.base.config.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. AutoImportPlugin({
  138. include: [
  139. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  140. /\.vue$/,
  141. /\.vue\?vue/, // .vue
  142. /\.md$/, // .md
  143. ],
  144. imports: [
  145. 'vue',
  146. 'vue-router',
  147. // 可额外添加需要 autoImport 的组件
  148. {
  149. // '@/hooks/web/useI18n': ['useI18n'],
  150. axios: [
  151. // default imports
  152. ['default', 'axios'], // import { default as axios } from 'axios',
  153. ],
  154. },
  155. ],
  156. dts: 'src/types/auto-imports.d.ts',
  157. // resolvers: [ElementPlusResolver()],
  158. eslintrc: {
  159. enabled: false, // Default `false`
  160. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  161. globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  162. },
  163. }),
  164. new rspack.DefinePlugin({
  165. // __VUE_OPTIONS_API__: true,
  166. // __VUE_PROD_DEVTOOLS__: false,
  167. API_BASE_URL: JSON.stringify(process.env.API_BASE_URL),
  168. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  169. }),
  170. new VueLoaderPlugin() as RspackPluginFunction,
  171. // 添加ESLint插件
  172. new ESLintPlugin({
  173. configType: 'flat',
  174. extensions: ['js', 'jsx', 'ts', 'tsx', 'vue'],
  175. exclude: ['node_modules', 'dist'],
  176. emitWarning: true,
  177. emitError: true,
  178. failOnError: false,
  179. failOnWarning: false,
  180. cache: true,
  181. cacheLocation: path.resolve(
  182. __dirname,
  183. 'node_modules/.cache/.eslintcache',
  184. ),
  185. }),
  186. ],
  187. optimization: {
  188. minimizer: [
  189. new rspack.SwcJsMinimizerRspackPlugin(),
  190. new rspack.LightningCssMinimizerRspackPlugin({
  191. minimizerOptions: { targets },
  192. }),
  193. ],
  194. },
  195. experiments: {
  196. futureDefaults: true,
  197. css: false,
  198. },
  199. };