Skip to content

Commit 37c25bb

Browse files
CristiCebandannyhw
andauthored
fix(deps): declare babel syntax typescript (#1483)
Co-authored-by: Daniel Williams <dannyhyunsoowilliams@gmail.com>
1 parent aed1802 commit 37c25bb

10 files changed

Lines changed: 78 additions & 14 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Fix `Cannot find module '@babel/plugin-syntax-typescript'` when bundling TypeScript with `babel-swc-loader` or the codegen rules. Re.Pack passed the plugin to Babel by name, so it only resolved when the project happened to have it installed. With pnpm, builds often worked only through the `NODE_PATH` set by script shims and failed when bundling from Xcode or Gradle.
6+
7+
- `@babel/plugin-syntax-typescript` is now a dependency of Re.Pack and is passed to Babel as a resolved path.
8+
- `@babel/core` is now declared as a peer dependency, since Re.Pack's loaders import it.

‎apps/tester-federation-v2/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
},
2626
"devDependencies": {
2727
"@babel/core": "^7.29.7",
28-
"@babel/plugin-syntax-typescript": "7.25.4",
2928
"@react-native-community/cli": "catalog:testers",
3029
"@react-native-community/cli-platform-android": "catalog:testers",
3130
"@react-native-community/cli-platform-ios": "catalog:testers",

‎packages/repack-expo/test/public-environment.test.cjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ test('resolves Expo development and production dotenv precedence without exposin
232232
const projectRoot = createEnvironmentProject({
233233
'.env': [
234234
'EXPO_PUBLIC_VALUE=base',
235+
// biome-ignore lint/suspicious/noTemplateCurlyInString: literal dotenv variable expansion under test
235236
'EXPO_PUBLIC_EXPANDED=${SHARED_SUFFIX}-base',
236237
'PRIVATE_TEST_SECRET=private-value-that-must-not-escape',
237238
].join('\n'),

‎packages/repack/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"clang-format:android": "find android/src/ -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i"
6666
},
6767
"peerDependencies": {
68+
"@babel/core": "^7.20.0",
6869
"@module-federation/enhanced": ">=0.6.10",
6970
"@rspack/core": ">=1",
7071
"react-native": ">=0.74",
@@ -82,6 +83,7 @@
8283
}
8384
},
8485
"dependencies": {
86+
"@babel/plugin-syntax-typescript": "^7.25.4",
8587
"@callstack/repack-dev-server": "workspace:*",
8688
"@discoveryjs/json-ext": "^0.5.7",
8789
"@rspack/plugin-react-refresh": "1.0.0",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getExtraBabelPlugins } from '../utils.js';
2+
3+
const syntaxTypeScriptPlugin = require.resolve(
4+
'@babel/plugin-syntax-typescript'
5+
);
6+
7+
describe('getExtraBabelPlugins', () => {
8+
it('adds the resolved TS syntax plugin for .ts files', () => {
9+
expect(getExtraBabelPlugins('/virtual/file.ts')).toEqual([
10+
[syntaxTypeScriptPlugin, { isTSX: false, allowNamespaces: true }],
11+
]);
12+
});
13+
14+
it('adds the resolved TSX syntax plugin for .tsx files', () => {
15+
expect(getExtraBabelPlugins('/virtual/file.tsx')).toEqual([
16+
[syntaxTypeScriptPlugin, { isTSX: true, allowNamespaces: true }],
17+
]);
18+
});
19+
20+
it('adds no plugins for other files', () => {
21+
expect(getExtraBabelPlugins('/virtual/file.js')).toEqual([]);
22+
});
23+
});

‎packages/repack/src/loaders/babelSwcLoader/utils.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import { importDefaultESM } from '../../helpers/index.js';
1010
type Swc = (typeof experiments)['swc'];
1111
type Logger = ReturnType<LoaderContext['getLogger']>;
1212

13+
const syntaxTypeScriptPlugin = require.resolve(
14+
'@babel/plugin-syntax-typescript'
15+
);
16+
1317
export function isTypeScriptSource(fileName: string) {
1418
return !!fileName && fileName.endsWith('.ts');
1519
}
@@ -34,12 +38,12 @@ export function getExtraBabelPlugins(filename: string) {
3438
// which includes the syntax-typescript plugin
3539
if (isTypeScriptSource(filename)) {
3640
extraBabelPlugins.push([
37-
'@babel/plugin-syntax-typescript',
41+
syntaxTypeScriptPlugin,
3842
{ isTSX: false, allowNamespaces: true },
3943
]);
4044
} else if (isTSXSource(filename)) {
4145
extraBabelPlugins.push([
42-
'@babel/plugin-syntax-typescript',
46+
syntaxTypeScriptPlugin,
4347
{ isTSX: true, allowNamespaces: true },
4448
]);
4549
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { getCodegenTransformRules } from '../getCodegenTransformRules.js';
2+
3+
const syntaxTypeScriptPlugin = require.resolve(
4+
'@babel/plugin-syntax-typescript'
5+
);
6+
7+
describe('getCodegenTransformRules', () => {
8+
const [rule] = getCodegenTransformRules();
9+
const { plugins, overrides } = rule.use.options;
10+
const getOverridePlugins = (file: string) =>
11+
overrides.find((override) => override.test.test(file))?.plugins;
12+
13+
it('references the resolved TS syntax plugin', () => {
14+
expect(plugins).toContainEqual([syntaxTypeScriptPlugin, false]);
15+
expect(getOverridePlugins('NativeFoo.ts')).toEqual([
16+
[syntaxTypeScriptPlugin, { isTSX: false, allowNamespaces: true }],
17+
]);
18+
expect(getOverridePlugins('FooNativeComponent.tsx')).toEqual([
19+
[syntaxTypeScriptPlugin, { isTSX: true, allowNamespaces: true }],
20+
]);
21+
});
22+
});

‎packages/repack/src/utils/getCodegenTransformRules.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* @returns Array of module rules
66
*/
77
export function getCodegenTransformRules() {
8+
const syntaxTypeScriptPlugin = require.resolve(
9+
'@babel/plugin-syntax-typescript'
10+
);
11+
812
return [
913
{
1014
type: 'javascript/auto',
@@ -21,7 +25,7 @@ export function getCodegenTransformRules() {
2125
},
2226
plugins: [
2327
'@callstack/repack/babel-plugin-syntax-react-native',
24-
['@babel/plugin-syntax-typescript', false],
28+
[syntaxTypeScriptPlugin, false],
2529
'@react-native/babel-plugin-codegen',
2630
],
2731
// config merging reference: https://babeljs.io/docs/options#pluginpreset-entries
@@ -30,7 +34,7 @@ export function getCodegenTransformRules() {
3034
test: /\.ts$/,
3135
plugins: [
3236
[
33-
'@babel/plugin-syntax-typescript',
37+
syntaxTypeScriptPlugin,
3438
{ isTSX: false, allowNamespaces: true },
3539
],
3640
],
@@ -39,7 +43,7 @@ export function getCodegenTransformRules() {
3943
test: /\.tsx$/,
4044
plugins: [
4145
[
42-
'@babel/plugin-syntax-typescript',
46+
syntaxTypeScriptPlugin,
4347
{ isTSX: true, allowNamespaces: true },
4448
],
4549
],

‎pnpm-lock.yaml‎

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

‎tests/integration/src/loaders/react-native-parser.test.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ function createFixtureProject(
8787

8888
linkPackage(projectRoot, '@callstack/repack', repackRoot);
8989
linkPackage(projectRoot, '@babel/runtime', babelRuntimeRoot);
90-
for (const name of [
90+
linkPackage(
91+
projectRoot,
9192
'@react-native/babel-plugin-codegen',
92-
'@babel/plugin-syntax-typescript',
93-
]) {
94-
linkPackage(projectRoot, name, resolvePresetPackage(name));
95-
}
93+
resolvePresetPackage('@react-native/babel-plugin-codegen')
94+
);
95+
// '@babel/plugin-syntax-typescript' is intentionally not linked: Re.Pack
96+
// resolves it from its own dependencies, as with isolated (e.g. pnpm) installs.
9697
// Webpack needs a resolvable SWC to exercise the SWC path instead of Babel.
9798
linkPackage(projectRoot, '@swc/core', swcRoot);
9899
}

0 commit comments

Comments
 (0)