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