Skip to content

Commit 1c1f985

Browse files
clydinalan-agius4
authored andcommitted
fix(@ngtools/webpack): support inline style sourcemaps when using css-loader for component styles
When using the `css-loader` as the final entry in the Webpack loader chain, sourcemaps will not be generated and inlined by the Webpack stylesheet runtime unless the `btoa` global function exists. Now that the Angular CLI uses the `css-loader` to handle `@import`, the `btoa` function must be provided within the custom VM execution context used to evaluate the output of the Webpack component stylesheet processing pipeline. (cherry picked from commit 55d3d6c)
1 parent 3ff3917 commit 1c1f985

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/ngtools/webpack/src/resource_loader.ts

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

9-
import assert from 'assert';
10-
import * as path from 'path';
11-
import * as vm from 'vm';
9+
import assert from 'node:assert';
10+
import { Buffer } from 'node:buffer';
11+
import * as path from 'node:path';
12+
import * as vm from 'node:vm';
1213
import type { Asset, Compilation } from 'webpack';
1314
import { addError } from './ivy/diagnostics';
1415
import { normalizePath } from './ivy/paths';
@@ -317,7 +318,13 @@ export class WebpackResourceLoader {
317318

318319
private _evaluate(filename: string, source: string): string | null {
319320
// Evaluate code
320-
const context: { resource?: string | { default?: string } } = {};
321+
322+
// css-loader requires the btoa function to exist to correctly generate inline sourcemaps
323+
const context: { btoa: (input: string) => string; resource?: string | { default?: string } } = {
324+
btoa(input) {
325+
return Buffer.from(input).toString('base64');
326+
},
327+
};
321328

322329
try {
323330
vm.runInNewContext(source, context, { filename });

0 commit comments

Comments
 (0)