-
Notifications
You must be signed in to change notification settings - Fork 12k
/
resource_loader.ts
196 lines (165 loc) · 6.6 KB
/
resource_loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// TODO: fix typings.
// tslint:disable-next-line:no-global-tslint-disable
// tslint:disable:no-any
import * as path from 'path';
import * as vm from 'vm';
import { RawSource } from 'webpack-sources';
import { normalizePath } from './ivy/paths';
const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
const LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
interface CompilationOutput {
outputName: string;
source: string;
}
export class WebpackResourceLoader {
private _parentCompilation: any;
private _context = '';
private _fileDependencies = new Map<string, Set<string>>();
private _reverseDependencies = new Map<string, Set<string>>();
private _cachedSources = new Map<string, string>();
private _cachedEvaluatedSources = new Map<string, RawSource>();
public changedFiles?: Iterable<string>;
update(parentCompilation: import('webpack').compilation.Compilation, changedFiles?: Iterable<string>) {
this._parentCompilation = parentCompilation;
this._context = parentCompilation.context;
// Update changed file list
this.changedFiles = changedFiles;
}
getModifiedResourceFiles() {
const modifiedResources = new Set<string>();
if (!this.changedFiles) {
return modifiedResources;
}
for (const changedFile of this.changedFiles) {
this.getAffectedResources(
changedFile,
).forEach((affected: string) => modifiedResources.add(affected));
}
return modifiedResources;
}
getResourceDependencies(filePath: string) {
return this._fileDependencies.get(filePath) || [];
}
getAffectedResources(file: string) {
return this._reverseDependencies.get(file) || [];
}
setAffectedResources(file: string, resources: Iterable<string>) {
this._reverseDependencies.set(file, new Set(resources));
}
private async _compile(filePath: string): Promise<CompilationOutput> {
if (!this._parentCompilation) {
throw new Error('WebpackResourceLoader cannot be used without parentCompilation');
}
// Simple sanity check.
if (filePath.match(/\.[jt]s$/)) {
return Promise.reject(
`Cannot use a JavaScript or TypeScript file (${filePath}) in a component's styleUrls or templateUrl.`,
);
}
const outputOptions = { filename: filePath };
const relativePath = path.relative(this._context || '', filePath);
const childCompiler = this._parentCompilation.createChildCompiler(relativePath, outputOptions);
childCompiler.context = this._context;
new NodeTemplatePlugin(outputOptions).apply(childCompiler);
new NodeTargetPlugin().apply(childCompiler);
new SingleEntryPlugin(this._context, filePath).apply(childCompiler);
new LibraryTemplatePlugin('resource', 'var').apply(childCompiler);
childCompiler.hooks.thisCompilation.tap('ngtools-webpack', (compilation: any) => {
compilation.hooks.additionalAssets.tapAsync('ngtools-webpack',
(callback: (err?: Error) => void) => {
if (this._cachedEvaluatedSources.has(compilation.fullHash)) {
const cachedEvaluatedSource = this._cachedEvaluatedSources.get(compilation.fullHash);
compilation.assets[filePath] = cachedEvaluatedSource;
callback();
return;
}
const asset = compilation.assets[filePath];
if (asset) {
this._evaluate({ outputName: filePath, source: asset.source() })
.then(output => {
const evaluatedSource = new RawSource(output);
this._cachedEvaluatedSources.set(compilation.fullHash, evaluatedSource);
compilation.assets[filePath] = evaluatedSource;
callback();
})
.catch(err => callback(err));
} else {
callback();
}
});
});
// Compile and return a promise
const childCompilation = await new Promise<any>((resolve, reject) => {
childCompiler.compile((err: Error, childCompilation: any) => {
if (err) {
reject(err);
} else {
resolve(childCompilation);
}
});
});
// Propagate warnings to parent compilation.
const { warnings, errors } = childCompilation;
if (warnings && warnings.length) {
this._parentCompilation.warnings.push(...warnings);
}
if (errors && errors.length) {
this._parentCompilation.errors.push(...errors);
}
Object.keys(childCompilation.assets).forEach(assetName => {
// Add all new assets to the parent compilation, with the exception of
// the file we're loading and its sourcemap.
if (
assetName !== filePath
&& assetName !== `${filePath}.map`
&& this._parentCompilation.assets[assetName] == undefined
) {
this._parentCompilation.assets[assetName] = childCompilation.assets[assetName];
}
});
// Save the dependencies for this resource.
this._fileDependencies.set(filePath, new Set(childCompilation.fileDependencies));
for (const file of childCompilation.fileDependencies) {
const resolvedFile = normalizePath(file);
const entry = this._reverseDependencies.get(resolvedFile);
if (entry) {
entry.add(filePath);
} else {
this._reverseDependencies.set(resolvedFile, new Set([filePath]));
}
}
const compilationHash = childCompilation.fullHash;
const maybeSource = this._cachedSources.get(compilationHash);
if (maybeSource) {
return { outputName: filePath, source: maybeSource };
} else {
const source = childCompilation.assets[filePath].source();
this._cachedSources.set(compilationHash, source);
return { outputName: filePath, source };
}
}
private async _evaluate({ outputName, source }: CompilationOutput): Promise<string> {
// Evaluate code
const context: { resource?: string | { default?: string } } = {};
vm.runInNewContext(source, context, { filename: outputName });
if (typeof context.resource === 'string') {
return context.resource;
} else if (typeof context.resource?.default === 'string') {
return context.resource.default;
}
throw new Error(`The loader "${outputName}" didn't return a string.`);
}
get(filePath: string): Promise<string> {
return this._compile(filePath)
.then((result: CompilationOutput) => result.source);
}
}