Skip to content

Commit 13b65df

Browse files
committed
fix(@angular/build): allow direct bundling of TSX files with application builder
When using the application builder with `isolatedModules`, the bundler will directly handle TypeScript transpilation and bundling. Previously, any input TSX files were loaded by the bundler as TS files. This difference caused errors when attempting to process the files since the syntax differs between TSX and TS. The appropriate loader will now be used if the input file is TSX in this situation.
1 parent b6951f4 commit 13b65df

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

packages/angular/build/src/builders/application/tests/behavior/typescript-isolated-modules_spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,33 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
4747

4848
expect(result?.success).toBe(true);
4949
});
50+
51+
it('supports TSX files with isolated modules enabled and enabled optimizations', async () => {
52+
// Enable tsconfig isolatedModules option in tsconfig
53+
await harness.modifyFile('tsconfig.json', (content) => {
54+
const tsconfig = JSON.parse(content);
55+
tsconfig.compilerOptions.isolatedModules = true;
56+
tsconfig.compilerOptions.jsx = 'react-jsx';
57+
58+
return JSON.stringify(tsconfig);
59+
});
60+
61+
await harness.writeFile('src/types.d.ts', `declare module 'react/jsx-runtime' { jsx: any }`);
62+
await harness.writeFile('src/abc.tsx', `export function hello() { return <h1>Hello</h1>; }`);
63+
await harness.modifyFile(
64+
'src/main.ts',
65+
(content) => content + `import { hello } from './abc'; console.log(hello());`,
66+
);
67+
68+
harness.useTarget('build', {
69+
...BASE_OPTIONS,
70+
optimization: true,
71+
externalDependencies: ['react'],
72+
});
73+
74+
const { result } = await harness.executeOnce();
75+
76+
expect(result?.success).toBe(true);
77+
});
5078
});
5179
});

packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import type {
1010
BuildFailure,
11+
Loader,
1112
Metafile,
1213
OnStartResult,
1314
OutputFile,
@@ -461,9 +462,21 @@ export function createCompilerPlugin(
461462
typeScriptFileCache.set(request, contents);
462463
}
463464

465+
let loader: Loader;
466+
if (useTypeScriptTranspilation || isJS) {
467+
// TypeScript has transpiled to JS or is already JS
468+
loader = 'js';
469+
} else if (request.at(-1) === 'x') {
470+
// TSX and TS have different syntax rules. Only set if input is a TSX file.
471+
loader = 'tsx';
472+
} else {
473+
// Otherwise, directly bundle TS
474+
loader = 'ts';
475+
}
476+
464477
return {
465478
contents,
466-
loader: useTypeScriptTranspilation || isJS ? 'js' : 'ts',
479+
loader,
467480
};
468481
});
469482

0 commit comments

Comments
 (0)