Skip to content

Commit

Permalink
fix(@ngtools/webpack): support inline style sourcemaps when using css…
Browse files Browse the repository at this point in the history
…-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)
  • Loading branch information
clydin authored and alan-agius4 committed Oct 14, 2022
1 parent 3ff3917 commit 1c1f985
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/ngtools/webpack/src/resource_loader.ts
Expand Up @@ -6,9 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import assert from 'assert';
import * as path from 'path';
import * as vm from 'vm';
import assert from 'node:assert';
import { Buffer } from 'node:buffer';
import * as path from 'node:path';
import * as vm from 'node:vm';
import type { Asset, Compilation } from 'webpack';
import { addError } from './ivy/diagnostics';
import { normalizePath } from './ivy/paths';
Expand Down Expand Up @@ -317,7 +318,13 @@ export class WebpackResourceLoader {

private _evaluate(filename: string, source: string): string | null {
// Evaluate code
const context: { resource?: string | { default?: string } } = {};

// css-loader requires the btoa function to exist to correctly generate inline sourcemaps
const context: { btoa: (input: string) => string; resource?: string | { default?: string } } = {
btoa(input) {
return Buffer.from(input).toString('base64');
},
};

try {
vm.runInNewContext(source, context, { filename });
Expand Down

0 comments on commit 1c1f985

Please sign in to comment.