Browse Source

allow control over indentation (closes #5)

contingency-plan
Rich-Harris 10 years ago
parent
commit
73a07c5c08
  1. 2
      package.json
  2. 44
      src/Bundle.js
  3. 5
      src/finalisers/amd.js
  4. 2
      src/finalisers/cjs.js
  5. 2
      src/finalisers/es6.js
  6. 4
      src/finalisers/iife.js
  7. 6
      src/finalisers/umd.js
  8. 33
      src/utils/getExportMode.js
  9. 7
      src/utils/getIndentString.js

2
package.json

@ -45,7 +45,7 @@
"dependencies": {
"acorn": "^1.1.0",
"chalk": "^1.0.0",
"magic-string": "^0.5.3",
"magic-string": "^0.6.2",
"minimist": "^1.1.1",
"sander": "^0.3.3"
},

44
src/Bundle.js

@ -9,10 +9,8 @@ import makeLegalIdentifier from './utils/makeLegalIdentifier';
import ensureArray from './utils/ensureArray';
import { defaultResolver, defaultExternalResolver } from './utils/resolvePath';
import { defaultLoader } from './utils/load';
function badExports ( option, keys ) {
throw new Error( `'${option}' was specified for options.exports, but entry module has following exports: ${keys.join(', ')}` );
}
import getExportMode from './utils/getExportMode';
import getIndentString from './utils/getIndentString';
export default class Bundle {
constructor ( options ) {
@ -191,8 +189,6 @@ export default class Bundle {
generate ( options = {} ) {
let magicString = new MagicString.Bundle({ separator: '' });
// Determine export mode - 'default', 'named', 'none'
let exportMode = this.getExportMode( options.exports );
const format = options.format || 'es6';
let previousMargin = 0;
@ -340,7 +336,13 @@ export default class Bundle {
throw new Error( `You must specify an output type - valid options are ${keys( finalisers ).join( ', ' )}` );
}
magicString = finalise( this, magicString.trim(), exportMode, options );
magicString = finalise( this, magicString.trim(), {
// Determine export mode - 'default', 'named', 'none'
exportMode: getExportMode( this, options.exports ),
// Determine indentation
indentString: getIndentString( magicString, options )
}, options );
const code = magicString.toString();
let map = null;
@ -361,32 +363,4 @@ export default class Bundle {
return { code, map };
}
getExportMode ( exportMode ) {
const exportKeys = keys( this.entryModule.exports );
if ( exportMode === 'default' ) {
if ( exportKeys.length !== 1 || exportKeys[0] !== 'default' ) {
badExports( 'default', exportKeys );
}
} else if ( exportMode === 'none' && exportKeys.length ) {
badExports( 'none', exportKeys );
}
if ( !exportMode || exportMode === 'auto' ) {
if ( exportKeys.length === 0 ) {
exportMode = 'none';
} else if ( exportKeys.length === 1 && exportKeys[0] === 'default' ) {
exportMode = 'default';
} else {
exportMode = 'named';
}
}
if ( !/(?:default|named|none)/.test( exportMode ) ) {
throw new Error( `options.exports must be 'default', 'named', 'none', 'auto', or left unspecified (defaults to 'auto')` );
}
return exportMode;
}
}

5
src/finalisers/amd.js

@ -1,6 +1,6 @@
import { getName, quoteId } from '../utils/map-helpers';
export default function amd ( bundle, magicString, exportMode, options ) {
export default function amd ( bundle, magicString, { exportMode, indentString }, options ) {
let deps = bundle.externalModules.map( quoteId );
let args = bundle.externalModules.map( getName );
@ -32,8 +32,7 @@ export default function amd ( bundle, magicString, exportMode, options ) {
}
return magicString
.trim()
.indent()
.indent( indentString )
.append( '\n\n});' )
.prepend( intro );
}

2
src/finalisers/cjs.js

@ -1,6 +1,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`;
// TODO handle empty imports, once they're supported

2
src/finalisers/es6.js

@ -1,6 +1,6 @@
import { keys } from '../utils/object';
export default function es6 ( bundle, magicString, exportMode, options ) {
export default function es6 ( bundle, magicString, { exportMode }, options ) {
const introBlock = ''; // TODO...
const exports = bundle.entryModule.exports;

4
src/finalisers/iife.js

@ -1,7 +1,7 @@
import { blank } from '../utils/object';
import { getName } from '../utils/map-helpers';
export default function iife ( bundle, magicString, exportMode, options ) {
export default function iife ( bundle, magicString, { exportMode, indentString }, options ) {
const globalNames = options.globals || blank();
let dependencies = bundle.externalModules.map( module => {
@ -30,7 +30,7 @@ export default function iife ( bundle, magicString, exportMode, options ) {
// TODO named exports
return magicString
.indent()
.indent( indentString )
.prepend( intro )
.append( outro );
}

6
src/finalisers/umd.js

@ -1,7 +1,7 @@
import { blank } 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, indentString }, options ) {
if ( exportMode !== 'none' && !options.moduleName ) {
throw new Error( 'You must supply options.moduleName for UMD bundles' );
}
@ -40,7 +40,7 @@ export default function umd ( bundle, magicString, exportMode, options ) {
${defaultExport}factory(${globalDeps});
}(this, function (${args}) { 'use strict';
`.replace( /^\t\t/gm, '' ).replace( /^\t/gm, indentStr );
`.replace( /^\t\t/gm, '' ).replace( /^\t/gm, magicString.getIndentString() );
const exports = bundle.entryModule.exports;
@ -62,7 +62,7 @@ export default function umd ( bundle, magicString, exportMode, options ) {
return magicString
.trim()
.indent()
.indent( indentString )
.append( '\n\n}));' )
.prepend( intro );
}

33
src/utils/getExportMode.js

@ -0,0 +1,33 @@
import { keys } from './object';
function badExports ( option, keys ) {
throw new Error( `'${option}' was specified for options.exports, but entry module has following exports: ${keys.join(', ')}` );
}
export default function getExportMode ( bundle, exportMode ) {
const exportKeys = keys( bundle.entryModule.exports );
if ( exportMode === 'default' ) {
if ( exportKeys.length !== 1 || exportKeys[0] !== 'default' ) {
badExports( 'default', exportKeys );
}
} else if ( exportMode === 'none' && exportKeys.length ) {
badExports( 'none', exportKeys );
}
if ( !exportMode || exportMode === 'auto' ) {
if ( exportKeys.length === 0 ) {
exportMode = 'none';
} else if ( exportKeys.length === 1 && exportKeys[0] === 'default' ) {
exportMode = 'default';
} else {
exportMode = 'named';
}
}
if ( !/(?:default|named|none)/.test( exportMode ) ) {
throw new Error( `options.exports must be 'default', 'named', 'none', 'auto', or left unspecified (defaults to 'auto')` );
}
return exportMode;
}

7
src/utils/getIndentString.js

@ -0,0 +1,7 @@
export default function getIndentString ( magicString, options ) {
if ( !( 'indent' in options ) || options.indent === true ) {
return magicString.getIndentString();
}
return options.indent || '';
}
Loading…
Cancel
Save