-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.ts
118 lines (108 loc) · 3.32 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
import { realpathSync } from 'fs';
import relative from 'require-relative';
import { mergeOptions } from '../../src/utils/mergeOptions';
import { GenericConfigObject } from '../../src/utils/parseOptions';
import { getAliasName } from '../../src/utils/relativeId';
import { handleError } from '../logging';
import batchWarnings from './batchWarnings';
import build from './build';
import loadConfigFile from './loadConfigFile';
import { stdinName, stdinPlugin } from './stdin';
import watch from './watch';
export default function runRollup(command: any) {
let inputSource;
if (command._.length > 0) {
if (command.input) {
handleError({
code: 'DUPLICATE_IMPORT_OPTIONS',
message: 'use --input, or pass input path as argument'
});
}
inputSource = command._;
} else if (typeof command.input === 'string') {
inputSource = [command.input];
} else {
inputSource = command.input;
}
if (inputSource && inputSource.length > 0) {
if (inputSource.some((input: string) => input.indexOf('=') !== -1)) {
command.input = {};
inputSource.forEach((input: string) => {
const equalsIndex = input.indexOf('=');
const value = input.substr(equalsIndex + 1);
let key = input.substr(0, equalsIndex);
if (!key) key = getAliasName(input);
command.input[key] = value;
});
} else {
command.input = inputSource;
}
}
if (command.environment) {
const environment = Array.isArray(command.environment)
? command.environment
: [command.environment];
environment.forEach((arg: string) => {
arg.split(',').forEach((pair: string) => {
const [key, ...value] = pair.split(':');
if (value.length) {
process.env[key] = value.join(':');
} else {
process.env[key] = String(true);
}
});
});
}
let configFile = command.config === true ? 'rollup.config.js' : command.config;
if (configFile) {
if (configFile.slice(0, 5) === 'node:') {
const pkgName = configFile.slice(5);
try {
configFile = relative.resolve(`rollup-config-${pkgName}`, process.cwd());
} catch (err) {
try {
configFile = relative.resolve(pkgName, process.cwd());
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
handleError({
code: 'MISSING_EXTERNAL_CONFIG',
message: `Could not resolve config file ${configFile}`
});
}
throw err;
}
}
} else {
// find real path of config so it matches what Node provides to callbacks in require.extensions
configFile = realpathSync(configFile);
}
if (command.watch) process.env.ROLLUP_WATCH = 'true';
return loadConfigFile(configFile, command)
.then(configs => execute(configFile, configs, command))
.catch(handleError);
} else {
if (!command.input && (command.stdin || !process.stdin.isTTY)) {
command.input = stdinName;
}
return execute(configFile, [{ input: [] }], command);
}
}
async function execute(
configFile: string,
configs: GenericConfigObject[],
command: any
): Promise<void> {
if (command.watch) {
watch(configFile, configs, command, command.silent);
} else {
for (const config of configs) {
const warnings = batchWarnings();
const { inputOptions, outputOptions } = mergeOptions(config, command,
warnings.add);
if (command.stdin !== false) {
inputOptions.plugins!.push(stdinPlugin());
}
await build(inputOptions, outputOptions, warnings, command.silent);
}
}
}