Skip to content

Commit ab4e77c

Browse files
committed
fix(@angular/build): allow .json file replacements with application builds
When using the `application` builder, the `fileReplacements` option will now work as it previous did with the `browser` builder when replacing JSON files. (cherry picked from commit c81dd81)
1 parent d261246 commit ab4e77c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { buildApplication } from '../../index';
10+
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
11+
12+
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
13+
describe('Option: "fileReplacements"', () => {
14+
it('should replace JSON files', async () => {
15+
harness.useTarget('build', {
16+
...BASE_OPTIONS,
17+
fileReplacements: [{ replace: './src/one.json', with: './src/two.json' }],
18+
});
19+
20+
await harness.modifyFile('tsconfig.json', (content) => {
21+
const tsconfig = JSON.parse(content);
22+
tsconfig.compilerOptions.resolveJsonModule = true;
23+
24+
return JSON.stringify(tsconfig);
25+
});
26+
27+
await harness.writeFile('./src/one.json', '{ "x": 12345 }');
28+
await harness.writeFile('./src/two.json', '{ "x": 67890 }');
29+
await harness.writeFile('src/main.ts', 'import { x } from "./one.json";\n console.log(x);');
30+
31+
const { result } = await harness.executeOnce();
32+
expect(result?.success).toBe(true);
33+
harness.expectFile('dist/browser/main.js').content.not.toContain('12345');
34+
harness.expectFile('dist/browser/main.js').content.toContain('67890');
35+
});
36+
});
37+
});

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,31 @@ export function createCompilerPlugin(
512512
}),
513513
);
514514

515+
// Add a load handler if there are file replacement option entries for JSON files
516+
if (
517+
pluginOptions.fileReplacements &&
518+
Object.keys(pluginOptions.fileReplacements).some((value) => value.endsWith('.json'))
519+
) {
520+
build.onLoad(
521+
{ filter: /\.json$/ },
522+
createCachedLoad(pluginOptions.loadResultCache, async (args) => {
523+
const replacement = pluginOptions.fileReplacements?.[path.normalize(args.path)];
524+
if (replacement) {
525+
return {
526+
contents: await import('fs/promises').then(({ readFile }) =>
527+
readFile(path.normalize(replacement)),
528+
),
529+
loader: 'json' as const,
530+
watchFiles: [replacement],
531+
};
532+
}
533+
534+
// If no replacement defined, let esbuild handle it directly
535+
return null;
536+
}),
537+
);
538+
}
539+
515540
// Setup bundling of component templates and stylesheets when in JIT mode
516541
if (pluginOptions.jit) {
517542
setupJitPluginCallbacks(

0 commit comments

Comments
 (0)