forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
117 lines (106 loc) · 3.31 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
import { realpathSync } from 'fs';
import relative from 'require-relative';
import { InputOptions, WarningHandler } from '../../../src/rollup/types';
import mergeOptions from '../../../src/utils/mergeOptions';
import { getAliasName } from '../../../src/utils/relativeId';
import { handleError } from '../logging';
import batchWarnings from './batchWarnings';
import build from './build';
import loadConfigFile from './loadConfigFile';
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) {
process.env[key] = value;
} 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';
loadConfigFile(configFile, command)
.then(configs => execute(configFile, configs, command))
.catch(handleError);
} else {
return execute(configFile, [{ input: null }] as any, command);
}
}
function execute(configFile: string, configs: InputOptions[], command: any) {
if (command.watch) {
watch(configFile, configs, command, command.silent);
} else {
let promise = Promise.resolve();
for (const config of configs) {
promise = promise.then(() => {
const warnings = batchWarnings();
const { inputOptions, outputOptions, optionError } = mergeOptions({
command,
config,
defaultOnWarnHandler: warnings.add
});
if (optionError)
(inputOptions.onwarn as WarningHandler)({ code: 'UNKNOWN_OPTION', message: optionError });
return build(inputOptions, outputOptions, warnings, command.silent);
});
}
return promise;
}
}