rspack.base.config.ts 5.6 KB

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