|
1 | 1 | import type { |
2 | 2 | Compiler as RspackCompiler, |
3 | 3 | Configuration as RspackConfiguration, |
| 4 | + RuleSetRule, |
| 5 | + RuleSetUseItem, |
4 | 6 | } from '@rspack/core'; |
5 | 7 | import { createFsFromVolume, Volume } from 'memfs'; |
6 | 8 | import { inject } from 'vitest'; |
@@ -112,3 +114,32 @@ export function getReactNativeVirtualModules( |
112 | 114 | };`, |
113 | 115 | }; |
114 | 116 | } |
| 117 | + |
| 118 | +export type SwcLoaderOptions = { |
| 119 | + jsc?: { |
| 120 | + transform?: { |
| 121 | + react?: { development?: boolean; importSource?: string }; |
| 122 | + }; |
| 123 | + }; |
| 124 | +}; |
| 125 | + |
| 126 | +/** |
| 127 | + * Collect options of every `builtin:swc-loader` in `module.rules`, |
| 128 | + * including nested `oneOf` and `rules` |
| 129 | + */ |
| 130 | +export function collectSwcLoaderOptions(rules: unknown[]): SwcLoaderOptions[] { |
| 131 | + return rules.flatMap((rule) => { |
| 132 | + if (!rule || typeof rule !== 'object') return []; |
| 133 | + const { use, loader, options, oneOf, rules: nested } = rule as RuleSetRule; |
| 134 | + const uses = [use].flat().filter(Boolean) as RuleSetUseItem[]; |
| 135 | + return [ |
| 136 | + ...(loader === 'builtin:swc-loader' ? [options as SwcLoaderOptions] : []), |
| 137 | + ...uses |
| 138 | + .filter((item) => typeof item === 'object') |
| 139 | + .filter((item) => item.loader === 'builtin:swc-loader') |
| 140 | + .map((item) => (item as { options: SwcLoaderOptions }).options), |
| 141 | + ...collectSwcLoaderOptions(oneOf ?? []), |
| 142 | + ...collectSwcLoaderOptions(nested ?? []), |
| 143 | + ]; |
| 144 | + }); |
| 145 | +} |
0 commit comments