-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Bundle.ts
172 lines (161 loc) · 5.5 KB
/
Bundle.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
import Chunk from './Chunk';
import {
NormalizedInputOptions,
NormalizedOutputOptions,
OutputBundle,
OutputBundleWithPlaceholders,
OutputChunk
} from './rollup/types';
import { Addons, createAddons } from './utils/addons';
import commondir from './utils/commondir';
import { error, warnDeprecation } from './utils/error';
import { FILE_PLACEHOLDER } from './utils/FileEmitter';
import { basename, isAbsolute } from './utils/path';
import { PluginDriver } from './utils/PluginDriver';
import { timeEnd, timeStart } from './utils/timers';
export default class Bundle {
constructor(
private readonly outputOptions: NormalizedOutputOptions,
private readonly unsetOptions: Set<string>,
private readonly inputOptions: NormalizedInputOptions,
private readonly pluginDriver: PluginDriver,
private readonly chunks: Chunk[]
) {}
async generate(isWrite: boolean): Promise<OutputBundle> {
timeStart('GENERATE', 1);
const inputBase = commondir(getAbsoluteEntryModulePaths(this.chunks));
const outputBundle: OutputBundleWithPlaceholders = Object.create(null);
this.pluginDriver.setOutputBundle(outputBundle, this.outputOptions.assetFileNames);
try {
await this.pluginDriver.hookParallel('renderStart', [this.outputOptions, this.inputOptions]);
if (this.chunks.length > 1) {
validateOptionsForMultiChunkOutput(this.outputOptions);
}
const addons = await createAddons(this.outputOptions, this.pluginDriver);
for (const chunk of this.chunks) {
chunk.generateExports(this.outputOptions);
}
for (const chunk of this.chunks) {
chunk.preRender(this.outputOptions, inputBase, this.pluginDriver);
}
this.assignChunkIds(inputBase, addons, outputBundle);
assignChunksToBundle(this.chunks, outputBundle);
await Promise.all(
this.chunks.map(chunk => {
const outputChunk = outputBundle[chunk.id!] as OutputChunk;
return chunk
.render(this.outputOptions, addons, outputChunk, this.pluginDriver)
.then(rendered => {
outputChunk.code = rendered.code;
outputChunk.map = rendered.map;
});
})
);
} catch (error) {
await this.pluginDriver.hookParallel('renderError', [error]);
throw error;
}
await this.pluginDriver.hookSeq('generateBundle', [
this.outputOptions,
outputBundle as OutputBundle,
isWrite
]);
for (const key of Object.keys(outputBundle)) {
const file = outputBundle[key] as any;
if (!file.type) {
warnDeprecation(
'A plugin is directly adding properties to the bundle object in the "generateBundle" hook. This is deprecated and will be removed in a future Rollup version, please use "this.emitFile" instead.',
true,
this.inputOptions
);
file.type = 'asset';
}
}
this.pluginDriver.finaliseAssets();
timeEnd('GENERATE', 1);
return outputBundle as OutputBundle;
}
private assignChunkIds(inputBase: string, addons: Addons, bundle: OutputBundleWithPlaceholders) {
const entryChunks: Chunk[] = [];
const otherChunks: Chunk[] = [];
for (const chunk of this.chunks) {
(chunk.facadeModule && chunk.facadeModule.isUserDefinedEntryPoint
? entryChunks
: otherChunks
).push(chunk);
}
// make sure entry chunk names take precedence with regard to deconflicting
const chunksForNaming: Chunk[] = entryChunks.concat(otherChunks);
for (const chunk of chunksForNaming) {
if (this.outputOptions.file) {
chunk.id = basename(this.outputOptions.file);
} else if (this.inputOptions.preserveModules) {
chunk.id = chunk.generateIdPreserveModules(
inputBase,
this.outputOptions,
bundle,
this.unsetOptions
);
} else {
chunk.id = chunk.generateId(addons, this.outputOptions, bundle, true, this.pluginDriver);
}
bundle[chunk.id] = FILE_PLACEHOLDER;
}
}
}
function getAbsoluteEntryModulePaths(chunks: Chunk[]): string[] {
const absoluteEntryModulePaths: string[] = [];
for (const chunk of chunks) {
for (const entryModule of chunk.entryModules) {
if (isAbsolute(entryModule.id)) {
absoluteEntryModulePaths.push(entryModule.id);
}
}
}
return absoluteEntryModulePaths;
}
function validateOptionsForMultiChunkOutput(outputOptions: NormalizedOutputOptions) {
if (outputOptions.format === 'umd' || outputOptions.format === 'iife')
return error({
code: 'INVALID_OPTION',
message: 'UMD and IIFE output formats are not supported for code-splitting builds.'
});
if (typeof outputOptions.file === 'string')
return error({
code: 'INVALID_OPTION',
message:
'When building multiple chunks, the "output.dir" option must be used, not "output.file". ' +
'To inline dynamic imports, set the "inlineDynamicImports" option.'
});
if (outputOptions.sourcemapFile)
return error({
code: 'INVALID_OPTION',
message: '"output.sourcemapFile" is only supported for single-file builds.'
});
}
function assignChunksToBundle(
chunks: Chunk[],
outputBundle: OutputBundleWithPlaceholders
): OutputBundle {
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
const facadeModule = chunk.facadeModule;
outputBundle[chunk.id!] = {
code: undefined as any,
dynamicImports: chunk.getDynamicImportIds(),
exports: chunk.getExportNames(),
facadeModuleId: facadeModule && facadeModule.id,
fileName: chunk.id,
imports: chunk.getImportIds(),
isDynamicEntry: chunk.isDynamicEntry,
isEntry: facadeModule !== null && facadeModule.isEntryPoint,
map: undefined,
modules: chunk.renderedModules,
get name() {
return chunk.getChunkName();
},
type: 'chunk'
} as OutputChunk;
}
return outputBundle as OutputBundle;
}