rspack.base.config.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. import packageJson from '../package.json';
  11. // 目标浏览器配置
  12. const targets = ['last 2 versions', '> 0.2%', 'not dead', 'Firefox ESR'];
  13. // 基础配置
  14. export const baseConfig = {
  15. entry: {
  16. main: './src/main.ts',
  17. },
  18. resolve: {
  19. extensions: ['...', '.ts', '.vue'],
  20. alias: {
  21. '@': path.resolve(__dirname, '../src'),
  22. },
  23. },
  24. module: {
  25. rules: [
  26. {
  27. test: /\.vue$/,
  28. loader: 'vue-loader',
  29. options: {
  30. experimentalInlineMatchResource: true,
  31. },
  32. },
  33. {
  34. test: /\.(js|ts)$/,
  35. use: [
  36. {
  37. loader: 'builtin:swc-loader',
  38. options: {
  39. jsc: {
  40. parser: {
  41. syntax: 'typescript',
  42. },
  43. },
  44. env: { targets },
  45. },
  46. },
  47. ],
  48. },
  49. {
  50. test: /\.jsx$/,
  51. use: {
  52. loader: 'builtin:swc-loader',
  53. options: {
  54. jsc: {
  55. parser: {
  56. syntax: 'ecmascript',
  57. jsx: true,
  58. },
  59. },
  60. },
  61. },
  62. type: 'javascript/auto',
  63. },
  64. {
  65. test: /\.tsx$/,
  66. use: {
  67. loader: 'builtin:swc-loader',
  68. options: {
  69. jsc: {
  70. parser: {
  71. syntax: 'typescript',
  72. tsx: true,
  73. },
  74. },
  75. },
  76. },
  77. type: 'javascript/auto',
  78. },
  79. {
  80. test: /\.d\.ts$/,
  81. loader: 'ignore-loader',
  82. },
  83. // SVG处理规则
  84. {
  85. test: /\.svg$/,
  86. exclude: [path.resolve(__dirname, '../src/assets/icons')],
  87. type: 'asset/resource',
  88. },
  89. {
  90. test: /\.svg$/,
  91. include: [path.resolve(__dirname, '../src/assets/icons')],
  92. use: [
  93. {
  94. loader: 'svg-sprite-loader',
  95. options: {
  96. symbolId: 'icon-[name]',
  97. },
  98. },
  99. ],
  100. },
  101. {
  102. test: /\.(png|jpe?g|gif)$/i,
  103. type: 'asset/resource',
  104. },
  105. // 处理CSS文件
  106. {
  107. test: /\.css$/i,
  108. use: [
  109. isProd() ? rspack.CssExtractRspackPlugin.loader : 'style-loader',
  110. 'css-loader',
  111. 'postcss-loader',
  112. ],
  113. },
  114. // 处理SCSS/SASS文件
  115. {
  116. test: /\.(scss|sass)$/i,
  117. use: [
  118. isProd() ? rspack.CssExtractRspackPlugin.loader : 'style-loader',
  119. 'css-loader',
  120. {
  121. loader: 'sass-loader',
  122. options: {
  123. api: 'modern-compiler',
  124. implementation: sassEmbedded,
  125. sassOptions: {
  126. includePaths: [path.resolve(__dirname, '../src/styles')],
  127. },
  128. additionalData: '@use "@/styles/variables.scss" as *;',
  129. },
  130. },
  131. ],
  132. type: 'javascript/auto',
  133. },
  134. ],
  135. },
  136. plugins: [
  137. new rspack.CssExtractRspackPlugin({
  138. filename: '[name].[contenthash].css',
  139. }),
  140. new rspack.HtmlRspackPlugin({
  141. template: './index.html',
  142. title: packageJson.name || '声音AI平台',
  143. // favicon: path.resolve(__dirname, '../public/favicon.ico'),
  144. meta: {
  145. description: '声音AI平台 - 专业的声音处理与分析工具',
  146. keywords: '声音AI,音频处理,AI平台,语音识别',
  147. author: 'Sound AI Team',
  148. },
  149. minify: isProd()
  150. ? {
  151. removeComments: true,
  152. collapseWhitespace: true,
  153. removeRedundantAttributes: true,
  154. useShortDoctype: true,
  155. removeEmptyAttributes: true,
  156. removeStyleLinkTypeAttributes: true,
  157. keepClosingSlash: true,
  158. minifyJS: true,
  159. minifyCSS: true,
  160. minifyURLs: true,
  161. }
  162. : false,
  163. }),
  164. AutoImportPlugin({
  165. include: [
  166. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  167. /\.vue$/,
  168. /\.vue\?vue/, // .vue
  169. /\.md$/, // .md
  170. ],
  171. imports: [
  172. 'vue',
  173. 'vue-router',
  174. // 可额外添加需要 autoImport 的组件
  175. {
  176. // '@/hooks/web/useI18n': ['useI18n'],
  177. axios: [
  178. // default imports
  179. ['default', 'axios'], // import { default as axios } from 'axios',
  180. ],
  181. },
  182. ],
  183. dts: 'src/types/auto-imports.d.ts',
  184. // resolvers: [ElementPlusResolver()],
  185. eslintrc: {
  186. enabled: false, // Default `false`
  187. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  188. globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  189. },
  190. }),
  191. new rspack.DefinePlugin({
  192. __VUE_OPTIONS_API__: 'true',
  193. __VUE_PROD_DEVTOOLS__: 'false',
  194. __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
  195. API_BASE_URL: JSON.stringify(process.env.API_BASE_URL),
  196. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  197. }),
  198. new VueLoaderPlugin() as RspackPluginFunction,
  199. // 添加ESLint插件
  200. new ESLintPlugin({
  201. configType: 'flat',
  202. extensions: ['js', 'jsx', 'ts', 'tsx', 'vue'],
  203. exclude: ['node_modules', 'dist'],
  204. emitWarning: true,
  205. emitError: true,
  206. failOnError: false,
  207. failOnWarning: false,
  208. cache: true,
  209. cacheLocation: path.resolve(
  210. __dirname,
  211. '../node_modules/.cache/.eslintcache',
  212. ),
  213. }),
  214. ],
  215. optimization: {
  216. minimizer: [
  217. new rspack.SwcJsMinimizerRspackPlugin(),
  218. new rspack.LightningCssMinimizerRspackPlugin({
  219. minimizerOptions: { targets },
  220. }),
  221. ],
  222. },
  223. experiments: {
  224. futureDefaults: true,
  225. css: false,
  226. },
  227. };
  228. export default baseConfig;