Browse Source

refactor and robustify export blocks

contingency-plan
Rich-Harris 10 years ago
parent
commit
7030e54785
  1. 14
      src/finalisers/amd.js
  2. 21
      src/finalisers/cjs.js
  3. 13
      src/finalisers/iife.js
  4. 12
      src/finalisers/shared/getExportBlock.js
  5. 20
      src/finalisers/umd.js

14
src/finalisers/amd.js

@ -1,5 +1,6 @@
import { getName, quoteId } from '../utils/map-helpers';
import getInteropBlock from './shared/getInteropBlock';
import getExportBlock from './shared/getExportBlock';
export default function amd ( bundle, magicString, { exportMode, indentString }, options ) {
let deps = bundle.externalModules.map( quoteId );
@ -20,18 +21,7 @@ export default function amd ( bundle, magicString, { exportMode, indentString },
const interopBlock = getInteropBlock( bundle );
if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
const exports = bundle.entryModule.exports;
let exportBlock;
if ( exportMode === 'default' ) {
exportBlock = `return ${bundle.entryModule.getCanonicalName('default')};`;
} else {
exportBlock = bundle.toExport.map( name => {
return `exports.${name} = ${exports[name].localName};`;
}).join( '\n' );
}
const exportBlock = getExportBlock( bundle, exportMode );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
return magicString

21
src/finalisers/cjs.js

@ -1,3 +1,5 @@
import getExportBlock from './shared/getExportBlock';
export default function cjs ( bundle, magicString, { exportMode }) {
let intro = `'use strict';\n\n`;
@ -21,23 +23,8 @@ export default function cjs ( bundle, magicString, { exportMode }) {
magicString.prepend( intro );
let exportBlock;
if ( exportMode === 'default' && bundle.entryModule.exports.default ) {
exportBlock = `module.exports = ${bundle.entryModule.getCanonicalName('default')};`;
} else if ( exportMode === 'named' ) {
exportBlock = bundle.toExport
.map( key => {
const specifier = bundle.entryModule.exports[ key ];
const name = bundle.entryModule.getCanonicalName( specifier.localName );
return `exports.${key} = ${name};`;
})
.join( '\n' );
}
if ( exportBlock ) {
magicString.append( '\n\n' + exportBlock );
}
const exportBlock = getExportBlock( bundle, exportMode, 'module.exports =' );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
return magicString;
}

13
src/finalisers/iife.js

@ -1,6 +1,7 @@
import { blank } from '../utils/object';
import { getName } from '../utils/map-helpers';
import getInteropBlock from './shared/getInteropBlock';
import getExportBlock from './shared/getExportBlock';
export default function iife ( bundle, magicString, { exportMode, indentString }, options ) {
const globalNames = options.globals || blank();
@ -23,16 +24,16 @@ export default function iife ( bundle, magicString, { exportMode, indentString }
let intro = `(function (${args}) { 'use strict';\n\n`;
let outro = `\n\n})(${dependencies});`;
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock( bundle );
if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
if ( exportMode === 'default' ) {
intro = `var ${options.moduleName} = ${intro}`;
magicString.append( `\n\nreturn ${bundle.entryModule.getCanonicalName('default')};` );
}
// TODO named exports
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock( bundle );
if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
const exportBlock = getExportBlock( bundle, exportMode );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
return magicString
.indent( indentString )

12
src/finalisers/shared/getExportBlock.js

@ -0,0 +1,12 @@
export default function getExportBlock ( bundle, exportMode, mechanism = 'return' ) {
if ( exportMode === 'default' ) {
return `${mechanism} ${bundle.entryModule.getCanonicalName('default')};`;
}
return bundle.toExport
.map( name => {
const prop = name === 'default' ? `['default']` : `.${name}`;
return `exports${prop} = ${bundle.entryModule.getCanonicalName(name)};`;
})
.join( '\n' );
}

20
src/finalisers/umd.js

@ -1,6 +1,7 @@
import { blank } from '../utils/object';
import { getName, quoteId, req } from '../utils/map-helpers';
import getInteropBlock from './shared/getInteropBlock';
import getExportBlock from './shared/getExportBlock';
export default function umd ( bundle, magicString, { exportMode, indentString }, options ) {
if ( exportMode !== 'none' && !options.moduleName ) {
@ -45,23 +46,8 @@ export default function umd ( bundle, magicString, { exportMode, indentString },
const interopBlock = getInteropBlock( bundle );
if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
const exports = bundle.entryModule.exports;
let exportBlock;
if ( exportMode === 'default' ) {
const canonicalName = bundle.entryModule.getCanonicalName( 'default' );
exportBlock = `return ${canonicalName};`;
} else {
exportBlock = bundle.toExport.map( name => {
const canonicalName = bundle.entryModule.getCanonicalName( exports[ name ].localName );
return `exports.${name} = ${canonicalName};`;
}).join( '\n' );
}
if ( exportBlock ) {
magicString.append( '\n\n' + exportBlock );
}
const exportBlock = getExportBlock( bundle, exportMode );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
return magicString
.trim()

Loading…
Cancel
Save