-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.js
110 lines (95 loc) · 2.99 KB
/
index.js
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
import { realpathSync } from 'fs';
import relative from 'require-relative';
import { handleError } from '../logging.js';
import mergeOptions from './mergeOptions.js';
import batchWarnings from './batchWarnings.js';
import loadConfigFile from './loadConfigFile.js';
import sequence from '../utils/sequence.js';
import build from './build.js';
import watch from './watch.js';
export default function runRollup ( command ) {
if ( command._.length > 1 ) {
handleError({
code: 'ONE_AT_A_TIME',
message: 'rollup can only bundle one file at a time'
});
}
if ( command._.length === 1 ) {
if ( command.input ) {
handleError({
code: 'DUPLICATE_IMPORT_OPTIONS',
message: 'use --input, or pass input path as argument'
});
}
command.input = command._[0];
}
if ( command.environment ) {
const environment = Array.isArray(command.environment) ? command.environment : [ command.environment ];
environment.forEach( arg => {
arg.split( ',' ).forEach( pair => {
const [ key, value ] = pair.split( ':' );
if ( value ) {
process.env[ key ] = value;
} else {
process.env[ key ] = 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.silent)
.then(normalized => execute( configFile, normalized, command ))
.catch(handleError);
} else {
return execute( configFile, [{}], command );
}
}
function execute ( configFile, configs, command ) {
if ( command.watch ) {
watch( configFile, configs, command, command.silent );
} else {
return sequence( configs, config => {
const { inputOptions, outputOptions, deprecations } = mergeOptions( config, command );
const warnings = batchWarnings();
const onwarn = inputOptions.onwarn;
if ( onwarn ) {
inputOptions.onwarn = warning => {
onwarn( warning, warnings.add );
};
} else {
inputOptions.onwarn = warnings.add;
}
if (deprecations.length) {
inputOptions.onwarn({
code: 'DEPRECATED_OPTIONS',
message: `The following options have been renamed — please update your config: ${deprecations.map(option => `${option.old} -> ${option.new}`).join(', ')}`,
deprecations
});
}
return build( inputOptions, outputOptions, warnings, command.silent );
});
}
}