From 1ef38cf131c0a7f374d0999cb743df47035c8418 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 30 Dec 2015 18:11:08 -0500 Subject: [PATCH] allow options.globals to be a function, warn on miss (#293) --- src/finalisers/iife.js | 8 ++++---- src/finalisers/shared/getGlobalNameMaker.js | 11 +++++++++++ src/finalisers/umd.js | 7 +++---- .../external-imports-custom-names-function/_config.js | 8 ++++++++ .../_expected/amd.js | 5 +++++ .../_expected/cjs.js | 5 +++++ .../_expected/es6.js | 3 +++ .../_expected/iife.js | 5 +++++ .../_expected/umd.js | 9 +++++++++ .../external-imports-custom-names-function/main.js | 2 ++ 10 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 src/finalisers/shared/getGlobalNameMaker.js create mode 100644 test/form/external-imports-custom-names-function/_config.js create mode 100644 test/form/external-imports-custom-names-function/_expected/amd.js create mode 100644 test/form/external-imports-custom-names-function/_expected/cjs.js create mode 100644 test/form/external-imports-custom-names-function/_expected/es6.js create mode 100644 test/form/external-imports-custom-names-function/_expected/iife.js create mode 100644 test/form/external-imports-custom-names-function/_expected/umd.js create mode 100644 test/form/external-imports-custom-names-function/main.js diff --git a/src/finalisers/iife.js b/src/finalisers/iife.js index 7c1c5d0..6e11ef5 100644 --- a/src/finalisers/iife.js +++ b/src/finalisers/iife.js @@ -2,6 +2,7 @@ import { blank } from '../utils/object.js'; import { getName } from '../utils/map-helpers.js'; import getInteropBlock from './shared/getInteropBlock.js'; import getExportBlock from './shared/getExportBlock.js'; +import getGlobalNameMaker from './shared/getGlobalNameMaker.js'; function setupNamespace ( keypath ) { let parts = keypath.split( '.' ); // TODO support e.g. `foo['something-hyphenated']`? @@ -16,13 +17,12 @@ function setupNamespace ( keypath ) { } export default function iife ( bundle, magicString, { exportMode, indentString }, options ) { - const globalNames = options.globals || blank(); + const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle.onwarn ); + const name = options.moduleName; const isNamespaced = name && ~name.indexOf( '.' ); - let dependencies = bundle.externalModules.map( module => { - return globalNames[ module.id ] || module.name; - }); + let dependencies = bundle.externalModules.map( globalNameMaker ); let args = bundle.externalModules.map( getName ); diff --git a/src/finalisers/shared/getGlobalNameMaker.js b/src/finalisers/shared/getGlobalNameMaker.js new file mode 100644 index 0000000..676b416 --- /dev/null +++ b/src/finalisers/shared/getGlobalNameMaker.js @@ -0,0 +1,11 @@ +export default function getGlobalNameMaker ( globals, onwarn ) { + const fn = typeof globals === 'function' ? globals : id => globals[ id ]; + + return function ( module ) { + const name = fn( module.id ); + if ( name ) return name; + + onwarn( `No name was provided for external module '${module.id}' in options.globals – guessing '${module.name}'` ); + return module.name; + }; +} diff --git a/src/finalisers/umd.js b/src/finalisers/umd.js index bdd999e..3e8e813 100644 --- a/src/finalisers/umd.js +++ b/src/finalisers/umd.js @@ -2,6 +2,7 @@ import { blank } from '../utils/object.js'; import { getName, quoteId, req } from '../utils/map-helpers.js'; import getInteropBlock from './shared/getInteropBlock.js'; import getExportBlock from './shared/getExportBlock.js'; +import getGlobalNameMaker from './shared/getGlobalNameMaker.js'; function setupNamespace ( name ) { const parts = name.split( '.' ); @@ -19,13 +20,11 @@ export default function umd ( bundle, magicString, { exportMode, indentString }, throw new Error( 'You must supply options.moduleName for UMD bundles' ); } - const globalNames = options.globals || blank(); + const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle.onwarn ); let amdDeps = bundle.externalModules.map( quoteId ); let cjsDeps = bundle.externalModules.map( req ); - let globalDeps = bundle.externalModules.map( module => { - return 'global.' + (globalNames[ module.id ] || module.name); - }); + let globalDeps = bundle.externalModules.map( module => `global.${globalNameMaker( module )}` ); let args = bundle.externalModules.map( getName ); diff --git a/test/form/external-imports-custom-names-function/_config.js b/test/form/external-imports-custom-names-function/_config.js new file mode 100644 index 0000000..cbc42c4 --- /dev/null +++ b/test/form/external-imports-custom-names-function/_config.js @@ -0,0 +1,8 @@ +module.exports = { + description: 'allows globals to be specified as a function', + options: { + globals: function ( id ) { + return id.replace( /-/g, '_' ); + } + } +}; diff --git a/test/form/external-imports-custom-names-function/_expected/amd.js b/test/form/external-imports-custom-names-function/_expected/amd.js new file mode 100644 index 0000000..ac48d1f --- /dev/null +++ b/test/form/external-imports-custom-names-function/_expected/amd.js @@ -0,0 +1,5 @@ +define(['a-b-c'], function (aBC) { 'use strict'; + + aBC.foo(); + +}); \ No newline at end of file diff --git a/test/form/external-imports-custom-names-function/_expected/cjs.js b/test/form/external-imports-custom-names-function/_expected/cjs.js new file mode 100644 index 0000000..58efa9a --- /dev/null +++ b/test/form/external-imports-custom-names-function/_expected/cjs.js @@ -0,0 +1,5 @@ +'use strict'; + +var aBC = require('a-b-c'); + +aBC.foo(); \ No newline at end of file diff --git a/test/form/external-imports-custom-names-function/_expected/es6.js b/test/form/external-imports-custom-names-function/_expected/es6.js new file mode 100644 index 0000000..cc482cc --- /dev/null +++ b/test/form/external-imports-custom-names-function/_expected/es6.js @@ -0,0 +1,3 @@ +import { foo } from 'a-b-c'; + +foo(); \ No newline at end of file diff --git a/test/form/external-imports-custom-names-function/_expected/iife.js b/test/form/external-imports-custom-names-function/_expected/iife.js new file mode 100644 index 0000000..d8d719b --- /dev/null +++ b/test/form/external-imports-custom-names-function/_expected/iife.js @@ -0,0 +1,5 @@ +(function (aBC) { 'use strict'; + + aBC.foo(); + +})(a_b_c); \ No newline at end of file diff --git a/test/form/external-imports-custom-names-function/_expected/umd.js b/test/form/external-imports-custom-names-function/_expected/umd.js new file mode 100644 index 0000000..1018b8a --- /dev/null +++ b/test/form/external-imports-custom-names-function/_expected/umd.js @@ -0,0 +1,9 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('a-b-c')) : + typeof define === 'function' && define.amd ? define(['a-b-c'], factory) : + (factory(global.a_b_c)); +}(this, function (aBC) { 'use strict'; + + aBC.foo(); + +})); \ No newline at end of file diff --git a/test/form/external-imports-custom-names-function/main.js b/test/form/external-imports-custom-names-function/main.js new file mode 100644 index 0000000..3746e06 --- /dev/null +++ b/test/form/external-imports-custom-names-function/main.js @@ -0,0 +1,2 @@ +import { foo } from 'a-b-c'; +foo();