rspack.base.config.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. {
  84. test: /\.svg$/,
  85. include: [path.resolve(__dirname, 'src/assets/svg')],
  86. use: [
  87. {
  88. loader: 'svg-sprite-loader',
  89. options: {
  90. symbolId: 'icon-[name]',
  91. },
  92. },
  93. ],
  94. },
  95. {
  96. test: /\.svg$/,
  97. exclude: [path.resolve(__dirname, 'src/assets/svg')],
  98. type: 'asset/resource',
  99. },
  100. {
  101. test: /\.(png|jpe?g|gif)$/i,
  102. type: 'asset/resource',
  103. },
  104. // 处理CSS文件
  105. {
  106. test: /\.css$/i,
  107. use: [
  108. isProd() ? rspack.CssExtractRspackPlugin.loader : 'style-loader',
  109. 'css-loader',
  110. 'postcss-loader',
  111. ],
  112. },
  113. // 处理SCSS/SASS文件
  114. {
  115. test: /\.(scss|sass)$/i,
  116. use: [
  117. isProd() ? rspack.CssExtractRspackPlugin.loader : 'style-loader',
  118. 'css-loader',
  119. {
  120. loader: 'sass-loader',
  121. options: {
  122. api: 'modern-compiler',
  123. implementation: sassEmbedded,
  124. sassOptions: {
  125. includePaths: [path.resolve(__dirname, '../src/styles')],
  126. },
  127. additionalData: '@use "@/styles/variables.scss" as *;',
  128. },
  129. },
  130. ],
  131. type: 'javascript/auto',
  132. },
  133. ],
  134. },
  135. plugins: [
  136. new rspack.CssExtractRspackPlugin({
  137. filename: '[name].[contenthash].css',
  138. }),
  139. new rspack.HtmlRspackPlugin({
  140. template: './index.html',
  141. title: packageJson.name || '声音AI平台',
  142. // favicon: path.resolve(__dirname, '../public/favicon.ico'),
  143. meta: {
  144. description: '声音AI平台 - 专业的声音处理与分析工具',
  145. keywords: '声音AI,音频处理,AI平台,语音识别',
  146. author: 'Sound AI Team',
  147. },
  148. minify: isProd()
  149. ? {
  150. removeComments: true,
  151. collapseWhitespace: true,
  152. removeRedundantAttributes: true,
  153. useShortDoctype: true,
  154. removeEmptyAttributes: true,
  155. removeStyleLinkTypeAttributes: true,
  156. keepClosingSlash: true,
  157. minifyJS: true,
  158. minifyCSS: true,
  159. minifyURLs: true,
  160. }
  161. : false,
  162. }),
  163. AutoImportPlugin({
  164. include: [
  165. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  166. /\.vue$/,
  167. /\.vue\?vue/, // .vue
  168. /\.md$/, // .md
  169. ],
  170. imports: [
  171. 'vue',
  172. 'vue-router',
  173. // 可额外添加需要 autoImport 的组件
  174. {
  175. // '@/hooks/web/useI18n': ['useI18n'],
  176. axios: [
  177. // default imports
  178. ['default', 'axios'], // import { default as axios } from 'axios',
  179. ],
  180. },
  181. ],
  182. dts: 'src/types/auto-imports.d.ts',
  183. // resolvers: [ElementPlusResolver()],
  184. eslintrc: {
  185. enabled: false, // Default `false`
  186. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  187. globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  188. },
  189. }),
  190. new rspack.DefinePlugin({
  191. __VUE_OPTIONS_API__: 'true',
  192. __VUE_PROD_DEVTOOLS__: 'false',
  193. __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
  194. API_BASE_URL: JSON.stringify(process.env.API_BASE_URL),
  195. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  196. }),
  197. new VueLoaderPlugin() as RspackPluginFunction,
  198. // 添加ESLint插件
  199. new ESLintPlugin({
  200. configType: 'flat',
  201. extensions: ['js', 'jsx', 'ts', 'tsx', 'vue'],
  202. exclude: ['node_modules', 'dist'],
  203. emitWarning: true,
  204. emitError: true,
  205. failOnError: false,
  206. failOnWarning: false,
  207. cache: true,
  208. cacheLocation: path.resolve(
  209. __dirname,
  210. '../node_modules/.cache/.eslintcache',
  211. ),
  212. }),
  213. ],
  214. optimization: {
  215. minimizer: [
  216. new rspack.SwcJsMinimizerRspackPlugin(),
  217. new rspack.LightningCssMinimizerRspackPlugin({
  218. minimizerOptions: { targets },
  219. }),
  220. ],
  221. },
  222. experiments: {
  223. futureDefaults: true,
  224. css: false,
  225. },
  226. };
  227. export default baseConfig;