Skip to content

Commit 1c527a9

Browse files
clydindgp1130
authored andcommitted
feat(@angular-devkit/build-angular): add esbuild-based builder initial support for fileReplacements
Support for the `fileReplacements` option from the Webpack-based builder has now been integrated into the experimental esbuild-based browser application builder. The option will no longer be ignored during builds. Only the officially supported form of the option (`replace`/`with` fields) is implemented.
1 parent ca56489 commit 1c527a9

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

packages/angular_devkit/build_angular/src/builders/browser-esbuild/compiler-plugin.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export function createCompilerPlugin(
128128
tsconfig: string;
129129
advancedOptimizations?: boolean;
130130
thirdPartySourcemaps?: boolean;
131+
fileReplacements?: Record<string, string>;
131132
},
132133
styleOptions: BundleStylesheetOptions,
133134
): Plugin {
@@ -256,6 +257,13 @@ export function createCompilerPlugin(
256257
return { content: contents };
257258
};
258259

260+
// Augment TypeScript Host for file replacements option
261+
if (pluginOptions.fileReplacements) {
262+
// Temporary deep import for file replacements support
263+
const { augmentHostWithReplacements } = require('@ngtools/webpack/src/ivy/host');
264+
augmentHostWithReplacements(host, pluginOptions.fileReplacements);
265+
}
266+
259267
// Create the Angular specific program that contains the Angular compiler
260268
const angularProgram = new compilerCli.NgtscProgram(rootNames, compilerOptions, host);
261269
const angularCompiler = angularProgram.compiler;
@@ -316,7 +324,9 @@ export function createCompilerPlugin(
316324
async (args) => {
317325
assert.ok(fileEmitter, 'Invalid plugin execution order');
318326

319-
const typescriptResult = await fileEmitter(args.path);
327+
const typescriptResult = await fileEmitter(
328+
pluginOptions.fileReplacements?.[args.path] ?? args.path,
329+
);
320330
if (!typescriptResult) {
321331
// No TS result indicates the file is not part of the TypeScript program.
322332
// If allowJs is enabled and the file is JS then defer to the next load hook.

packages/angular_devkit/build_angular/src/builders/browser-esbuild/experimental-warnings.ts

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const UNSUPPORTED_OPTIONS: Array<keyof BrowserBuilderOptions> = [
1313
'allowedCommonJsDependencies',
1414
'budgets',
1515
'extractLicenses',
16-
'fileReplacements',
1716
'progress',
1817
'scripts',
1918
'statsJson',

packages/angular_devkit/build_angular/src/builders/browser-esbuild/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@ async function bundleCode(
249249
sourcemapOptions: SourceMapClass,
250250
tsconfig: string,
251251
) {
252+
let fileReplacements: Record<string, string> | undefined;
253+
if (options.fileReplacements) {
254+
for (const replacement of options.fileReplacements) {
255+
fileReplacements ??= {};
256+
fileReplacements[path.join(workspaceRoot, replacement.replace)] = path.join(
257+
workspaceRoot,
258+
replacement.with,
259+
);
260+
}
261+
}
262+
252263
return bundle({
253264
absWorkingDir: workspaceRoot,
254265
bundle: true,
@@ -288,6 +299,7 @@ async function bundleCode(
288299
thirdPartySourcemaps: sourcemapOptions.vendor,
289300
tsconfig,
290301
advancedOptimizations: options.buildOptimizer,
302+
fileReplacements,
291303
},
292304
// Component stylesheet options
293305
{

packages/angular_devkit/build_angular/src/builders/browser-esbuild/schema.json

+11-30
Original file line numberDiff line numberDiff line change
@@ -461,38 +461,19 @@
461461
]
462462
},
463463
"fileReplacement": {
464-
"oneOf": [
465-
{
466-
"type": "object",
467-
"properties": {
468-
"src": {
469-
"type": "string",
470-
"pattern": "\\.(([cm]?j|t)sx?|json)$"
471-
},
472-
"replaceWith": {
473-
"type": "string",
474-
"pattern": "\\.(([cm]?j|t)sx?|json)$"
475-
}
476-
},
477-
"additionalProperties": false,
478-
"required": ["src", "replaceWith"]
464+
"type": "object",
465+
"properties": {
466+
"replace": {
467+
"type": "string",
468+
"pattern": "\\.(([cm]?j|t)sx?|json)$"
479469
},
480-
{
481-
"type": "object",
482-
"properties": {
483-
"replace": {
484-
"type": "string",
485-
"pattern": "\\.(([cm]?j|t)sx?|json)$"
486-
},
487-
"with": {
488-
"type": "string",
489-
"pattern": "\\.(([cm]?j|t)sx?|json)$"
490-
}
491-
},
492-
"additionalProperties": false,
493-
"required": ["replace", "with"]
470+
"with": {
471+
"type": "string",
472+
"pattern": "\\.(([cm]?j|t)sx?|json)$"
494473
}
495-
]
474+
},
475+
"additionalProperties": false,
476+
"required": ["replace", "with"]
496477
},
497478
"budget": {
498479
"type": "object",

0 commit comments

Comments
 (0)