Skip to content

Commit 0520608

Browse files
clydinalan-agius4
authored andcommittedJan 9, 2023
fix(@angular-devkit/build-angular): use relative css resource paths in esbuild JSON stats
When using the experimental esbuild-based browser application builder with the `--stats-json` option, all referenced CSS resources will now use paths relative to the workspace root within the output JSON file. Previously, the resource paths within the stats JSON file were absolute and were inconsistent with the other JavaScript and CSS paths within the file. CSS resources include files that have been referenced in a bundled stylesheet `url()` such as images or fonts. (cherry picked from commit 84d2d8b)
1 parent 22cba79 commit 0520608

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed
 

‎packages/angular_devkit/build_angular/src/builders/browser-esbuild/css-resource-plugin.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88

99
import type { Plugin, PluginBuild } from 'esbuild';
10-
import { readFile } from 'fs/promises';
10+
import { readFile } from 'node:fs/promises';
11+
import { join, relative } from 'node:path';
1112

1213
/**
1314
* Symbol marker used to indicate CSS resource resolution is being attempted.
@@ -54,15 +55,25 @@ export function createCssResourcePlugin(): Plugin {
5455
resolveDir,
5556
});
5657

58+
// Return results that are not files since these are most likely specific to another plugin
59+
// and cannot be loaded by this plugin.
60+
if (result.namespace !== 'file' || !result.path) {
61+
return result;
62+
}
63+
64+
// All file results are considered CSS resources and will be loaded via the file loader
5765
return {
5866
...result,
67+
// Use a relative path to prevent fully resolved paths in the metafile (JSON stats file).
68+
// This is only necessary for custom namespaces. esbuild will handle the file namespace.
69+
path: relative(build.initialOptions.absWorkingDir ?? '', result.path),
5970
namespace: 'css-resource',
6071
};
6172
});
6273

6374
build.onLoad({ filter: /.*/, namespace: 'css-resource' }, async (args) => {
6475
return {
65-
contents: await readFile(args.path),
76+
contents: await readFile(join(build.initialOptions.absWorkingDir ?? '', args.path)),
6677
loader: 'file',
6778
};
6879
});

0 commit comments

Comments
 (0)
Please sign in to comment.