1 | require( 'source-map-support' ).install();
|
2 |
|
3 | var path = require( 'path' );
|
4 | var handleError = require( './handleError' );
|
5 | var rollup = require( '../' );
|
6 |
|
7 |
|
8 | var log = console.error.bind(console);
|
9 |
|
10 | module.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 | var config = command.config === true ? 'rollup.config.js' : command.config;
|
24 |
|
25 | if ( config ) {
|
26 | config = path.resolve( config );
|
27 |
|
28 | rollup.rollup({
|
29 | entry: config,
|
30 | onwarn: log
|
31 | }).then( function ( bundle ) {
|
32 | var code = bundle.generate({
|
33 | format: 'cjs'
|
34 | }).code;
|
35 |
|
36 |
|
37 | var defaultLoader = require.extensions[ '.js' ];
|
38 | require.extensions[ '.js' ] = function ( m, filename ) {
|
39 | if ( filename === config ) {
|
40 | m._compile( code, filename );
|
41 | } else {
|
42 | defaultLoader( m, filename );
|
43 | }
|
44 | };
|
45 |
|
46 | try {
|
47 | var options = require( path.resolve( config ) );
|
48 | } catch ( err ) {
|
49 | handleError( err );
|
50 | }
|
51 |
|
52 | execute( options, command );
|
53 |
|
54 | require.extensions[ '.js' ] = defaultLoader;
|
55 | })
|
56 | .catch(log);
|
57 | } else {
|
58 | execute( {}, command );
|
59 | }
|
60 | };
|
61 |
|
62 | var equivalents = {
|
63 | input: 'entry',
|
64 | output: 'dest',
|
65 | name: 'moduleName',
|
66 | format: 'format',
|
67 | globals: 'globals',
|
68 | id: 'moduleId',
|
69 | sourcemap: 'sourceMap'
|
70 | };
|
71 |
|
72 | function execute ( options, command ) {
|
73 | var external = command.external ? command.external.split( ',' ) : [];
|
74 |
|
75 | if ( command.globals ) {
|
76 | var globals = Object.create( null );
|
77 |
|
78 | command.globals.split( ',' ).forEach(function ( str ) {
|
79 | var names = str.split( ':' );
|
80 | globals[ names[0] ] = names[1];
|
81 |
|
82 |
|
83 | if ( external.indexOf( names[0] ) === -1 ) {
|
84 | external.push( names[0] );
|
85 | }
|
86 | });
|
87 |
|
88 | command.globals = globals;
|
89 | }
|
90 |
|
91 | options.onwarn = options.onwarn || log;
|
92 |
|
93 | options.external = external;
|
94 | options.indent = command.indent !== false;
|
95 |
|
96 | Object.keys( equivalents ).forEach( function ( cliOption ) {
|
97 | if ( command[ cliOption ] ) {
|
98 | options[ equivalents[ cliOption ] ] = command[ cliOption ];
|
99 | }
|
100 | });
|
101 |
|
102 | try {
|
103 | bundle( options ).catch( handleError );
|
104 | } catch ( err ) {
|
105 | handleError( err );
|
106 | }
|
107 | }
|
108 |
|
109 | function bundle ( options ) {
|
110 | if ( !options.entry ) {
|
111 | handleError({ code: 'MISSING_INPUT_OPTION' });
|
112 | }
|
113 |
|
114 | return rollup.rollup( options ).then( function ( bundle ) {
|
115 | if ( options.dest ) {
|
116 | return bundle.write( options );
|
117 | }
|
118 |
|
119 | if ( options.sourceMap && options.sourceMap !== 'inline' ) {
|
120 | handleError({ code: 'MISSING_OUTPUT_OPTION' });
|
121 | }
|
122 |
|
123 | var result = bundle.generate( options );
|
124 |
|
125 | var code = result.code,
|
126 | map = result.map;
|
127 |
|
128 | if ( options.sourceMap === 'inline' ) {
|
129 | code += '\n//# sourceMappingURL=' + map.toUrl();
|
130 | }
|
131 |
|
132 | process.stdout.write( code );
|
133 | });
|
134 | }
|