/
build.ts
88 lines (83 loc) · 2.83 KB
/
build.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
import ms from 'pretty-ms';
import tc from 'turbocolor';
import * as rollup from '../../src/node-entry';
import { InputOptions, OutputAsset, OutputChunk, OutputOptions, RollupBuild, SourceMap } from '../../src/rollup/types';
import relativeId from '../../src/utils/relativeId';
import { handleError, stderr } from '../logging';
import SOURCEMAPPING_URL from '../sourceMappingUrl';
import { BatchWarnings } from './batchWarnings';
import { printTimings } from './timings';
export default function build(
inputOptions: InputOptions,
outputOptions: OutputOptions[],
warnings: BatchWarnings,
silent = false
) {
const useStdout = !outputOptions[0].file && !outputOptions[0].dir;
const start = Date.now();
const files = useStdout
? ['stdout']
: outputOptions.map(t => relativeId(t.file || (t.dir as string)));
if (!silent) {
let inputFiles: string = undefined as any;
if (typeof inputOptions.input === 'string') {
inputFiles = inputOptions.input;
} else if (inputOptions.input instanceof Array) {
inputFiles = inputOptions.input.join(', ');
} else if (typeof inputOptions.input === 'object' && inputOptions.input !== null) {
inputFiles = Object.keys(inputOptions.input)
.map(name => (inputOptions.input as Record<string, string>)[name])
.join(', ');
}
stderr(tc.cyan(`\n${tc.bold(inputFiles)} → ${tc.bold(files.join(', '))}...`));
}
return rollup
.rollup(inputOptions as any)
.then((bundle: RollupBuild) => {
if (useStdout) {
const output = outputOptions[0];
if (output.sourcemap && output.sourcemap !== 'inline') {
handleError({
code: 'MISSING_OUTPUT_OPTION',
message: 'You must specify a --file (-o) option when creating a file with a sourcemap'
});
}
return bundle.generate(output).then(({ output: outputs }) => {
for (const file of outputs) {
let source: string | Buffer;
if ((file as OutputAsset).isAsset) {
source = (file as OutputAsset).source;
} else {
source = (file as OutputChunk).code;
if (output.sourcemap === 'inline') {
source += `\n//# ${SOURCEMAPPING_URL}=${((file as OutputChunk)
.map as SourceMap).toUrl()}\n`;
}
}
if (outputs.length > 1)
process.stdout.write('\n' + tc.cyan(tc.bold('//→ ' + file.fileName + ':')) + '\n');
process.stdout.write(source);
}
return null
});
}
return Promise.all(outputOptions.map(output => bundle.write(output) as Promise<any>)).then(
() => bundle
);
})
.then((bundle: RollupBuild | null) => {
if (!silent) {
warnings.flush();
stderr(
tc.green(`created ${tc.bold(files.join(', '))} in ${tc.bold(ms(Date.now() - start))}`)
);
if (bundle && bundle.getTimings) {
printTimings(bundle.getTimings());
}
}
})
.catch((err: any) => {
if (warnings.count > 0) warnings.flush();
handleError(err);
});
}