/
FileEmitter.ts
353 lines (325 loc) · 10.1 KB
/
FileEmitter.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import sha256 from 'hash.js/lib/hash/sha/256';
import Chunk from '../Chunk';
import Graph from '../Graph';
import Module from '../Module';
import { FilePlaceholder, OutputBundleWithPlaceholders } from '../rollup/types';
import { BuildPhase } from './buildPhase';
import {
errAssetNotFinalisedForFileName,
errAssetReferenceIdNotFoundForSetSource,
errAssetSourceAlreadySet,
errChunkNotGeneratedForFileName,
errFailedValidation,
errFileNameConflict,
errFileReferenceIdNotFoundForFilename,
errInvalidRollupPhaseForChunkEmission,
errNoAssetSourceSet,
error
} from './error';
import { extname } from './path';
import { isPlainPathFragment } from './relativeId';
import { makeUnique, renderNamePattern } from './renderNamePattern';
interface OutputSpecificFileData {
assetFileNames: string;
bundle: OutputBundleWithPlaceholders;
}
function generateAssetFileName(
name: string | undefined,
source: string | Buffer,
output: OutputSpecificFileData
): string {
const emittedName = name || 'asset';
return makeUnique(
renderNamePattern(output.assetFileNames, 'output.assetFileNames', {
hash() {
const hash = sha256();
hash.update(emittedName);
hash.update(':');
hash.update(source);
return hash.digest('hex').substr(0, 8);
},
ext: () => extname(emittedName).substr(1),
extname: () => extname(emittedName),
name: () => emittedName.substr(0, emittedName.length - extname(emittedName).length)
}),
output.bundle
);
}
function reserveFileNameInBundle(fileName: string, bundle: OutputBundleWithPlaceholders) {
if (fileName in bundle) {
return error(errFileNameConflict(fileName));
}
bundle[fileName] = FILE_PLACEHOLDER;
}
interface ConsumedChunk {
fileName: string | undefined;
module: null | Module;
name: string;
type: 'chunk';
}
interface ConsumedAsset {
fileName: string | undefined;
name: string | undefined;
source: string | Buffer | undefined;
type: 'asset';
}
interface EmittedFile {
fileName?: string;
name?: string;
type: 'chunk' | 'asset';
[key: string]: unknown;
}
type ConsumedFile = ConsumedChunk | ConsumedAsset;
export const FILE_PLACEHOLDER: FilePlaceholder = {
type: 'placeholder'
};
function hasValidType(
emittedFile: unknown
): emittedFile is { type: 'asset' | 'chunk'; [key: string]: unknown } {
return (
emittedFile &&
((emittedFile as { [key: string]: unknown }).type === 'asset' ||
(emittedFile as { [key: string]: unknown }).type === 'chunk')
);
}
function hasValidName(emittedFile: {
type: 'asset' | 'chunk';
[key: string]: unknown;
}): emittedFile is EmittedFile {
const validatedName = emittedFile.fileName || emittedFile.name;
return (
!validatedName || (typeof validatedName === 'string' && isPlainPathFragment(validatedName))
);
}
function getValidSource(
source: unknown,
emittedFile: { fileName?: string; name?: string },
fileReferenceId: string | null
): string | Buffer {
if (typeof source !== 'string' && !Buffer.isBuffer(source)) {
const assetName = emittedFile.fileName || emittedFile.name || fileReferenceId;
return error(
errFailedValidation(
`Could not set source for ${
typeof assetName === 'string' ? `asset "${assetName}"` : 'unnamed asset'
}, asset source needs to be a string of Buffer.`
)
);
}
return source;
}
function getAssetFileName(file: ConsumedAsset, referenceId: string): string {
if (typeof file.fileName !== 'string') {
return error(errAssetNotFinalisedForFileName(file.name || referenceId));
}
return file.fileName;
}
function getChunkFileName(file: ConsumedChunk): string {
const fileName = file.fileName || (file.module && (file.module.facadeChunk as Chunk).id);
if (!fileName) return error(errChunkNotGeneratedForFileName(file.fileName || file.name));
return fileName;
}
export class FileEmitter {
private filesByReferenceId = new Map<string, ConsumedFile>();
// tslint:disable member-ordering
private buildFilesByReferenceId = this.filesByReferenceId;
private graph: Graph;
private output: OutputSpecificFileData | null = null;
constructor(graph: Graph) {
this.graph = graph;
}
public emitFile = (emittedFile: unknown): string => {
if (!hasValidType(emittedFile)) {
return error(
errFailedValidation(
`Emitted files must be of type "asset" or "chunk", received "${emittedFile &&
(emittedFile as any).type}".`
)
);
}
if (!hasValidName(emittedFile)) {
return error(
errFailedValidation(
`The "fileName" or "name" properties of emitted files must be strings that are neither absolute nor relative paths and do not contain invalid characters, received "${emittedFile.fileName ||
emittedFile.name}".`
)
);
}
if (emittedFile.type === 'chunk') {
return this.emitChunk(emittedFile);
} else {
return this.emitAsset(emittedFile);
}
};
public getFileName = (fileReferenceId: string) => {
const emittedFile = this.filesByReferenceId.get(fileReferenceId);
if (!emittedFile) return error(errFileReferenceIdNotFoundForFilename(fileReferenceId));
if (emittedFile.type === 'chunk') {
return getChunkFileName(emittedFile);
} else {
return getAssetFileName(emittedFile, fileReferenceId);
}
};
public setAssetSource = (referenceId: string, requestedSource: unknown) => {
const consumedFile = this.filesByReferenceId.get(referenceId);
if (!consumedFile) return error(errAssetReferenceIdNotFoundForSetSource(referenceId));
if (consumedFile.type !== 'asset') {
return error(
errFailedValidation(
`Asset sources can only be set for emitted assets but "${referenceId}" is an emitted chunk.`
)
);
}
if (consumedFile.source !== undefined) {
return error(errAssetSourceAlreadySet(consumedFile.name || referenceId));
}
const source = getValidSource(requestedSource, consumedFile, referenceId);
if (this.output) {
this.finalizeAsset(consumedFile, source, referenceId, this.output);
} else {
consumedFile.source = source;
}
};
public startOutput(outputBundle: OutputBundleWithPlaceholders, assetFileNames: string) {
this.filesByReferenceId = new Map(this.buildFilesByReferenceId);
this.output = {
assetFileNames,
bundle: outputBundle
};
for (const emittedFile of this.filesByReferenceId.values()) {
if (emittedFile.fileName) {
reserveFileNameInBundle(emittedFile.fileName, this.output.bundle);
}
}
for (const [referenceId, consumedFile] of this.filesByReferenceId.entries()) {
if (consumedFile.type === 'asset' && consumedFile.source !== undefined) {
this.finalizeAsset(consumedFile, consumedFile.source, referenceId, this.output);
}
}
}
public assertAssetsFinalized() {
for (const [referenceId, emittedFile] of this.filesByReferenceId.entries()) {
if (emittedFile.type === 'asset' && typeof emittedFile.fileName !== 'string')
error(errNoAssetSourceSet(emittedFile.name || referenceId));
}
}
private emitAsset(emittedAsset: EmittedFile): string {
const source =
typeof emittedAsset.source !== 'undefined'
? getValidSource(emittedAsset.source, emittedAsset, null)
: undefined;
const consumedAsset: ConsumedAsset = {
fileName: emittedAsset.fileName,
name: emittedAsset.name,
source,
type: 'asset'
};
const referenceId = this.assignReferenceId(
consumedAsset,
emittedAsset.fileName || emittedAsset.name || emittedAsset.type
);
if (this.output) {
if (emittedAsset.fileName) {
reserveFileNameInBundle(emittedAsset.fileName, this.output.bundle);
}
if (source !== undefined) {
this.finalizeAsset(consumedAsset, source, referenceId, this.output);
}
}
return referenceId;
}
private emitChunk(emittedChunk: EmittedFile): string {
if (this.graph.phase > BuildPhase.LOAD_AND_PARSE) {
error(errInvalidRollupPhaseForChunkEmission());
}
if (typeof emittedChunk.id !== 'string') {
return error(
errFailedValidation(
`Emitted chunks need to have a valid string id, received "${emittedChunk.id}"`
)
);
}
const consumedChunk: ConsumedChunk = {
fileName: emittedChunk.fileName,
module: null,
name: emittedChunk.name || emittedChunk.id,
type: 'chunk'
};
this.graph.moduleLoader
.addEntryModules(
[
{
fileName: emittedChunk.fileName || null,
id: emittedChunk.id,
name: emittedChunk.name || null
}
],
false
)
.then(({ newEntryModules: [module] }) => {
consumedChunk.module = module;
})
.catch(() => {
// Avoid unhandled Promise rejection as the error will be thrown later
// once module loading has finished
});
return this.assignReferenceId(consumedChunk, emittedChunk.id);
}
private assignReferenceId(file: ConsumedFile, idBase: string): string {
let referenceId: string | undefined;
do {
const hash = sha256();
if (referenceId) {
hash.update(referenceId);
} else {
hash.update(idBase);
}
referenceId = hash.digest('hex').substr(0, 8);
} while (this.filesByReferenceId.has(referenceId));
this.filesByReferenceId.set(referenceId, file);
return referenceId;
}
private finalizeAsset(
consumedFile: ConsumedFile,
source: string | Buffer,
referenceId: string,
output: OutputSpecificFileData
) {
const fileName =
consumedFile.fileName ||
this.findExistingAssetFileNameWithSource(output.bundle, source) ||
generateAssetFileName(consumedFile.name, source, output);
// We must not modify the original assets to avoid interaction between outputs
const assetWithFileName = { ...consumedFile, source, fileName };
this.filesByReferenceId.set(referenceId, assetWithFileName);
const self = this;
output.bundle[fileName] = {
fileName,
get isAsset(): true {
self.graph.warnDeprecation(
'Accessing "isAsset" on files in the bundle is deprecated, please use "type === \'asset\'" instead',
false
);
return true;
},
source,
type: 'asset'
};
}
private findExistingAssetFileNameWithSource(
bundle: OutputBundleWithPlaceholders,
source: string | Buffer
): string | null {
for (const fileName of Object.keys(bundle)) {
const outputFile = bundle[fileName];
if (
outputFile.type === 'asset' &&
(Buffer.isBuffer(source) && Buffer.isBuffer(outputFile.source)
? source.equals(outputFile.source)
: source === outputFile.source)
)
return fileName;
}
return null;
}
}