diff --git a/packages/angular_devkit/build_angular/src/builders/browser-esbuild/sass-plugin.ts b/packages/angular_devkit/build_angular/src/builders/browser-esbuild/sass-plugin.ts index 6d96d3e071ab..a9c5dc5bd551 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser-esbuild/sass-plugin.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser-esbuild/sass-plugin.ts @@ -114,32 +114,54 @@ async function compileString( url, { previousResolvedModules }: FileImporterWithRequestContextOptions, ): Promise => { - 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; }, }, ],