/
rollup.ts
421 lines (386 loc) · 13.2 KB
/
rollup.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { version as rollupVersion } from 'package.json';
import Chunk from '../Chunk';
import Graph from '../Graph';
import { createAddons } from '../utils/addons';
import { assignChunkIds } from '../utils/assignChunkIds';
import commondir from '../utils/commondir';
import { errCannotEmitFromOptionsHook, error } from '../utils/error';
import { writeFile } from '../utils/fs';
import getExportMode from '../utils/getExportMode';
import {
ensureArray,
GenericConfigObject,
parseInputOptions,
parseOutputOptions
} from '../utils/parseOptions';
import { basename, dirname, isAbsolute, resolve } from '../utils/path';
import { PluginDriver } from '../utils/PluginDriver';
import { ANONYMOUS_OUTPUT_PLUGIN_PREFIX, ANONYMOUS_PLUGIN_PREFIX } from '../utils/pluginUtils';
import { SOURCEMAPPING_URL } from '../utils/sourceMappingURL';
import { getTimings, initialiseTimers, timeEnd, timeStart } from '../utils/timers';
import {
InputOptions,
OutputAsset,
OutputBundle,
OutputBundleWithPlaceholders,
OutputChunk,
OutputOptions,
Plugin,
RollupBuild,
RollupOutput,
RollupWatcher,
WarningHandler
} from './types';
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 applyOptionHook(inputOptions: InputOptions, plugin: Plugin) {
if (plugin.options)
return plugin.options.call({ meta: { rollupVersion } }, inputOptions) || inputOptions;
return inputOptions;
}
function normalizePlugins(rawPlugins: any, anonymousPrefix: string): Plugin[] {
const plugins = ensureArray(rawPlugins);
for (let pluginIndex = 0; pluginIndex < plugins.length; pluginIndex++) {
const plugin = plugins[pluginIndex];
if (!plugin.name) {
plugin.name = `${anonymousPrefix}${pluginIndex + 1}`;
}
}
return plugins;
}
function getInputOptions(rawInputOptions: GenericConfigObject): InputOptions {
if (!rawInputOptions) {
throw new Error('You must supply an options object to rollup');
}
let inputOptions = parseInputOptions(rawInputOptions);
inputOptions = inputOptions.plugins!.reduce(applyOptionHook, inputOptions);
inputOptions.plugins = normalizePlugins(inputOptions.plugins!, ANONYMOUS_PLUGIN_PREFIX);
if (inputOptions.inlineDynamicImports) {
if (inputOptions.preserveModules)
return error({
code: 'INVALID_OPTION',
message: `"preserveModules" does not support the "inlineDynamicImports" option.`
});
if (inputOptions.manualChunks)
return error({
code: 'INVALID_OPTION',
message: '"manualChunks" option is not supported for "inlineDynamicImports".'
});
if (
(inputOptions.input instanceof Array && inputOptions.input.length > 1) ||
(typeof inputOptions.input === 'object' && Object.keys(inputOptions.input).length > 1)
)
return error({
code: 'INVALID_OPTION',
message: 'Multiple inputs are not supported for "inlineDynamicImports".'
});
} else if (inputOptions.preserveModules) {
if (inputOptions.manualChunks)
return error({
code: 'INVALID_OPTION',
message: '"preserveModules" does not support the "manualChunks" option.'
});
}
return inputOptions;
}
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: facadeModule !== null && facadeModule.dynamicallyImportedBy.length > 0,
isEntry: facadeModule !== null && facadeModule.isEntryPoint,
map: undefined,
modules: chunk.renderedModules,
get name() {
return chunk.getChunkName();
},
type: 'chunk'
} as OutputChunk;
}
return outputBundle as OutputBundle;
}
export default function rollup(rawInputOptions: GenericConfigObject): Promise<RollupBuild> {
return rollupInternal(rawInputOptions, null);
}
export async function rollupInternal(
rawInputOptions: GenericConfigObject,
watcher: RollupWatcher | null
): Promise<RollupBuild> {
const inputOptions = getInputOptions(rawInputOptions);
initialiseTimers(inputOptions);
const graph = new Graph(inputOptions, watcher);
// remove the cache option from the memory after graph creation (cache is not used anymore)
const useCache = rawInputOptions.cache !== false;
delete inputOptions.cache;
delete rawInputOptions.cache;
timeStart('BUILD', 1);
let chunks: Chunk[];
try {
await graph.pluginDriver.hookParallel('buildStart', [inputOptions]);
chunks = await graph.build(
inputOptions.input as string | string[] | Record<string, string>,
inputOptions.manualChunks,
inputOptions.inlineDynamicImports!
);
} catch (err) {
const watchFiles = Object.keys(graph.watchFiles);
if (watchFiles.length > 0) {
err.watchFiles = watchFiles;
}
await graph.pluginDriver.hookParallel('buildEnd', [err]);
throw err;
}
await graph.pluginDriver.hookParallel('buildEnd', []);
timeEnd('BUILD', 1);
function getOutputOptionsAndPluginDriver(
rawOutputOptions: GenericConfigObject
): { outputOptions: OutputOptions; outputPluginDriver: PluginDriver } {
if (!rawOutputOptions) {
throw new Error('You must supply an options object');
}
const outputPluginDriver = graph.pluginDriver.createOutputPluginDriver(
normalizePlugins(rawOutputOptions.plugins, ANONYMOUS_OUTPUT_PLUGIN_PREFIX)
);
return {
outputOptions: normalizeOutputOptions(
inputOptions as GenericConfigObject,
rawOutputOptions,
chunks.length > 1,
outputPluginDriver
),
outputPluginDriver
};
}
async function generate(
outputOptions: OutputOptions,
isWrite: boolean,
outputPluginDriver: PluginDriver
): Promise<OutputBundle> {
timeStart('GENERATE', 1);
const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]';
const inputBase = commondir(getAbsoluteEntryModulePaths(chunks));
const outputBundleWithPlaceholders: OutputBundleWithPlaceholders = Object.create(null);
outputPluginDriver.setOutputBundle(outputBundleWithPlaceholders, assetFileNames);
let outputBundle;
try {
await outputPluginDriver.hookParallel('renderStart', [outputOptions, inputOptions]);
const addons = await createAddons(outputOptions, outputPluginDriver);
for (const chunk of chunks) {
if (!inputOptions.preserveModules) chunk.generateInternalExports(outputOptions);
if (inputOptions.preserveModules || (chunk.facadeModule && chunk.facadeModule.isEntryPoint))
chunk.exportMode = getExportMode(chunk, outputOptions, chunk.facadeModule!.id);
}
for (const chunk of chunks) {
chunk.preRender(outputOptions, inputBase);
}
assignChunkIds(
chunks,
inputOptions,
outputOptions,
inputBase,
addons,
outputBundleWithPlaceholders,
outputPluginDriver
);
outputBundle = assignChunksToBundle(chunks, outputBundleWithPlaceholders);
await Promise.all(
chunks.map(chunk => {
const outputChunk = outputBundleWithPlaceholders[chunk.id!] as OutputChunk;
return chunk
.render(outputOptions, addons, outputChunk, outputPluginDriver)
.then(rendered => {
outputChunk.code = rendered.code;
outputChunk.map = rendered.map;
});
})
);
} catch (error) {
await outputPluginDriver.hookParallel('renderError', [error]);
throw error;
}
await outputPluginDriver.hookSeq('generateBundle', [outputOptions, outputBundle, isWrite]);
for (const key of Object.keys(outputBundle)) {
const file = outputBundle[key] as any;
if (!file.type) {
graph.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
);
file.type = 'asset';
}
}
outputPluginDriver.finaliseAssets();
timeEnd('GENERATE', 1);
return outputBundle;
}
const cache = useCache ? graph.getCache() : undefined;
const result: RollupBuild = {
cache: cache!,
generate: (rawOutputOptions: OutputOptions) => {
const { outputOptions, outputPluginDriver } = getOutputOptionsAndPluginDriver(
rawOutputOptions as GenericConfigObject
);
return generate(outputOptions, false, outputPluginDriver).then(result =>
createOutput(result)
);
},
watchFiles: Object.keys(graph.watchFiles),
write: (rawOutputOptions: OutputOptions) => {
const { outputOptions, outputPluginDriver } = getOutputOptionsAndPluginDriver(
rawOutputOptions as GenericConfigObject
);
if (!outputOptions.dir && !outputOptions.file) {
return error({
code: 'MISSING_OPTION',
message: 'You must specify "output.file" or "output.dir" for the build.'
});
}
return generate(outputOptions, true, outputPluginDriver).then(async bundle => {
await Promise.all(
Object.keys(bundle).map(chunkId => writeOutputFile(bundle[chunkId], outputOptions))
);
await outputPluginDriver.hookParallel('writeBundle', [outputOptions, bundle]);
return createOutput(bundle);
});
}
};
if (inputOptions.perf === true) result.getTimings = getTimings;
return result;
}
enum SortingFileType {
ENTRY_CHUNK = 0,
SECONDARY_CHUNK = 1,
ASSET = 2
}
function getSortingFileType(file: OutputAsset | OutputChunk): SortingFileType {
if (file.type === 'asset') {
return SortingFileType.ASSET;
}
if (file.isEntry) {
return SortingFileType.ENTRY_CHUNK;
}
return SortingFileType.SECONDARY_CHUNK;
}
function createOutput(outputBundle: Record<string, OutputChunk | OutputAsset | {}>): RollupOutput {
return {
output: (Object.keys(outputBundle)
.map(fileName => outputBundle[fileName])
.filter(outputFile => Object.keys(outputFile).length > 0) as (
| OutputChunk
| OutputAsset
)[]).sort((outputFileA, outputFileB) => {
const fileTypeA = getSortingFileType(outputFileA);
const fileTypeB = getSortingFileType(outputFileB);
if (fileTypeA === fileTypeB) return 0;
return fileTypeA < fileTypeB ? -1 : 1;
}) as [OutputChunk, ...(OutputChunk | OutputAsset)[]]
};
}
function writeOutputFile(
outputFile: OutputAsset | OutputChunk,
outputOptions: OutputOptions
): Promise<unknown> {
const fileName = resolve(outputOptions.dir || dirname(outputOptions.file!), outputFile.fileName);
let writeSourceMapPromise: Promise<void> | undefined;
let source: string | Uint8Array;
if (outputFile.type === 'asset') {
source = outputFile.source;
} else {
source = outputFile.code;
if (outputOptions.sourcemap && outputFile.map) {
let url: string;
if (outputOptions.sourcemap === 'inline') {
url = outputFile.map.toUrl();
} else {
url = `${basename(outputFile.fileName)}.map`;
writeSourceMapPromise = writeFile(`${fileName}.map`, outputFile.map.toString());
}
if (outputOptions.sourcemap !== 'hidden') {
source += `//# ${SOURCEMAPPING_URL}=${url}\n`;
}
}
}
return Promise.all([writeFile(fileName, source), writeSourceMapPromise]);
}
function normalizeOutputOptions(
inputOptions: GenericConfigObject,
rawOutputOptions: GenericConfigObject,
hasMultipleChunks: boolean,
outputPluginDriver: PluginDriver
): OutputOptions {
const outputOptions = parseOutputOptions(
outputPluginDriver.hookReduceArg0Sync(
'outputOptions',
[rawOutputOptions.output || inputOptions.output || rawOutputOptions] as [OutputOptions],
(outputOptions, result) => result || outputOptions,
pluginContext => {
const emitError = () => pluginContext.error(errCannotEmitFromOptionsHook());
return {
...pluginContext,
emitFile: emitError,
setAssetSource: emitError
};
}
) as GenericConfigObject,
inputOptions.onwarn as WarningHandler
);
if (typeof outputOptions.file === 'string') {
if (typeof outputOptions.dir === 'string')
return error({
code: 'INVALID_OPTION',
message:
'You must set either "output.file" for a single-file build or "output.dir" when generating multiple chunks.'
});
if (inputOptions.preserveModules) {
return error({
code: 'INVALID_OPTION',
message:
'You must set "output.dir" instead of "output.file" when using the "preserveModules" option.'
});
}
if (typeof inputOptions.input === 'object' && !Array.isArray(inputOptions.input))
return error({
code: 'INVALID_OPTION',
message: 'You must set "output.dir" instead of "output.file" when providing named inputs.'
});
}
if (hasMultipleChunks) {
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.'
});
}
return outputOptions;
}