Browse Source

basic finalisers for all module types

contingency-plan
Rich-Harris 10 years ago
parent
commit
ed4e27d0a7
  1. 15
      README.md
  2. 2
      src/Bundle.js
  3. 38
      src/finalisers/amd.js
  4. 1
      src/finalisers/cjs.js
  5. 6
      src/finalisers/es6.js
  6. 34
      src/finalisers/umd.js
  7. 8
      src/utils/map-helpers.js

15
README.md

@ -102,7 +102,20 @@ rollup.rollup( 'app.js', {
}).then( function ( bundle ) { }).then( function ( bundle ) {
// generate code and a sourcemap // generate code and a sourcemap
const { code, map } = bundle.generate({ const { code, map } = bundle.generate({
format: 'amd' // output format - 'amd', 'cjs', 'es6', 'umd'
format: 'amd',
// exports - 'auto', 'none', 'default', 'named'
exports: 'auto',
// amd/umd options
moduleId: 'my-library',
// umd options
moduleName: 'MyLibrary', // necessary if the bundle has exports
globals: {
backbone: 'Backbone'
}
}); });
fs.writeFileSync( 'bundle.js', code + '\n//# sourceMappingURL=bundle.js.map' ); fs.writeFileSync( 'bundle.js', code + '\n//# sourceMappingURL=bundle.js.map' );

2
src/Bundle.js

@ -197,7 +197,7 @@ export default class Bundle {
throw new Error( `You must specify an output type - valid options are ${keys( finalisers ).join( ', ' )}` ); throw new Error( `You must specify an output type - valid options are ${keys( finalisers ).join( ', ' )}` );
} }
magicString = finalise( this, magicString, exportMode, options ); magicString = finalise( this, magicString.trim(), exportMode, options );
return { return {
code: magicString.toString(), code: magicString.toString(),

38
src/finalisers/amd.js

@ -1,3 +1,39 @@
import { has } from '../utils/object';
import { getName, quoteId } from '../utils/map-helpers';
export default function amd ( bundle, magicString, exportMode, options ) { export default function amd ( bundle, magicString, exportMode, options ) {
throw new Error( 'TODO' ); const indentStr = magicString.getIndentString();
let deps = bundle.externalModules.map( quoteId );
let args = bundle.externalModules.map( getName );
if ( exportMode === 'named' ) {
args.unshift( `exports` );
deps.unshift( `'exports'` );
}
const params =
( has( options, 'moduleId' ) ? `['${options.moduleId}'], ` : `` ) +
( deps.length ? `[${deps.join( ', ' )}], ` : `` );
const intro = `define(${params}function (${args.join( ', ' )}) { 'use strict';\n\n`;
const exports = bundle.entryModule.exports;
let exportBlock;
if ( exportMode === 'default' ) {
exportBlock = `return ${bundle.entryModule.getCanonicalName('default')};`;
} else {
exportBlock = '\n\n' + Object.keys( exports ).map( name => {
return `exports.${name} = ${exports[name].localName};`;
}).join( '\n' );
}
return magicString
.append( exportBlock )
.trim()
.indent()
.append( '\n\n});' )
.prepend( intro );
} }

1
src/finalisers/cjs.js

@ -3,7 +3,6 @@ import { keys } from '../utils/object';
export default function cjs ( bundle, magicString, exportMode ) { export default function cjs ( bundle, magicString, exportMode ) {
let intro = `'use strict';\n\n`; let intro = `'use strict';\n\n`;
// TODO handle ambiguous default imports
// TODO handle empty imports, once they're supported // TODO handle empty imports, once they're supported
const importBlock = bundle.externalModules const importBlock = bundle.externalModules
.map( module => { .map( module => {

6
src/finalisers/es6.js

@ -1,3 +1,7 @@
export default function es6 ( bundle, magicString, exportMode, options ) { export default function es6 ( bundle, magicString, exportMode, options ) {
throw new Error( 'TODO' ); // TODO
const introBlock = '';
const exportBlock = '';
return magicString.trim();
} }

34
src/finalisers/umd.js

@ -1,12 +1,37 @@
import { has } from '../utils/object';
import { getName, quoteId, req } from '../utils/map-helpers';
export default function umd ( bundle, magicString, exportMode, options ) { export default function umd ( bundle, magicString, exportMode, options ) {
const indentStr = magicString.getIndentString(); const indentStr = magicString.getIndentString();
const globalNames = options.globals || {};
let amdDeps = bundle.externalModules.map( quoteId );
let cjsDeps = bundle.externalModules.map( req );
let globalDeps = bundle.externalModules.map( module => {
return has( globalNames, module.id ) ? globalNames[ module.id ] : module.name;
});
let args = bundle.externalModules.map( getName );
if ( exportMode === 'named' ) {
amdDeps.unshift( `'exports'` );
cjsDeps.unshift( `'exports'` );
globalDeps.unshift( `(global.${moduleName} = {})` );
args.unshift( 'exports' );
}
const amdParams =
( has( options, 'moduleId' ) ? `['${options.moduleId}'], ` : `` ) +
( amdDeps.length ? `[${amdDeps.join( ', ' )}], ` : `` );
const intro = const intro =
`(function (global, factory) { `(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof exports === 'object' && typeof module !== 'undefined' ? factory(${cjsDeps.join( ', ' )}) :
typeof define === 'function' && define.amd ? define(factory) : typeof define === 'function' && define.amd ? define(${amdParams}factory) :
factory((global.${options.globalName} = {})); factory(${globalDeps});
}(this, function (exports) { 'use strict'; }(this, function (${args}) { 'use strict';
`.replace( /^\t\t/gm, '' ).replace( /^\t/gm, indentStr ); `.replace( /^\t\t/gm, '' ).replace( /^\t/gm, indentStr );
@ -18,6 +43,7 @@ export default function umd ( bundle, magicString, exportMode, options ) {
return magicString return magicString
.append( exportBlock ) .append( exportBlock )
.trim()
.indent() .indent()
.append( '\n\n}));' ) .append( '\n\n}));' )
.prepend( intro ); .prepend( intro );

8
src/utils/map-helpers.js

@ -1,3 +1,11 @@
export function getName ( x ) { export function getName ( x ) {
return x.name; return x.name;
} }
export function quoteId ( x ) {
return `'${x.id}'`
}
export function req ( x ) {
return `require('${x.id}')`
}

Loading…
Cancel
Save