Skip to content

Commit

Permalink
Fixed issue with umd noConflict bundle + named exports. In such case …
Browse files Browse the repository at this point in the history
…undefined got exported as module.
  • Loading branch information
Andarist committed Jul 4, 2017
1 parent a001e6b commit b5dfd1e
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 7 deletions.
33 changes: 28 additions & 5 deletions src/finalisers/umd.js
Expand Up @@ -25,6 +25,15 @@ function setupNamespace ( name ) {
.join( ', ' );
}

function safeAccess ( name ) {
const parts = name.split( '.' );

let acc = 'global';
return parts
.map( part => ( acc += property( part ), acc ) )
.join( ` && ` );
}

const wrapperOutro = '\n\n})));';

export default function umd ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) {
Expand Down Expand Up @@ -67,13 +76,27 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i

const useStrict = options.useStrict !== false ? ` 'use strict';` : ``;

const globalExport = options.noConflict === true ?
`(function() {
var current = ${globalProp(options.moduleName)};
var exports = factory(${globalDeps});
let globalExport;

if (options.noConflict === true) {
let factory;

if ( exportMode === 'default' ) {
factory = `var exports = factory(${globalDeps});`;
} else if ( exportMode === 'named' ) {
const module = globalDeps.shift();
factory = `var exports = ${module};
factory(${['exports'].concat(globalDeps)});`;
}
globalExport = `(function() {
var current = ${safeAccess(options.moduleName)};
${factory}
${globalProp(options.moduleName)} = exports;
exports.noConflict = function() { ${globalProp(options.moduleName)} = current; return exports; };
})()` : `(${defaultExport}factory(${globalDeps}))`;
})()`;
} else {
globalExport = `(${defaultExport}factory(${globalDeps}))`;
}

const wrapperIntro =
`(function (global, factory) {
Expand Down
3 changes: 2 additions & 1 deletion test/form/umd-noconflict-extend/_expected/umd.js
Expand Up @@ -3,7 +3,8 @@
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(function() {
var current = global.FooBar;
var exports = factory((global.FooBar = global.FooBar || {}));
var exports = (global.FooBar = global.FooBar || {});
factory(exports);
global.FooBar = exports;
exports.noConflict = function() { global.FooBar = current; return exports; };
})();
Expand Down
8 changes: 8 additions & 0 deletions test/form/umd-noconflict-namespaced/_config.js
@@ -0,0 +1,8 @@
module.exports = {
description: 'exports noConflict method for default umd when requested',
options: {
noConflict: true,
moduleName: 'my.name.spaced.module'
}
};

17 changes: 17 additions & 0 deletions test/form/umd-noconflict-namespaced/_expected/amd.js
@@ -0,0 +1,17 @@
define(['exports'], function (exports) { 'use strict';

function doThings() {
console.log( 'doing things...' );
}

const number = 42;

var setting = 'no';

exports.doThings = doThings;
exports.number = number;
exports.setting = setting;

Object.defineProperty(exports, '__esModule', { value: true });

});
15 changes: 15 additions & 0 deletions test/form/umd-noconflict-namespaced/_expected/cjs.js
@@ -0,0 +1,15 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function doThings() {
console.log( 'doing things...' );
}

const number = 42;

var setting = 'no';

exports.doThings = doThings;
exports.number = number;
exports.setting = setting;
9 changes: 9 additions & 0 deletions test/form/umd-noconflict-namespaced/_expected/es.js
@@ -0,0 +1,9 @@
function doThings() {
console.log( 'doing things...' );
}

const number = 42;

var setting = 'no';

export { doThings, number, setting };
19 changes: 19 additions & 0 deletions test/form/umd-noconflict-namespaced/_expected/iife.js
@@ -0,0 +1,19 @@
this.my = this.my || {};
this.my.name = this.my.name || {};
this.my.name.spaced = this.my.name.spaced || {};
this.my.name.spaced.module = (function (exports) {
'use strict';

function doThings() {
console.log( 'doing things...' );
}

const number = 42;

var setting = 'no';

exports.doThings = doThings;
exports.number = number;
exports.setting = setting;

}({}));
27 changes: 27 additions & 0 deletions test/form/umd-noconflict-namespaced/_expected/umd.js
@@ -0,0 +1,27 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(function() {
var current = global.my && global.my.name && global.my.name.spaced && global.my.name.spaced.module;
var exports = (global.my = global.my || {}, global.my.name = global.my.name || {}, global.my.name.spaced = global.my.name.spaced || {}, global.my.name.spaced.module = {});
factory(exports);
global.my.name.spaced.module = exports;
exports.noConflict = function() { global.my.name.spaced.module = current; return exports; };
})();
}(this, (function (exports) { 'use strict';

function doThings() {
console.log( 'doing things...' );
}

const number = 42;

var setting = 'no';

exports.doThings = doThings;
exports.number = number;
exports.setting = setting;

Object.defineProperty(exports, '__esModule', { value: true });

})));
7 changes: 7 additions & 0 deletions test/form/umd-noconflict-namespaced/main.js
@@ -0,0 +1,7 @@
export function doThings() {
console.log( 'doing things...' );
}

export const number = 42;

export var setting = 'no';
3 changes: 2 additions & 1 deletion test/form/umd-noconflict/_expected/umd.js
Expand Up @@ -3,7 +3,8 @@
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(function() {
var current = global.FooBar;
var exports = factory((global.FooBar = {}));
var exports = (global.FooBar = {});
factory(exports);
global.FooBar = exports;
exports.noConflict = function() { global.FooBar = current; return exports; };
})();
Expand Down

0 comments on commit b5dfd1e

Please sign in to comment.