Skip to content

Commit 75afecb

Browse files
clydinfilipesilva
authored andcommitted
refactor(@angular-devkit/build-angular): use library output for stylesheet child compilations
This change allows the result of the stylesheet Webpack compilation to be explicitly available for access after the output source code is generated. The previous method relied on assumptions within the code generated by Webpack and no longer worked with Webpack 5.
1 parent 8a10f2e commit 75afecb

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

packages/ngtools/webpack/src/resource_loader.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { forwardSlashPath } from './utils';
1515

1616
const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
1717
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
18-
const LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin');
18+
const LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
1919
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
2020

2121

@@ -68,7 +68,7 @@ export class WebpackResourceLoader {
6868
new NodeTemplatePlugin(outputOptions).apply(childCompiler);
6969
new NodeTargetPlugin().apply(childCompiler);
7070
new SingleEntryPlugin(this._context, filePath).apply(childCompiler);
71-
new LoaderTargetPlugin('node').apply(childCompiler);
71+
new LibraryTemplatePlugin('resource', 'var').apply(childCompiler);
7272

7373
childCompiler.hooks.thisCompilation.tap('ngtools-webpack', (compilation: any) => {
7474
compilation.hooks.additionalAssets.tapAsync('ngtools-webpack',
@@ -155,13 +155,13 @@ export class WebpackResourceLoader {
155155

156156
private async _evaluate({ outputName, source }: CompilationOutput): Promise<string> {
157157
// Evaluate code
158-
const evaluatedSource = vm.runInNewContext(source, undefined, { filename: outputName });
159-
if (typeof evaluatedSource === 'object' && typeof evaluatedSource.default === 'string') {
160-
return evaluatedSource.default;
161-
}
158+
const context: { resource?: string | { default?: string } } = {};
159+
vm.runInNewContext(source, context, { filename: outputName });
162160

163-
if (typeof evaluatedSource === 'string') {
164-
return evaluatedSource;
161+
if (typeof context.resource === 'string') {
162+
return context.resource;
163+
} else if (typeof context.resource?.default === 'string') {
164+
return context.resource.default;
165165
}
166166

167167
throw new Error(`The loader "${outputName}" didn't return a string.`);

0 commit comments

Comments
 (0)