|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 | import * as vm from 'vm';
|
9 |
| -import { Compilation, NormalModule } from 'webpack'; |
| 9 | +import { Compilation, EntryPlugin, NormalModule, library, node } from 'webpack'; |
10 | 10 | import { RawSource } from 'webpack-sources';
|
11 | 11 | 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'); |
18 | 12 |
|
19 | 13 | interface CompilationOutput {
|
20 | 14 | content: string;
|
@@ -76,27 +70,35 @@ export class WebpackResourceLoader {
|
76 | 70 | throw new Error('WebpackResourceLoader cannot be used without parentCompilation');
|
77 | 71 | }
|
78 | 72 |
|
| 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 | + |
79 | 79 | // Simple sanity check.
|
80 | 80 | 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.`); |
84 | 82 | }
|
85 | 83 |
|
86 |
| - // Create a special URL for reading the resource from memory |
87 |
| - const angularScheme = 'angular-resource://'; |
88 |
| - |
89 | 84 | 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 | + |
91 | 93 | const context = this._parentCompilation.compiler.context;
|
92 | 94 | const childCompiler = this._parentCompilation.createChildCompiler(
|
93 | 95 | 'angular-compiler:resource',
|
94 | 96 | outputOptions,
|
95 | 97 | [
|
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'), |
100 | 102 | ],
|
101 | 103 | );
|
102 | 104 |
|
@@ -148,29 +150,18 @@ export class WebpackResourceLoader {
|
148 | 150 |
|
149 | 151 | let finalContent: string | undefined;
|
150 | 152 | 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 | + }); |
174 | 165 |
|
175 | 166 | return new Promise<CompilationOutput>((resolve, reject) => {
|
176 | 167 | childCompiler.runAsChild((error, _, childCompilation) => {
|
|
0 commit comments