Skip to content

Commit

Permalink
perf(@angular-devkit/build-angular): enhance Sass package resolution …
Browse files Browse the repository at this point in the history
…in esbuild builder

The package module resolution logic for Sass stylesheets within the esbuild-based browser
application builder has been restructured to limit the need to perform fallback resolution
unless fully required. This allows common cases to avoid unnecessary and expensive resolution
attempts. This provided a roughly 40% improvement in build times for the Angular Material
documentation site.
  • Loading branch information
clydin authored and angular-robot[bot] committed Apr 12, 2023
1 parent c6c045b commit 11fa122
Showing 1 changed file with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,32 +114,54 @@ async function compileString(
url,
{ previousResolvedModules }: FileImporterWithRequestContextOptions,
): Promise<URL | null> => {
const result = await resolveUrl(url, previousResolvedModules);
let result = await resolveUrl(url);
if (result.path) {
return pathToFileURL(result.path);
}

// Check for package deep imports
if (!result.path) {
const parts = url.split('/');
const hasScope = parts.length >= 2 && parts[0].startsWith('@');
const [nameOrScope, nameOrFirstPath, ...pathPart] = parts;
const packageName = hasScope ? `${nameOrScope}/${nameOrFirstPath}` : nameOrScope;

const packageResult = await resolveUrl(
packageName + '/package.json',
previousResolvedModules,
const parts = url.split('/');
const hasScope = parts.length >= 2 && parts[0].startsWith('@');
const [nameOrScope, nameOrFirstPath, ...pathPart] = parts;
const packageName = hasScope ? `${nameOrScope}/${nameOrFirstPath}` : nameOrScope;

let packageResult = await resolveUrl(packageName + '/package.json');

if (packageResult.path) {
return pathToFileURL(
join(
dirname(packageResult.path),
!hasScope && nameOrFirstPath ? nameOrFirstPath : '',
...pathPart,
),
);
}

// Check with Yarn PnP workaround using previous resolved modules.
// This is done last to avoid a performance penalty for common cases.

if (packageResult.path) {
return pathToFileURL(
join(
dirname(packageResult.path),
!hasScope && nameOrFirstPath ? nameOrFirstPath : '',
...pathPart,
),
);
}
result = await resolveUrl(url, previousResolvedModules);
if (result.path) {
return pathToFileURL(result.path);
}

packageResult = await resolveUrl(
packageName + '/package.json',
previousResolvedModules,
);

if (packageResult.path) {
return pathToFileURL(
join(
dirname(packageResult.path),
!hasScope && nameOrFirstPath ? nameOrFirstPath : '',
...pathPart,
),
);
}

return result.path ? pathToFileURL(result.path) : null;
// Not found
return null;
},
},
],
Expand Down

0 comments on commit 11fa122

Please sign in to comment.