Skip to content

Instantly share code, notes, and snippets.

@plexus
Created September 16, 2016 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plexus/e3971543bce8840f2e076bbefeec5016 to your computer and use it in GitHub Desktop.
Save plexus/e3971543bce8840f2e076bbefeec5016 to your computer and use it in GitHub Desktop.
import { getName, quotePath } from '../utils/map-helpers.js';
import getInteropBlock from './shared/getInteropBlock.js';
import getExportBlock from './shared/getExportBlock.js';
export default function closure ( bundle, magicString, { exportMode, indentString, intro }, options ) {
if ( exportMode !== 'none' && !options.moduleName ) {
throw new Error( 'You must supply options.moduleName for Google Closure bundles' );
}
const deps = bundle.externalModules.map( quotePath );
const args = bundle.externalModules.map( getName );
const preamble = `goog.provide('${options.moduleName}');\n` +
deps.map( dep => `goog.require(${dep});\n` ).join('');
const lvalues = args.map(a => a.replace(/\./g, '$'));
args.unshift(options.moduleName);
lvalues.unshift('exports');
const useStrict = options.useStrict !== false ? ` 'use strict';` : ``;
const wrapperStart = `${preamble}\n\n(function (${lvalues.join( ', ' )}) {${useStrict}\n\n`;
const wrapperEnd = `})(${args.join( ', ' )});`;
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock( bundle );
if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
if ( intro ) magicString.prepend( intro );
const exportBlock = getExportBlock( bundle.entryModule, exportMode );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
if ( options.outro ) magicString.append( `\n${options.outro}` );
return magicString
.indent( indentString )
.prepend( wrapperStart )
.append( wrapperEnd );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment