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.

157 lines
3.5 KiB

10 years ago
require( 'source-map-support' ).install();
var path = require( 'path' );
10 years ago
var handleError = require( './handleError' );
var rollup = require( '../' );
// log to stderr to keep `rollup main.js > bundle.js` from breaking
var log = console.error.bind(console);
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
}
if ( command.environment ) {
command.environment.split( ',' ).forEach( function ( pair ) {
var index = pair.indexOf( ':' );
if ( ~index ) {
process.env[ pair.slice( 0, index ) ] = pair.slice( index + 1 );
} else {
process.env[ pair ] = true;
}
});
}
var config = command.config === true ? 'rollup.config.js' : command.config;
if ( config ) {
config = path.resolve( config );
rollup.rollup({
entry: config,
onwarn: function ( message ) {
if ( /Treating .+ as external dependency/.test( message ) ) return;
log( message );
}
}).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 ) );
if ( Object.keys( options ).length === 0 ) {
handleError({ code: 'MISSING_CONFIG' });
}
} catch ( err ) {
handleError( err );
}
execute( options, command );
require.extensions[ '.js' ] = defaultLoader;
})
.catch(log);
} else {
execute( {}, command );
}
};
var equivalents = {
banner: 'banner',
footer: 'footer',
format: 'format',
globals: 'globals',
id: 'moduleId',
input: 'entry',
intro: 'intro',
name: 'moduleName',
output: 'dest',
outro: 'outro',
sourcemap: 'sourceMap'
};
function execute ( options, command ) {
var external = ( options.external || [] )
.concat( 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.onwarn = options.onwarn || log;
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 );
});
}