From 73a07c5c088156616d6567c64b826ce325d12681 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 2 Jun 2015 14:36:21 -0400 Subject: [PATCH] allow control over indentation (closes #5) --- package.json | 2 +- src/Bundle.js | 44 ++++++++---------------------------- src/finalisers/amd.js | 5 ++-- src/finalisers/cjs.js | 2 +- src/finalisers/es6.js | 2 +- src/finalisers/iife.js | 4 ++-- src/finalisers/umd.js | 6 ++--- src/utils/getExportMode.js | 33 +++++++++++++++++++++++++++ src/utils/getIndentString.js | 7 ++++++ 9 files changed, 59 insertions(+), 46 deletions(-) create mode 100644 src/utils/getExportMode.js create mode 100644 src/utils/getIndentString.js diff --git a/package.json b/package.json index eb2e8a0..eb28e96 100644 --- a/package.json +++ b/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" }, diff --git a/src/Bundle.js b/src/Bundle.js index 54ec830..a89b180 100644 --- a/src/Bundle.js +++ b/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; - } } diff --git a/src/finalisers/amd.js b/src/finalisers/amd.js index 2632e79..dd86c57 100644 --- a/src/finalisers/amd.js +++ b/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 ); } diff --git a/src/finalisers/cjs.js b/src/finalisers/cjs.js index abd23dd..6974ef5 100644 --- a/src/finalisers/cjs.js +++ b/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 diff --git a/src/finalisers/es6.js b/src/finalisers/es6.js index b7a2620..1bf26d3 100644 --- a/src/finalisers/es6.js +++ b/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; diff --git a/src/finalisers/iife.js b/src/finalisers/iife.js index 802dfe3..98a5ff3 100644 --- a/src/finalisers/iife.js +++ b/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 ); } diff --git a/src/finalisers/umd.js b/src/finalisers/umd.js index 61f7d0d..7142520 100644 --- a/src/finalisers/umd.js +++ b/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 ); } diff --git a/src/utils/getExportMode.js b/src/utils/getExportMode.js new file mode 100644 index 0000000..0dbf07d --- /dev/null +++ b/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; +} diff --git a/src/utils/getIndentString.js b/src/utils/getIndentString.js new file mode 100644 index 0000000..8ad7b55 --- /dev/null +++ b/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 || ''; +}