Skip to content

Commit 87db9a1

Browse files
whydidoodannyhw
andauthored
fix(repack-plugin-nativewind): configure SWC rules nested in oneOf (#1494)
Co-authored-by: Daniel Williams <dannyhyunsoowilliams@gmail.com>
1 parent 37c25bb commit 87db9a1

6 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎.changeset/nativewind-swc-oneof.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack-plugin-nativewind": patch
3+
---
4+
5+
Configure the NativeWind JSX import source for `builtin:swc-loader` rules nested in `oneOf` or `rules`, such as the ones from `getJsTransformRules`. Previously only top-level rules were updated, so JSX kept importing from `react`.

‎packages/plugin-nativewind/src/plugin.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
Compiler as RspackCompiler,
3+
RuleSetRules,
34
RuleSetUse,
45
RuleSetUseItem,
56
SwcLoaderOptions,
@@ -154,7 +155,11 @@ export class NativeWindPlugin {
154155
*
155156
* TODO made obsolete by the new babel-swc-loader, remove in 6.0
156157
*/
157-
compiler.options.module.rules.forEach((rule) => {
158+
this.configureRules(compiler.options.module.rules);
159+
}
160+
161+
private configureRules(rules: RuleSetRules | undefined) {
162+
rules?.forEach((rule) => {
158163
if (!rule || typeof rule !== 'object') {
159164
return;
160165
}
@@ -168,6 +173,10 @@ export class NativeWindPlugin {
168173
rule.options as SwcLoaderOptions
169174
);
170175
}
176+
177+
// e.g. `getJsTransformRules` nests its `builtin:swc-loader` rules in `oneOf`
178+
this.configureRules(rule.oneOf);
179+
this.configureRules(rule.rules);
171180
});
172181
}
173182
}

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/integration/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"devDependencies": {
1515
"@babel/runtime": "^7.29.7",
1616
"@callstack/repack": "workspace:*",
17+
"@callstack/repack-plugin-nativewind": "workspace:*",
1718
"@module-federation/enhanced": "2.0.1",
1819
"@module-federation/enhanced-v15": "npm:@module-federation/enhanced@0.15.0",
1920
"@module-federation/enhanced-v21": "npm:@module-federation/enhanced@0.21.0",

‎tests/integration/src/helpers.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type {
22
Compiler as RspackCompiler,
33
Configuration as RspackConfiguration,
4+
RuleSetRule,
5+
RuleSetUseItem,
46
} from '@rspack/core';
57
import { createFsFromVolume, Volume } from 'memfs';
68
import { inject } from 'vitest';
@@ -112,3 +114,32 @@ export function getReactNativeVirtualModules(
112114
};`,
113115
};
114116
}
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import path from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { getJsTransformRules } from '@callstack/repack';
4+
import { NativeWindPlugin } from '@callstack/repack-plugin-nativewind';
5+
import { describe, expect, inject, it } from 'vitest';
6+
import { collectSwcLoaderOptions, createCompiler } from '../helpers.js';
7+
8+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
9+
10+
// builtin:swc-loader only exists in Rspack
11+
describe.runIf(inject('bundlerType') === 'rspack')('NativeWindPlugin', () => {
12+
it('uses the NativeWind JSX import source in getJsTransformRules', async () => {
13+
const compiler = await createCompiler({
14+
context: __dirname,
15+
mode: 'development',
16+
entry: './index.js',
17+
output: { path: '/out' },
18+
module: { rules: getJsTransformRules() },
19+
plugins: [new NativeWindPlugin({ checkDependencies: false })],
20+
});
21+
22+
const importSources = collectSwcLoaderOptions(
23+
compiler.options.module.rules
24+
).map((options) => options.jsc?.transform?.react?.importSource);
25+
26+
expect(importSources.length).toBeGreaterThan(0);
27+
expect(new Set(importSources)).toEqual(new Set(['nativewind']));
28+
});
29+
});

0 commit comments

Comments
 (0)