-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
runRollup.js
287 lines (240 loc) · 6.81 KB
/
runRollup.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
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
import path from 'path';
import { realpathSync } from 'fs';
import * as rollup from 'rollup';
import relative from 'require-relative';
import chalk from 'chalk';
import { handleWarning, handleError, stderr } from './logging.js';
import SOURCEMAPPING_URL from './sourceMappingUrl.js';
import { install as installSourcemapSupport } from 'source-map-support';
installSourcemapSupport();
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 ) {
command.environment.split( ',' ).forEach( pair => {
const index = pair.indexOf( ':' );
if ( ~index ) {
process.env[ pair.slice( 0, index ) ] = pair.slice( index + 1 );
} else {
process.env[ pair ] = true;
}
});
}
let config = command.config === true ? 'rollup.config.js' : command.config;
if ( config ) {
if ( config.slice( 0, 5 ) === 'node:' ) {
const pkgName = config.slice( 5 );
try {
config = relative.resolve( `rollup-config-${pkgName}`, process.cwd() );
} catch ( err ) {
try {
config = relative.resolve( pkgName, process.cwd() );
} catch ( err ) {
if ( err.code === 'MODULE_NOT_FOUND' ) {
handleError({
code: 'MISSING_EXTERNAL_CONFIG',
message: `Could not resolve config file ${config}`
});
}
throw err;
}
}
} else {
// find real path of config so it matches what Node provides to callbacks in require.extensions
config = realpathSync( config );
}
rollup.rollup({
entry: config,
external: id => {
return (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5,id.length) === '.json';
},
onwarn: handleWarning
})
.then( bundle => {
return bundle.generate({
format: 'cjs'
});
})
.then( ({ code }) => {
if ( command.watch ) process.env.ROLLUP_WATCH = 'true';
// temporarily override require
const defaultLoader = require.extensions[ '.js' ];
require.extensions[ '.js' ] = ( m, filename ) => {
if ( filename === config ) {
m._compile( code, filename );
} else {
defaultLoader( m, filename );
}
};
const configs = require( config );
const normalized = Array.isArray( configs ) ? configs : [configs];
normalized.forEach(options => {
if ( Object.keys( options ).length === 0 ) {
handleError({
code: 'MISSING_CONFIG',
message: 'Config file must export an options object',
url: 'https://github.com/rollup/rollup/wiki/Command-Line-Interface#using-a-config-file'
});
}
execute( options, command );
});
require.extensions[ '.js' ] = defaultLoader;
})
.catch( handleError );
} else {
execute( {}, command );
}
}
const equivalents = {
useStrict: 'useStrict',
banner: 'banner',
footer: 'footer',
format: 'format',
globals: 'globals',
id: 'moduleId',
indent: 'indent',
input: 'entry',
intro: 'intro',
legacy: 'legacy',
name: 'moduleName',
output: 'dest',
outro: 'outro',
sourcemap: 'sourceMap',
treeshake: 'treeshake'
};
function execute ( options, command ) {
let external;
const commandExternal = ( command.external || '' ).split( ',' );
const optionsExternal = options.external;
if ( command.globals ) {
const globals = Object.create( null );
command.globals.split( ',' ).forEach( str => {
const names = str.split( ':' );
globals[ names[0] ] = names[1];
// Add missing Module IDs to external.
if ( commandExternal.indexOf( names[0] ) === -1 ) {
commandExternal.push( names[0] );
}
});
command.globals = globals;
}
if ( typeof optionsExternal === 'function' ) {
external = id => {
return optionsExternal( id ) || ~commandExternal.indexOf( id );
};
} else {
external = ( optionsExternal || [] ).concat( commandExternal );
}
if (typeof command.extend !== 'undefined') {
options.extend = command.extend;
}
if ( command.silent ) {
options.onwarn = () => {};
}
if ( !options.onwarn ) {
const seen = new Set();
options.onwarn = warning => {
const str = warning.toString();
if ( seen.has( str ) ) return;
seen.add( str );
handleWarning( warning );
};
}
options.external = external;
// Use any options passed through the CLI as overrides.
Object.keys( equivalents ).forEach( cliOption => {
if ( command.hasOwnProperty( cliOption ) ) {
options[ equivalents[ cliOption ] ] = command[ cliOption ];
}
});
if ( command.watch ) {
if ( !options.entry || ( !options.dest && !options.targets ) ) {
handleError({
code: 'WATCHER_MISSING_INPUT_OR_OUTPUT',
message: 'must specify --input and --output when using rollup --watch'
});
}
try {
const watch = relative( 'rollup-watch', process.cwd() );
const watcher = watch( rollup, options );
watcher.on( 'event', event => {
switch ( event.code ) {
case 'STARTING': // TODO this isn't emitted by newer versions of rollup-watch
stderr( 'checking rollup-watch version...' );
break;
case 'BUILD_START':
stderr( 'bundling...' );
break;
case 'BUILD_END':
stderr( 'bundled in ' + event.duration + 'ms. Watching for changes...' );
break;
case 'ERROR':
handleError( event.error, true );
break;
default:
stderr( 'unknown event', event );
}
});
} catch ( err ) {
if ( err.code === 'MODULE_NOT_FOUND' ) {
handleError({
code: 'ROLLUP_WATCH_NOT_INSTALLED',
message: 'rollup --watch depends on the rollup-watch package, which could not be found. Install it with npm install -D rollup-watch'
});
}
handleError( err );
}
} else {
bundle( options ).catch( handleError );
}
}
function clone ( object ) {
return assign( {}, object );
}
function assign ( target, source ) {
Object.keys( source ).forEach( key => {
target[ key ] = source[ key ];
});
return target;
}
function bundle ( options ) {
return rollup.rollup( options )
.then( bundle => {
if ( options.dest ) {
return bundle.write( options );
}
if ( options.targets ) {
let result = null;
options.targets.forEach( target => {
result = bundle.write( assign( clone( options ), target ) );
});
return result;
}
if ( options.sourceMap && options.sourceMap !== 'inline' ) {
handleError({
code: 'MISSING_OUTPUT_OPTION',
message: 'You must specify an --output (-o) option when creating a file with a sourcemap'
});
}
return bundle.generate(options).then( ({ code, map }) => {
if ( options.sourceMap === 'inline' ) {
code += `\n//# ${SOURCEMAPPING_URL}=${map.toUrl()}\n`;
}
process.stdout.write( code );
});
})
.catch( handleError );
}