Skip to content

Commit ed67d9a

Browse files
committed
refactor(@angular-devkit/build-angular): replace most custom path resolve usages with Node.js builtins
During the build initialization phase, many paths are converted back and forth between multiple normalized forms. These conversions involve potentially expensive string operations. The majority of the custom path `resolve` function from `@angular-devkit/core` usages have now been removed in favor of the Node.js builtin path functions. This change reduces the need to perform additional string manipulation where possible.
1 parent c4d3dfa commit ed67d9a

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

packages/angular_devkit/build_angular/src/builders/app-shell/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
createBuilder,
1313
targetFromTargetString,
1414
} from '@angular-devkit/architect';
15-
import { JsonObject, normalize, resolve } from '@angular-devkit/core';
15+
import { JsonObject, normalize } from '@angular-devkit/core';
1616
import * as fs from 'fs';
1717
import * as path from 'path';
1818
import { normalizeOptimization } from '../../utils';
@@ -52,7 +52,7 @@ async function _renderUniversal(
5252
}
5353

5454
const projectMetadata = await context.getProjectMetadata(projectName);
55-
const projectRoot = resolve(normalize(root), normalize((projectMetadata.root as string) || ''));
55+
const projectRoot = path.join(root, (projectMetadata.root as string | undefined) ?? '');
5656

5757
const { styles } = normalizeOptimization(browserOptions.optimization);
5858
const inlineCriticalCssProcessor = styles.inlineCritical
@@ -114,7 +114,7 @@ async function _renderUniversal(
114114

115115
if (browserOptions.serviceWorker) {
116116
await augmentAppWithServiceWorker(
117-
projectRoot,
117+
normalize(projectRoot),
118118
normalize(outputPath),
119119
browserOptions.baseHref || '/',
120120
browserOptions.ngswConfigPath,

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
1010
import { EmittedFiles, WebpackLoggingCallback, runWebpack } from '@angular-devkit/build-webpack';
11-
import { getSystemPath, json, logging, normalize, resolve } from '@angular-devkit/core';
11+
import { json, logging, normalize } from '@angular-devkit/core';
1212
import * as fs from 'fs';
1313
import * as path from 'path';
1414
import { Observable, from } from 'rxjs';
@@ -142,8 +142,6 @@ export function buildWebpackBrowser(
142142
indexHtml?: IndexHtmlTransform;
143143
} = {},
144144
): Observable<BrowserBuilderOutput> {
145-
const root = normalize(context.workspaceRoot);
146-
147145
const projectName = context.target?.project;
148146
if (!projectName) {
149147
throw new Error('The builder requires a target.');
@@ -157,20 +155,17 @@ export function buildWebpackBrowser(
157155

158156
return from(context.getProjectMetadata(projectName)).pipe(
159157
switchMap(async (projectMetadata) => {
160-
const sysProjectRoot = getSystemPath(
161-
resolve(
162-
normalize(context.workspaceRoot),
163-
normalize((projectMetadata.root as string) ?? ''),
164-
),
165-
);
166-
167158
// Purge old build disk cache.
168159
await purgeStaleBuildCache(context);
169160

170-
checkInternetExplorerSupport(sysProjectRoot, context.logger);
161+
// Initialize builder
162+
const initialization = await initialize(options, context, transforms.webpackConfiguration);
163+
164+
// Check and warn about IE browser support
165+
checkInternetExplorerSupport(initialization.projectRoot, context.logger);
171166

172167
return {
173-
...(await initialize(options, context, transforms.webpackConfiguration)),
168+
...initialization,
174169
cacheOptions: normalizeCacheOptions(projectMetadata, context.workspaceRoot),
175170
};
176171
}),
@@ -274,7 +269,7 @@ export function buildWebpackBrowser(
274269
await copyAssets(
275270
normalizeAssetPatterns(
276271
options.assets,
277-
root,
272+
normalize(context.workspaceRoot),
278273
normalize(projectRoot),
279274
projectSourceRoot === undefined ? undefined : normalize(projectSourceRoot),
280275
),

packages/angular_devkit/build_angular/src/utils/normalize-builder-schema.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { Path, getSystemPath, json } from '@angular-devkit/core';
9+
import { json, normalize } from '@angular-devkit/core';
1010
import {
1111
AssetPatternClass,
1212
Schema as BrowserBuilderSchema,
@@ -35,19 +35,27 @@ export type NormalizedBrowserBuilderSchema = BrowserBuilderSchema &
3535
};
3636

3737
export function normalizeBrowserSchema(
38-
root: Path,
39-
projectRoot: Path,
40-
sourceRoot: Path | undefined,
38+
workspaceRoot: string,
39+
projectRoot: string,
40+
projectSourceRoot: string | undefined,
4141
options: BrowserBuilderSchema,
4242
metadata: json.JsonObject,
4343
): NormalizedBrowserBuilderSchema {
4444
const normalizedSourceMapOptions = normalizeSourceMaps(options.sourceMap || false);
4545

4646
return {
4747
...options,
48-
cache: normalizeCacheOptions(metadata, getSystemPath(root)),
49-
assets: normalizeAssetPatterns(options.assets || [], root, projectRoot, sourceRoot),
50-
fileReplacements: normalizeFileReplacements(options.fileReplacements || [], root),
48+
cache: normalizeCacheOptions(metadata, workspaceRoot),
49+
assets: normalizeAssetPatterns(
50+
options.assets || [],
51+
normalize(workspaceRoot),
52+
normalize(projectRoot),
53+
projectSourceRoot ? normalize(projectSourceRoot) : undefined,
54+
),
55+
fileReplacements: normalizeFileReplacements(
56+
options.fileReplacements || [],
57+
normalize(workspaceRoot),
58+
),
5159
optimization: normalizeOptimization(options.optimization),
5260
sourceMap: normalizedSourceMapOptions,
5361
preserveSymlinks:
@@ -66,6 +74,6 @@ export function normalizeBrowserSchema(
6674
// A value of 0 is falsy and will disable polling rather then enable
6775
// 500 ms is a sensible default in this case
6876
poll: options.poll === 0 ? 500 : options.poll,
69-
supportedBrowsers: getSupportedBrowsers(getSystemPath(projectRoot)),
77+
supportedBrowsers: getSupportedBrowsers(projectRoot),
7078
};
7179
}

packages/angular_devkit/build_angular/src/utils/webpack-browser-config.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { BuilderContext } from '@angular-devkit/architect';
10-
import { getSystemPath, json, logging, normalize, resolve } from '@angular-devkit/core';
10+
import { logging } from '@angular-devkit/core';
1111
import * as path from 'path';
1212
import { ScriptTarget } from 'typescript';
1313
import { Configuration, javascript } from 'webpack';
@@ -146,26 +146,24 @@ export async function generateBrowserWebpackConfigFromContext(
146146
throw new Error('The builder requires a target.');
147147
}
148148

149-
const workspaceRoot = normalize(context.workspaceRoot);
149+
const workspaceRoot = context.workspaceRoot;
150150
const projectMetadata = await context.getProjectMetadata(projectName);
151-
const projectRoot = resolve(workspaceRoot, normalize((projectMetadata.root as string) || ''));
152-
const projectSourceRoot = projectMetadata.sourceRoot as string | undefined;
153-
const sourceRoot = projectSourceRoot
154-
? resolve(workspaceRoot, normalize(projectSourceRoot))
155-
: undefined;
151+
const projectRoot = path.join(workspaceRoot, (projectMetadata.root as string | undefined) ?? '');
152+
const sourceRoot = projectMetadata.sourceRoot as string | undefined;
153+
const projectSourceRoot = sourceRoot ? path.join(workspaceRoot, sourceRoot) : undefined;
156154

157155
const normalizedOptions = normalizeBrowserSchema(
158156
workspaceRoot,
159157
projectRoot,
160-
sourceRoot,
158+
projectSourceRoot,
161159
options,
162160
projectMetadata,
163161
);
164162

165163
const config = await generateWebpackConfig(
166-
getSystemPath(workspaceRoot),
167-
getSystemPath(projectRoot),
168-
sourceRoot && getSystemPath(sourceRoot),
164+
workspaceRoot,
165+
projectRoot,
166+
projectSourceRoot,
169167
projectName,
170168
normalizedOptions,
171169
webpackPartialGenerator,
@@ -189,8 +187,8 @@ export async function generateBrowserWebpackConfigFromContext(
189187

190188
return {
191189
config,
192-
projectRoot: getSystemPath(projectRoot),
193-
projectSourceRoot: sourceRoot && getSystemPath(sourceRoot),
190+
projectRoot,
191+
projectSourceRoot,
194192
};
195193
}
196194

0 commit comments

Comments
 (0)