UNPKG

3.33 kBJavaScriptView Raw
1require( 'source-map-support' ).install();
2
3var path = require( 'path' );
4var handleError = require( './handleError' );
5var rollup = require( '../' );
6
7// log to stderr to keep `rollup main.js > bundle.js` from breaking
8var log = console.error.bind(console);
9
10module.exports = function ( command ) {
11 if ( command._.length > 1 ) {
12 handleError({ code: 'ONE_AT_A_TIME' });
13 }
14
15 if ( command._.length === 1 ) {
16 if ( command.input ) {
17 handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' });
18 }
19
20 command.input = command._[0];
21 }
22
23 if ( command.environment ) {
24 command.environment.split( ',' ).forEach( function ( pair ) {
25 var index = pair.indexOf( ':' );
26 if ( ~index ) {
27 process.env[ pair.slice( 0, index ) ] = pair.slice( index + 1 );
28 } else {
29 process.env[ pair ] = true;
30 }
31 });
32 }
33
34 var config = command.config === true ? 'rollup.config.js' : command.config;
35
36 if ( config ) {
37 config = path.resolve( config );
38
39 rollup.rollup({
40 entry: config,
41 onwarn: function ( message ) {
42 if ( /Treating .+ as external dependency/.test( message ) ) return;
43 log( message );
44 }
45 }).then( function ( bundle ) {
46 var code = bundle.generate({
47 format: 'cjs'
48 }).code;
49
50 // temporarily override require
51 var defaultLoader = require.extensions[ '.js' ];
52 require.extensions[ '.js' ] = function ( m, filename ) {
53 if ( filename === config ) {
54 m._compile( code, filename );
55 } else {
56 defaultLoader( m, filename );
57 }
58 };
59
60 try {
61 var options = require( path.resolve( config ) );
62 } catch ( err ) {
63 handleError( err );
64 }
65
66 execute( options, command );
67
68 require.extensions[ '.js' ] = defaultLoader;
69 })
70 .catch(log);
71 } else {
72 execute( {}, command );
73 }
74};
75
76var equivalents = {
77 input: 'entry',
78 output: 'dest',
79 name: 'moduleName',
80 format: 'format',
81 globals: 'globals',
82 id: 'moduleId',
83 sourcemap: 'sourceMap'
84};
85
86function execute ( options, command ) {
87 var external = command.external ? command.external.split( ',' ) : [];
88
89 if ( command.globals ) {
90 var globals = Object.create( null );
91
92 command.globals.split( ',' ).forEach(function ( str ) {
93 var names = str.split( ':' );
94 globals[ names[0] ] = names[1];
95
96 // Add missing Module IDs to external.
97 if ( external.indexOf( names[0] ) === -1 ) {
98 external.push( names[0] );
99 }
100 });
101
102 command.globals = globals;
103 }
104
105 options.onwarn = options.onwarn || log;
106
107 options.external = external;
108 options.indent = command.indent !== false;
109
110 Object.keys( equivalents ).forEach( function ( cliOption ) {
111 if ( command[ cliOption ] ) {
112 options[ equivalents[ cliOption ] ] = command[ cliOption ];
113 }
114 });
115
116 try {
117 bundle( options ).catch( handleError );
118 } catch ( err ) {
119 handleError( err );
120 }
121}
122
123function bundle ( options ) {
124 if ( !options.entry ) {
125 handleError({ code: 'MISSING_INPUT_OPTION' });
126 }
127
128 return rollup.rollup( options ).then( function ( bundle ) {
129 if ( options.dest ) {
130 return bundle.write( options );
131 }
132
133 if ( options.sourceMap && options.sourceMap !== 'inline' ) {
134 handleError({ code: 'MISSING_OUTPUT_OPTION' });
135 }
136
137 var result = bundle.generate( options );
138
139 var code = result.code,
140 map = result.map;
141
142 if ( options.sourceMap === 'inline' ) {
143 code += '\n//# sourceMappingURL=' + map.toUrl();
144 }
145
146 process.stdout.write( code );
147 });
148}