-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.ts
510 lines (466 loc) · 16.1 KB
/
index.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import Chunk from '../Chunk';
import { optimizeChunks } from '../chunk-optimization';
import Graph from '../Graph';
import { createAddons } from '../utils/addons';
import { createAssetPluginHooks, finaliseAsset } from '../utils/assetHooks';
import commondir from '../utils/commondir';
import { Deprecation } from '../utils/deprecateOptions';
import error from '../utils/error';
import { writeFile } from '../utils/fs';
import getExportMode from '../utils/getExportMode';
import mergeOptions, { GenericConfigObject } from '../utils/mergeOptions';
import { basename, dirname, resolve } from '../utils/path';
import { SOURCEMAPPING_URL } from '../utils/sourceMappingURL';
import { getTimings, initialiseTimers, timeEnd, timeStart } from '../utils/timers';
import { Watcher } from '../watch';
import {
InputOptions,
OutputBundle,
OutputChunk,
OutputFile,
OutputOptions,
Plugin,
RollupBuild,
RollupSingleFileBuild,
WarningHandler
} from './types';
function addDeprecations(deprecations: Deprecation[], warn: WarningHandler) {
const message = `The following options have been renamed — please update your config: ${deprecations
.map(option => `${option.old} -> ${option.new}`)
.join(', ')}`;
warn({
code: 'DEPRECATED_OPTIONS',
message,
deprecations
});
}
function checkInputOptions(options: InputOptions) {
if (options.transform || options.load || options.resolveId || options.resolveExternal) {
throw new Error(
'The `transform`, `load`, `resolveId` and `resolveExternal` options are deprecated in favour of a unified plugin API. See https://github.com/rollup/rollup/wiki/Plugins for details'
);
}
}
function checkOutputOptions(options: OutputOptions) {
if (<string>options.format === 'es6') {
error({
message: 'The `es6` output format is deprecated – use `es` instead',
url: `https://rollupjs.org/#format-f-output-format-`
});
}
if (!options.format) {
error({
message: `You must specify output.format, which can be one of 'amd', 'cjs', 'system', 'esm', 'iife' or 'umd'`,
url: `https://rollupjs.org/#format-f-output-format-`
});
}
if (options.moduleId) {
if (options.amd) throw new Error('Cannot have both output.amd and output.moduleId');
}
}
const throwAsyncGenerateError = {
get() {
throw new Error(`bundle.generate(...) now returns a Promise instead of a { code, map } object`);
}
};
function applyOptionHook(inputOptions: InputOptions, plugin: Plugin) {
if (plugin.options) return plugin.options(inputOptions) || inputOptions;
return inputOptions;
}
function getInputOptions(rawInputOptions: GenericConfigObject): any {
if (!rawInputOptions) {
throw new Error('You must supply an options object to rollup');
}
let { inputOptions, deprecations, optionError } = mergeOptions({
config: rawInputOptions,
deprecateConfig: { input: true }
});
if (optionError) inputOptions.onwarn({ message: optionError, code: 'UNKNOWN_OPTION' });
if (deprecations.length) addDeprecations(deprecations, inputOptions.onwarn);
checkInputOptions(inputOptions);
const plugins = inputOptions.plugins;
inputOptions.plugins = Array.isArray(plugins) ? plugins : plugins ? [plugins] : [];
inputOptions = inputOptions.plugins.reduce(applyOptionHook, inputOptions);
if (!inputOptions.experimentalCodeSplitting) {
inputOptions.inlineDynamicImports = true;
if (inputOptions.manualChunks)
error({
code: 'INVALID_OPTION',
message: '"manualChunks" option is only supported for experimentalCodeSplitting.'
});
if (inputOptions.optimizeChunks)
error({
code: 'INVALID_OPTION',
message: '"optimizeChunks" option is only supported for experimentalCodeSplitting.'
});
if (inputOptions.input instanceof Array || typeof inputOptions.input === 'object')
error({
code: 'INVALID_OPTION',
message: 'Multiple inputs are only supported for experimentalCodeSplitting.'
});
}
if (inputOptions.inlineDynamicImports) {
if (inputOptions.manualChunks)
error({
code: 'INVALID_OPTION',
message: '"manualChunks" option is not supported for inlineDynamicImports.'
});
if (inputOptions.optimizeChunks)
error({
code: 'INVALID_OPTION',
message: '"optimizeChunks" option is not supported for inlineDynamicImports.'
});
if (inputOptions.input instanceof Array || typeof inputOptions.input === 'object')
error({
code: 'INVALID_OPTION',
message: 'Multiple inputs are not supported for inlineDynamicImports.'
});
} else if (inputOptions.experimentalPreserveModules) {
if (inputOptions.inlineDynamicImports)
error({
code: 'INVALID_OPTION',
message: `experimentalPreserveModules does not support the inlineDynamicImports option.`
});
if (inputOptions.manualChunks)
error({
code: 'INVALID_OPTION',
message: 'experimentalPreserveModules does not support the manualChunks option.'
});
if (inputOptions.optimizeChunks)
error({
code: 'INVALID_OPTION',
message: 'experimentalPreserveModules does not support the optimizeChunks option.'
});
}
return inputOptions;
}
let curWatcher: Watcher;
export function setWatcher(watcher: Watcher) {
curWatcher = watcher;
}
export default function rollup(
rawInputOptions: GenericConfigObject
): Promise<RollupSingleFileBuild | RollupBuild> {
try {
const inputOptions = getInputOptions(rawInputOptions);
initialiseTimers(inputOptions);
const graph = new Graph(inputOptions, curWatcher);
curWatcher = undefined;
timeStart('BUILD', 1);
return graph.pluginDriver
.hookParallel('buildStart')
.then(() =>
graph.build(
inputOptions.input,
inputOptions.manualChunks,
inputOptions.inlineDynamicImports,
inputOptions.experimentalPreserveModules
)
)
.then(
chunks =>
graph.pluginDriver.hookParallel('buildEnd').then(() => {
return chunks;
}),
err =>
graph.pluginDriver.hookParallel('buildEnd', err).then(() => {
throw err;
})
)
.then(chunks => {
timeEnd('BUILD', 1);
// TODO: deprecate legacy single chunk return
let singleChunk: Chunk | void;
const singleInput =
typeof inputOptions.input === 'string' ||
(inputOptions.input instanceof Array && inputOptions.input.length === 1);
//let imports: string[], exports: string[];
if (!inputOptions.experimentalPreserveModules) {
if (singleInput) {
for (const chunk of chunks) {
if (chunk.entryModule === undefined) continue;
if (singleChunk) {
singleChunk = undefined;
break;
}
singleChunk = chunk;
}
}
}
// ensure we only do one optimization pass per build
let optimized = false;
function generate(rawOutputOptions: GenericConfigObject, isWrite: boolean) {
const outputOptions = normalizeOutputOptions(inputOptions, rawOutputOptions);
if (inputOptions.experimentalCodeSplitting) {
if (typeof outputOptions.file === 'string' && typeof outputOptions.dir === 'string')
error({
code: 'INVALID_OPTION',
message:
'Build must set either output.file for a single-file build or output.dir when generating multiple chunks.'
});
if (chunks.length > 1) {
if (outputOptions.format === 'umd' || outputOptions.format === 'iife')
error({
code: 'INVALID_OPTION',
message:
'UMD and IIFE output formats are not supported with the experimentalCodeSplitting option.'
});
if (outputOptions.sourcemapFile)
error({
code: 'INVALID_OPTION',
message: '"sourcemapFile" is only supported for single-file builds.'
});
}
if (!singleChunk && typeof outputOptions.file === 'string')
error({
code: 'INVALID_OPTION',
message: singleInput
? 'When building a bundle using dynamic imports, the output.dir option must be used, not output.file. Alternatively set inlineDynamicImports: true to output a single file.'
: 'When building multiple entry point inputs, the output.dir option must be used, not output.file.'
});
}
if (!outputOptions.file && inputOptions.experimentalCodeSplitting)
singleChunk = undefined;
timeStart('GENERATE', 1);
// populate asset files into output
const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]';
const outputBundle: OutputBundle = graph.finaliseAssets(assetFileNames);
const inputBase = commondir(
chunks.filter(chunk => chunk.entryModule).map(chunk => chunk.entryModule.id)
);
return createAddons(graph, outputOptions)
.then(addons => {
// pre-render all chunks
for (const chunk of chunks) {
if (!inputOptions.experimentalPreserveModules)
chunk.generateInternalExports(outputOptions);
if (chunk.isEntryModuleFacade)
chunk.exportMode = getExportMode(chunk, outputOptions);
}
for (const chunk of chunks) {
chunk.preRender(outputOptions, inputBase);
}
if (!optimized && inputOptions.optimizeChunks) {
optimizeChunks(chunks, outputOptions, inputOptions.chunkGroupingSize, inputBase);
optimized = true;
}
// then name all chunks
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
const imports = chunk.getImportIds();
const exports = chunk.getExportNames();
const modules = chunk.renderedModules;
if (chunk === singleChunk) {
singleChunk.id = basename(
outputOptions.file ||
(inputOptions.input instanceof Array
? inputOptions.input[0]
: <string>inputOptions.input)
);
const outputChunk: OutputChunk = {
imports,
exports,
modules,
code: undefined,
map: undefined
};
outputBundle[singleChunk.id] = outputChunk;
} else if (inputOptions.experimentalPreserveModules) {
chunk.generateIdPreserveModules(inputBase);
} else {
let pattern, patternName;
if (chunk.isEntryModuleFacade) {
pattern = outputOptions.entryFileNames || '[name].js';
patternName = 'output.entryFileNames';
} else {
pattern = outputOptions.chunkFileNames || '[name]-[hash].js';
patternName = 'output.chunkFileNames';
}
chunk.generateId(pattern, patternName, addons, outputOptions, outputBundle);
}
outputBundle[chunk.id] = {
imports,
exports,
modules,
code: undefined,
map: undefined
};
}
// render chunk import statements and finalizer wrappers given known names
return Promise.all(
chunks.map(chunk => {
const chunkId = chunk.id;
return chunk.render(outputOptions, addons).then(rendered => {
const outputChunk = <OutputChunk>outputBundle[chunkId];
outputChunk.code = rendered.code;
outputChunk.map = rendered.map;
return graph.pluginDriver.hookParallel('ongenerate', [
{ bundle: outputChunk, ...outputOptions },
outputChunk
]);
});
})
).then(() => {});
})
.then(() => {
// run generateBundle hook
// assets emitted during generateBundle are unique to that specific generate call
const assets = new Map(graph.assetsById);
const generateAssetPluginHooks = createAssetPluginHooks(
assets,
graph.watchFiles,
outputBundle,
assetFileNames
);
return graph.pluginDriver
.hookSeq('generateBundle', [outputOptions, outputBundle, isWrite], context => ({
...context,
...generateAssetPluginHooks
}))
.then(() => {
// throw errors for assets not finalised with a source
assets.forEach(asset => {
if (asset.fileName === undefined)
finaliseAsset(asset, outputBundle, assetFileNames);
});
});
})
.then(() => {
timeEnd('GENERATE', 1);
return outputBundle;
});
}
const cache = rawInputOptions.cache === false ? undefined : graph.getCache();
const result: RollupSingleFileBuild | RollupBuild = {
cache,
watchFiles: Object.keys(graph.watchFiles),
generate: <any>((rawOutputOptions: GenericConfigObject) => {
const promise = generate(rawOutputOptions, false).then(
result =>
inputOptions.experimentalCodeSplitting
? { output: result }
: <OutputChunk>result[chunks[0].id]
);
Object.defineProperty(promise, 'code', throwAsyncGenerateError);
Object.defineProperty(promise, 'map', throwAsyncGenerateError);
return promise;
}),
write: <any>((outputOptions: OutputOptions) => {
if (
inputOptions.experimentalCodeSplitting &&
(!outputOptions || (!outputOptions.dir && !outputOptions.file))
) {
error({
code: 'MISSING_OPTION',
message: 'You must specify output.file or output.dir for the build.'
});
} else if (
!inputOptions.experimentalCodeSplitting &&
(!outputOptions || !outputOptions.file)
) {
error({
code: 'MISSING_OPTION',
message: 'You must specify output.file.'
});
}
return generate(outputOptions, true).then(outputBundle =>
Promise.all(
Object.keys(outputBundle).map(chunkId => {
return writeOutputFile(
graph,
result,
chunkId,
outputBundle[chunkId],
outputOptions
);
})
).then(
() =>
inputOptions.experimentalCodeSplitting
? { output: outputBundle }
: <OutputChunk>outputBundle[chunks[0].id]
)
);
})
};
if (!inputOptions.experimentalCodeSplitting) {
(<any>result).imports = (<Chunk>singleChunk).getImportIds();
(<any>result).exports = (<Chunk>singleChunk).getExportNames();
(<any>result).modules = (cache || graph.getCache()).modules;
}
if (inputOptions.perf === true) result.getTimings = getTimings;
return result;
});
} catch (err) {
return Promise.reject(err);
}
}
function isOutputChunk(file: OutputFile): file is OutputChunk {
return typeof (<OutputChunk>file).code === 'string';
}
function writeOutputFile(
graph: Graph,
build: RollupBuild | RollupSingleFileBuild,
outputFileName: string,
outputFile: OutputFile,
outputOptions: OutputOptions
): Promise<void> {
const filename = resolve(outputOptions.dir || dirname(outputOptions.file), outputFileName);
let writeSourceMapPromise: Promise<void>;
let source: string | Buffer;
if (isOutputChunk(outputFile)) {
source = outputFile.code;
if (outputOptions.sourcemap && outputFile.map) {
let url: string;
if (outputOptions.sourcemap === 'inline') {
url = outputFile.map.toUrl();
} else {
url = `${basename(outputFileName)}.map`;
writeSourceMapPromise = writeFile(`${filename}.map`, outputFile.map.toString());
}
source += `//# ${SOURCEMAPPING_URL}=${url}\n`;
}
} else {
source = outputFile;
}
return writeFile(filename, source)
.then(() => writeSourceMapPromise)
.then(
() =>
isOutputChunk(outputFile) &&
graph.pluginDriver.hookSeq('onwrite', [
{
bundle: build,
...outputOptions
},
outputFile
])
)
.then(() => {});
}
function normalizeOutputOptions(
inputOptions: GenericConfigObject,
rawOutputOptions: GenericConfigObject
): OutputOptions {
if (!rawOutputOptions) {
throw new Error('You must supply an options object');
}
// since deprecateOptions, adds the output properties
// to `inputOptions` so adding that lastly
const consolidatedOutputOptions = {
output: { ...rawOutputOptions, ...rawOutputOptions.output, ...inputOptions.output }
};
const mergedOptions = mergeOptions({
// just for backward compatiblity to fallback on root
// if the option isn't present in `output`
config: consolidatedOutputOptions,
deprecateConfig: { output: true }
});
if (mergedOptions.optionError) throw new Error(mergedOptions.optionError);
// now outputOptions is an array, but rollup.rollup API doesn't support arrays
const outputOptions = mergedOptions.outputOptions[0];
const deprecations = mergedOptions.deprecations;
if (deprecations.length) addDeprecations(deprecations, inputOptions.onwarn);
checkOutputOptions(outputOptions);
return outputOptions;
}