Created
September 16, 2016 17:18
-
-
Save plexus/e3971543bce8840f2e076bbefeec5016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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