Skip to content

Commit 6a142a2

Browse files
committed
fix(@angular-devkit/build-angular): allow the esbuild-based builder to fully resolve global stylesheet packages
The esbuild-based experimental builder will now leverage the bundler to perform resolution of CSS imports. This allows for more comprehensive resolution including packages which use the `sass` and/or `style` custom conditions within a `package.json` exports field.
1 parent e995bda commit 6a142a2

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ export async function buildEsbuildBrowser(
149149
const { entryPoints: stylesheetEntrypoints, noInjectNames } = resolveGlobalStyles(
150150
options.styles,
151151
workspaceRoot,
152-
!!options.preserveSymlinks,
152+
// preserveSymlinks is always true here to allow the bundler to handle the option
153+
true,
154+
// skipResolution to leverage the bundler's more comprehensive resolution
155+
true,
153156
);
154157
for (const [name, files] of Object.entries(stylesheetEntrypoints)) {
155158
const virtualEntryData = files
@@ -164,6 +167,7 @@ export async function buildEsbuildBrowser(
164167
sourcemap: !!sourcemapOptions.styles && (sourcemapOptions.hidden ? 'external' : true),
165168
outputNames: noInjectNames.includes(name) ? { media: outputNames.media } : outputNames,
166169
includePaths: options.stylePreprocessorOptions?.includePaths,
170+
preserveSymlinks: options.preserveSymlinks,
167171
},
168172
);
169173

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ async function bundleStylesheet(
3838
write: false,
3939
platform: 'browser',
4040
preserveSymlinks: options.preserveSymlinks,
41-
conditions: ['style'],
42-
mainFields: ['style'],
41+
conditions: ['style', 'sass'],
42+
mainFields: ['style', 'sass'],
4343
plugins: [
4444
createSassPlugin({ sourcemap: !!options.sourcemap, includePaths: options.includePaths }),
4545
],

packages/angular_devkit/build_angular/src/webpack/configs/styles.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function resolveGlobalStyles(
3030
styleEntrypoints: StyleElement[],
3131
root: string,
3232
preserveSymlinks: boolean,
33+
skipResolution = false,
3334
): { entryPoints: Record<string, string[]>; noInjectNames: string[]; paths: string[] } {
3435
const entryPoints: Record<string, string[]> = {};
3536
const noInjectNames: string[] = [];
@@ -40,22 +41,25 @@ export function resolveGlobalStyles(
4041
}
4142

4243
for (const style of normalizeExtraEntryPoints(styleEntrypoints, 'styles')) {
43-
let resolvedPath = path.resolve(root, style.input);
44-
if (!fs.existsSync(resolvedPath)) {
45-
try {
46-
resolvedPath = require.resolve(style.input, { paths: [root] });
47-
} catch {}
44+
let stylesheetPath = style.input;
45+
if (!skipResolution) {
46+
stylesheetPath = path.resolve(root, stylesheetPath);
47+
if (!fs.existsSync(stylesheetPath)) {
48+
try {
49+
stylesheetPath = require.resolve(style.input, { paths: [root] });
50+
} catch {}
51+
}
4852
}
4953

5054
if (!preserveSymlinks) {
51-
resolvedPath = fs.realpathSync(resolvedPath);
55+
stylesheetPath = fs.realpathSync(stylesheetPath);
5256
}
5357

5458
// Add style entry points.
5559
if (entryPoints[style.bundleName]) {
56-
entryPoints[style.bundleName].push(resolvedPath);
60+
entryPoints[style.bundleName].push(stylesheetPath);
5761
} else {
58-
entryPoints[style.bundleName] = [resolvedPath];
62+
entryPoints[style.bundleName] = [stylesheetPath];
5963
}
6064

6165
// Add non injected styles to the list.
@@ -64,7 +68,7 @@ export function resolveGlobalStyles(
6468
}
6569

6670
// Add global css paths.
67-
paths.push(resolvedPath);
71+
paths.push(stylesheetPath);
6872
}
6973

7074
return { entryPoints, noInjectNames, paths };

0 commit comments

Comments
 (0)