rspack.base.config.ts 5.6 KB

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