You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.7 KiB

10 years ago
require( 'source-map-support' ).install();
var path = require( 'path' );
10 years ago
var handleError = require( './handleError' );
var rollup = require( '../' );
module.exports = function ( command ) {
if ( command._.length > 1 ) {
10 years ago
handleError({ code: 'ONE_AT_A_TIME' });
}
if ( command._.length === 1 ) {
if ( command.input ) {
10 years ago
handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' });
}
command.input = command._[0];
10 years ago
}
var config = command.config === true ? 'rollup.config.js' : command.config;
if ( config ) {
config = path.resolve( config );
rollup.rollup({
entry: config,
onwarn: function () {}
}).then( function ( bundle ) {
var code = bundle.generate({
format: 'cjs'
}).code;
// temporarily override require
var defaultLoader = require.extensions[ '.js' ];
require.extensions[ '.js' ] = function ( m, filename ) {
if ( filename === config ) {
m._compile( code, filename );
} else {
defaultLoader( m, filename );
}
};
try {
var options = require( path.resolve( config ) );
} catch ( err ) {
handleError( err );
}
execute( options, command );
require.extensions[ '.js' ] = defaultLoader;
});
} else {
execute( {}, command );
}
};
var equivalents = {
input: 'entry',
output: 'dest',
name: 'moduleName',
format: 'format',
globals: 'globals',
id: 'moduleId',
sourcemap: 'sourceMap'
};
function execute ( options, command ) {
var external = command.external ? command.external.split( ',' ) : [];
if ( command.globals ) {
var globals = Object.create( null );
command.globals.split( ',' ).forEach(function ( str ) {
var names = str.split( ':' );
globals[ names[0] ] = names[1];
// Add missing Module IDs to external.
if ( external.indexOf( names[0] ) === -1 ) {
external.push( names[0] );
}
});
command.globals = globals;
}
options.external = external;
options.indent = command.indent !== false;
Object.keys( equivalents ).forEach( function ( cliOption ) {
if ( command[ cliOption ] ) {
options[ equivalents[ cliOption ] ] = command[ cliOption ];
}
});
10 years ago
try {
bundle( options ).catch( handleError );
} catch ( err ) {
handleError( err );
}
}
10 years ago
function bundle ( options ) {
if ( !options.entry ) {
10 years ago
handleError({ code: 'MISSING_INPUT_OPTION' });
}
return rollup.rollup( options ).then( function ( bundle ) {
if ( options.dest ) {
return bundle.write( options );
10 years ago
}
if ( options.sourceMap && options.sourceMap !== 'inline' ) {
10 years ago
handleError({ code: 'MISSING_OUTPUT_OPTION' });
}
var result = bundle.generate( options );
10 years ago
var code = result.code,
map = result.map;
10 years ago
if ( options.sourceMap === 'inline' ) {
10 years ago
code += '\n//# sourceMappingURL=' + map.toUrl();
}
process.stdout.write( code );
});
}