Skip to content

Commit 1f662eb

Browse files
alan-agius4clydin
authored andcommitted
refactor(@ngtools/webpack): remove usage of deprecated webpack plugins
Remove usage of `SingleEntryPlugin` and `LibraryTemplatePlugin` which are both deprecated.
1 parent 1532e32 commit 1f662eb

File tree

1 file changed

+32
-41
lines changed

1 file changed

+32
-41
lines changed

packages/ngtools/webpack/src/resource_loader.ts

+32-41
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import * as vm from 'vm';
9-
import { Compilation, NormalModule } from 'webpack';
9+
import { Compilation, EntryPlugin, NormalModule, library, node } from 'webpack';
1010
import { RawSource } from 'webpack-sources';
1111
import { normalizePath } from './ivy/paths';
12-
import { isWebpackFiveOrHigher } from './webpack-version';
13-
14-
const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
15-
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
16-
const LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
17-
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
1812

1913
interface CompilationOutput {
2014
content: string;
@@ -76,27 +70,35 @@ export class WebpackResourceLoader {
7670
throw new Error('WebpackResourceLoader cannot be used without parentCompilation');
7771
}
7872

73+
// Create a special URL for reading the resource from memory
74+
const entry = data ? 'angular-resource://' : filePath;
75+
if (!entry) {
76+
throw new Error(`"filePath" or "data" must be specified.`);
77+
}
78+
7979
// Simple sanity check.
8080
if (filePath?.match(/\.[jt]s$/)) {
81-
return Promise.reject(
82-
`Cannot use a JavaScript or TypeScript file (${filePath}) in a component's styleUrls or templateUrl.`,
83-
);
81+
throw new Error(`Cannot use a JavaScript or TypeScript file (${filePath}) in a component's styleUrls or templateUrl.`);
8482
}
8583

86-
// Create a special URL for reading the resource from memory
87-
const angularScheme = 'angular-resource://';
88-
8984
const outputFilePath = filePath || `angular-resource-output-${this.outputPathCounter++}.css`;
90-
const outputOptions = { filename: outputFilePath };
85+
const outputOptions = {
86+
filename: outputFilePath,
87+
library: {
88+
type: 'var',
89+
name: 'resource',
90+
},
91+
};
92+
9193
const context = this._parentCompilation.compiler.context;
9294
const childCompiler = this._parentCompilation.createChildCompiler(
9395
'angular-compiler:resource',
9496
outputOptions,
9597
[
96-
new NodeTemplatePlugin(outputOptions),
97-
new NodeTargetPlugin(),
98-
new SingleEntryPlugin(context, data ? angularScheme : filePath, 'resource'),
99-
new LibraryTemplatePlugin('resource', 'var'),
98+
new node.NodeTemplatePlugin(outputOptions),
99+
new node.NodeTargetPlugin(),
100+
new EntryPlugin(context, entry, { name: 'resource' }),
101+
new library.EnableLibraryPlugin('var'),
100102
],
101103
);
102104

@@ -148,29 +150,18 @@ export class WebpackResourceLoader {
148150

149151
let finalContent: string | undefined;
150152
let finalMap: string | undefined;
151-
if (isWebpackFiveOrHigher()) {
152-
childCompiler.hooks.compilation.tap('angular-compiler', (childCompilation) => {
153-
// tslint:disable-next-line: no-any
154-
(childCompilation.hooks as any).processAssets.tap(
155-
{ name: 'angular-compiler', stage: 5000 },
156-
() => {
157-
finalContent = childCompilation.assets[outputFilePath]?.source().toString();
158-
finalMap = childCompilation.assets[outputFilePath + '.map']?.source().toString();
159-
160-
delete childCompilation.assets[outputFilePath];
161-
delete childCompilation.assets[outputFilePath + '.map'];
162-
},
163-
);
164-
});
165-
} else {
166-
childCompiler.hooks.afterCompile.tap('angular-compiler', (childCompilation) => {
167-
finalContent = childCompilation.assets[outputFilePath]?.source().toString();
168-
finalMap = childCompilation.assets[outputFilePath + '.map']?.source().toString();
169-
170-
delete childCompilation.assets[outputFilePath];
171-
delete childCompilation.assets[outputFilePath + '.map'];
172-
});
173-
}
153+
childCompiler.hooks.compilation.tap('angular-compiler', (childCompilation) => {
154+
childCompilation.hooks.processAssets.tap(
155+
{ name: 'angular-compiler', stage: Compilation.PROCESS_ASSETS_STAGE_REPORT },
156+
() => {
157+
finalContent = childCompilation.assets[outputFilePath]?.source().toString();
158+
finalMap = childCompilation.assets[outputFilePath + '.map']?.source().toString();
159+
160+
delete childCompilation.assets[outputFilePath];
161+
delete childCompilation.assets[outputFilePath + '.map'];
162+
},
163+
);
164+
});
174165

175166
return new Promise<CompilationOutput>((resolve, reject) => {
176167
childCompiler.runAsChild((error, _, childCompilation) => {

0 commit comments

Comments
 (0)