forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
486 lines (416 loc) · 13.7 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
import { getTimings, initialiseTimers, timeEnd, timeStart } from '../utils/timers';
import { basename, extname } from '../utils/path';
import { writeFile } from '../utils/fs';
import { mapSequence } from '../utils/promise';
import error from '../utils/error';
import { SOURCEMAPPING_URL } from '../utils/sourceMappingURL';
import mergeOptions, { GenericConfigObject } from '../utils/mergeOptions';
import { Deprecation } from '../utils/deprecateOptions';
import Graph from '../Graph';
import ensureArray from '../utils/ensureArray';
import { SourceMap } from 'magic-string';
import { createAddons } from '../utils/addons';
import commondir from '../utils/commondir';
import {
WarningHandler,
InputOptions,
OutputOptions,
SerializedTimings,
Plugin,
ModuleJSON
} from './types';
export const VERSION = '<@VERSION@>';
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 (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 options.format, which can be one of 'amd', 'cjs', 'system', 'es', 'iife' or 'umd'`,
url: `https://rollupjs.org/#format-f-output-format-`
});
}
if (options.moduleId) {
if (options.amd) throw new Error('Cannot have both options.amd and options.moduleId');
}
}
const throwAsyncGenerateError = {
get() {
throw new Error(`bundle.generate(...) now returns a Promise instead of a { code, map } object`);
}
};
export interface OutputChunk {
imports: string[];
exports: string[];
modules: string[];
code: string;
map?: SourceMap;
}
export interface Bundle {
// TODO: consider deprecating to match code splitting
imports: string[];
exports: string[];
modules: ModuleJSON[];
cache: {
modules: ModuleJSON[];
};
generate: (outputOptions: OutputOptions) => Promise<OutputChunk>;
write: (options: OutputOptions) => Promise<OutputChunk>;
getTimings?: () => SerializedTimings;
}
export interface BundleSet {
cache: {
modules: ModuleJSON[];
};
generate: (outputOptions: OutputOptions) => Promise<{ [chunkName: string]: OutputChunk }>;
write: (options: OutputOptions) => Promise<{ [chunkName: string]: OutputChunk }>;
getTimings?: () => SerializedTimings;
}
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');
}
const { 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);
inputOptions.plugins = ensureArray(inputOptions.plugins);
return inputOptions.plugins.reduce(applyOptionHook, inputOptions);
}
export default function rollup(
rawInputOptions: GenericConfigObject | InputOptions
): Promise<Bundle | BundleSet> {
try {
const inputOptions = getInputOptions(rawInputOptions);
initialiseTimers(inputOptions);
const graph = new Graph(inputOptions);
timeStart('BUILD', 1);
const codeSplitting =
(inputOptions.experimentalCodeSplitting && typeof inputOptions.input !== 'string') ||
inputOptions.experimentalPreserveModules;
if (!codeSplitting) {
if (inputOptions.manualChunks)
error({
code: 'INVALID_OPTION',
message: '"chunks" option is only supported for code-splitting builds.'
});
return graph.buildSingle(inputOptions.input).then(chunk => {
timeEnd('BUILD', 1);
const imports = chunk.getImportIds();
const exports = chunk.getExportNames();
const modules = graph.getCache().modules;
function generate(rawOutputOptions: GenericConfigObject) {
const outputOptions = normalizeOutputOptions(inputOptions, rawOutputOptions);
if (outputOptions.entryNames || outputOptions.chunkNames)
error({
code: 'INVALID_OPTION',
message:
'"entryNames" and "chunkNames" options are only supported for code-splitting builds.'
});
timeStart('GENERATE', 1);
const promise = Promise.resolve()
.then(() => {
return createAddons(graph, outputOptions);
})
.then(addons => {
chunk.generateInternalExports();
chunk.preRender(outputOptions);
return chunk.render(outputOptions, addons);
})
.then(rendered => {
timeEnd('GENERATE', 1);
const output = {
imports,
exports,
modules: chunk.getModuleIds(),
code: rendered.code,
map: rendered.map
};
graph.plugins.forEach(plugin => {
if (plugin.ongenerate) {
plugin.ongenerate(
Object.assign(
{
bundle: result
},
outputOptions
),
output
);
}
});
return output;
});
Object.defineProperty(promise, 'code', throwAsyncGenerateError);
Object.defineProperty(promise, 'map', throwAsyncGenerateError);
return promise;
}
const result: Bundle = {
imports,
exports,
modules,
cache: { modules },
generate,
write: (outputOptions: OutputOptions) => {
if (!outputOptions || (!outputOptions.file && !outputOptions.dest)) {
error({
code: 'MISSING_OPTION',
message: 'You must specify output.file'
});
}
return generate(outputOptions).then(result => {
const file = outputOptions.file;
let { code, map } = result;
const promises = [];
if (outputOptions.sourcemap) {
let url;
if (outputOptions.sourcemap === 'inline') {
url = map.toUrl();
} else {
url = `${basename(file)}.map`;
promises.push(writeFile(file + '.map', map.toString()));
}
code += `//# ${SOURCEMAPPING_URL}=${url}\n`;
}
const wasmModule = modules.find(x => extname(x.id) === '.wasm');
if (wasmModule !== undefined) {
promises.push(writeFile('./dist/addTwo.wasm', wasmModule.code));
}
promises.push(writeFile(file, code));
return (
Promise.all(promises)
.then(() =>
mapSequence(graph.plugins.filter(plugin => plugin.onwrite), (plugin: Plugin) =>
Promise.resolve(
plugin.onwrite(
Object.assign(
{
bundle: result
},
outputOptions
),
result
)
)
)
)
// ensures return isn't void[]
.then(() => result)
);
});
}
};
if (inputOptions.perf === true) {
result.getTimings = getTimings;
}
return result;
});
}
// code splitting case
if (inputOptions.experimentalPreserveModules && typeof inputOptions.input === 'string') {
inputOptions.input = [inputOptions.input];
}
if (!(Array.isArray(inputOptions.input) || typeof inputOptions.input === 'object')) {
error({
code: 'INVALID_OPTION',
message: 'When code-splitting, "input" must be an array or object of entry points.'
});
}
return graph
.buildChunks(
inputOptions.input,
inputOptions.manualChunks,
inputOptions.experimentalPreserveModules
)
.then(chunks => {
timeEnd('BUILD', 1);
function generate(rawOutputOptions: GenericConfigObject) {
const outputOptions = normalizeOutputOptions(inputOptions, rawOutputOptions);
if (typeof outputOptions.file === 'string')
error({
code: 'INVALID_OPTION',
message: 'When code-splitting, the "dir" output option must be used, not "file".'
});
if (outputOptions.format === 'umd' || outputOptions.format === 'iife') {
error({
code: 'INVALID_OPTION',
message:
'UMD and IIFE output formats are not supported with the experimentalCodeSplitting option.'
});
}
timeStart('GENERATE', 1);
const generated: { [chunkName: string]: OutputChunk } = {};
let preserveModulesBase: string;
if (inputOptions.experimentalPreserveModules)
preserveModulesBase = commondir(chunks.map(chunk => chunk.entryModule.id));
let existingNames = Object.create(null);
const promise = createAddons(graph, outputOptions)
.then(addons => {
// first pre-render all chunks
// then name all chunks
return (
Promise.resolve()
.then(() => {
if (!inputOptions.experimentalPreserveModules) {
const mangleExportNames =
outputOptions.format === 'system' || outputOptions.format === 'es';
for (let chunk of chunks) {
chunk.generateInternalExports(mangleExportNames);
}
}
for (let chunk of chunks) {
chunk.preRender(outputOptions);
}
for (let chunk of chunks) {
if (inputOptions.experimentalPreserveModules) {
chunk.generateNamePreserveModules(preserveModulesBase);
} else {
let pattern;
if (chunk.isEntryModuleFacade) {
pattern = outputOptions.entryNames || '[alias].js';
} else {
pattern = outputOptions.chunkNames || '[alias]-[hash].js';
}
chunk.generateName(pattern, addons, outputOptions, existingNames);
}
}
})
// second render chunks given known names
.then(() => {
return Promise.all(
chunks.map(chunk => {
return chunk.render(outputOptions, addons).then(rendered => {
const output = {
imports: chunk.getImportIds(),
exports: chunk.getExportNames(),
modules: chunk.getModuleIds(),
code: rendered.code,
map: rendered.map
};
graph.plugins.forEach(plugin => {
if (plugin.ongenerate) {
plugin.ongenerate(Object.assign(chunk, outputOptions), output);
}
});
generated[chunk.id] = output;
});
})
);
})
);
})
.then(() => {
timeEnd('GENERATE', 1);
return generated;
});
Object.defineProperty(promise, 'code', throwAsyncGenerateError);
Object.defineProperty(promise, 'map', throwAsyncGenerateError);
return promise;
}
const result: BundleSet = {
cache: graph.getCache(),
generate,
write(outputOptions: OutputOptions) {
if (!outputOptions || !outputOptions.dir) {
error({
code: 'MISSING_OPTION',
message: 'You must specify output.dir when code-splitting.'
});
}
return generate(outputOptions).then(result => {
const dir = outputOptions.dir;
return Promise.all(
Object.keys(result).map(chunkName => {
let chunk = result[chunkName];
let { code, map } = chunk;
const promises = [];
if (outputOptions.sourcemap) {
let url;
if (outputOptions.sourcemap === 'inline') {
url = (<any>map).toUrl();
} else {
url = `${chunkName}.map`;
promises.push(writeFile(dir + '/' + chunkName + '.map', map.toString()));
}
code += `//# ${SOURCEMAPPING_URL}=${url}\n`;
}
promises.push(writeFile(dir + '/' + chunkName, code));
return Promise.all(promises).then(() => {
return mapSequence(
graph.plugins.filter(plugin => plugin.onwrite),
(plugin: Plugin) =>
Promise.resolve(
plugin.onwrite(Object.assign({ bundle: chunk }, outputOptions), chunk)
)
);
});
})
).then(() => result);
});
}
};
if (inputOptions.perf === true) {
result.getTimings = getTimings;
}
return result;
});
} catch (err) {
return Promise.reject(err);
}
}
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 = Object.assign(
{},
{
output: Object.assign({}, 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;
}