Skip to content

Commit

Permalink
Added support for --extend option. Default behaviour for the IIFE sho…
Browse files Browse the repository at this point in the history
…uld be to create a simple local var or to assign a module to a namespaced name
  • Loading branch information
Andarist committed Jul 3, 2017
1 parent 269949f commit 97baa41
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/finalisers/iife.js
Expand Up @@ -26,17 +26,17 @@ const thisProp = name => `this${keypath( name )}`;
export default function iife ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) {
const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle, 'null' );

const name = options.moduleName;
const { extend, moduleName: name } = options;
const isNamespaced = name && ~name.indexOf( '.' );
const justVariable = !extend && !isNamespaced;

if ( name && !isLegal(name) ) {
if ( name && justVariable && !isLegal(name) ) {
error({
code: 'ILLEGAL_IDENTIFIER_AS_NAME',
message: `Given moduleName - ${ name } - is not legal JS identifier.`
})
message: `Given moduleName (${name}) is not legal JS identifier. If you need this you can try --extend option`
});
}

const isNamespaced = name && ~name.indexOf( '.' );

warnOnBuiltins( bundle );

const external = trimEmptyImports( bundle.externalModules );
Expand All @@ -50,7 +50,7 @@ export default function iife ( bundle, magicString, { exportMode, indentString,
});
}

if ( isNamespaced ) {
if ( extend ) {
dependencies.unshift( `(${thisProp(name)} = ${thisProp(name)} || {})` );
args.unshift( 'exports' );
} else if ( exportMode === 'named' ) {
Expand All @@ -63,7 +63,7 @@ export default function iife ( bundle, magicString, { exportMode, indentString,
let wrapperIntro = `(function (${args}) {\n${useStrict}`;
const wrapperOutro = `\n\n}(${dependencies}));`;

if ( exportMode !== 'none' ) {
if ( exportMode !== 'none' && !extend) {
wrapperIntro = ( isNamespaced ? thisProp(name) : `${bundle.varOrConst} ${name}` ) + ` = ${wrapperIntro}`;
}

Expand Down
1 change: 1 addition & 0 deletions src/rollup.js
Expand Up @@ -19,6 +19,7 @@ const ALLOWED_KEYS = [
'dest',
'entry',
'exports',
'extend',
'external',
'footer',
'format',
Expand Down
7 changes: 7 additions & 0 deletions test/form/extend-exports/_config.js
@@ -0,0 +1,7 @@
module.exports = {
description: 'extends module correctly',
options: {
extend: true,
moduleName: 'foo'
}
};
9 changes: 9 additions & 0 deletions test/form/extend-exports/_expected/amd.js
@@ -0,0 +1,9 @@
define(['exports'], function (exports) { 'use strict';

const answer = 42;

exports.answer = answer;

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

});
7 changes: 7 additions & 0 deletions test/form/extend-exports/_expected/cjs.js
@@ -0,0 +1,7 @@
'use strict';

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

const answer = 42;

exports.answer = answer;
3 changes: 3 additions & 0 deletions test/form/extend-exports/_expected/es.js
@@ -0,0 +1,3 @@
const answer = 42;

export { answer };
8 changes: 8 additions & 0 deletions test/form/extend-exports/_expected/iife.js
@@ -0,0 +1,8 @@
(function (exports) {
'use strict';

const answer = 42;

exports.answer = answer;

}((this.foo = this.foo || {})));
13 changes: 13 additions & 0 deletions test/form/extend-exports/_expected/umd.js
@@ -0,0 +1,13 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.foo = global.foo || {})));
}(this, (function (exports) { 'use strict';

const answer = 42;

exports.answer = answer;

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

})));
1 change: 1 addition & 0 deletions test/form/extend-exports/main.js
@@ -0,0 +1 @@
export const answer = 42;
7 changes: 7 additions & 0 deletions test/form/extend-namespaced-exports/_config.js
@@ -0,0 +1,7 @@
module.exports = {
description: 'extends namespaced module name',
options: {
extend: true,
moduleName: 'foo.bar.baz'
}
};
9 changes: 9 additions & 0 deletions test/form/extend-namespaced-exports/_expected/amd.js
@@ -0,0 +1,9 @@
define(['exports'], function (exports) { 'use strict';

const answer = 42;

exports.answer = answer;

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

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

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

const answer = 42;

exports.answer = answer;
3 changes: 3 additions & 0 deletions test/form/extend-namespaced-exports/_expected/es.js
@@ -0,0 +1,3 @@
const answer = 42;

export { answer };
10 changes: 10 additions & 0 deletions test/form/extend-namespaced-exports/_expected/iife.js
@@ -0,0 +1,10 @@
this.foo = this.foo || {};
this.foo.bar = this.foo.bar || {};
(function (exports) {
'use strict';

const answer = 42;

exports.answer = answer;

}((this.foo.bar.baz = this.foo.bar.baz || {})));
13 changes: 13 additions & 0 deletions test/form/extend-namespaced-exports/_expected/umd.js
@@ -0,0 +1,13 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.foo = global.foo || {}, global.foo.bar = global.foo.bar || {}, global.foo.bar.baz = global.foo.bar.baz || {})));
}(this, (function (exports) { 'use strict';

const answer = 42;

exports.answer = answer;

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

})));
1 change: 1 addition & 0 deletions test/form/extend-namespaced-exports/main.js
@@ -0,0 +1 @@
export const answer = 42;
1 change: 1 addition & 0 deletions test/form/module-name-scoped-package/_config.js
@@ -1,6 +1,7 @@
module.exports = {
description: 'allows module name with dashes to be added to the global object',
options: {
extend: true,
moduleName: '@scoped/npm-package'
}
};
4 changes: 2 additions & 2 deletions test/form/module-name-wat/_expected/iife.js
@@ -1,11 +1,11 @@
this.foo = this.foo || {};
this.foo['@scoped/npm-package'] = this.foo['@scoped/npm-package'] || {};
this.foo['@scoped/npm-package'].bar = this.foo['@scoped/npm-package'].bar || {};
(function (exports) {
this.foo['@scoped/npm-package'].bar['why-would-you-do-this'] = (function (exports) {
'use strict';

let foo = 'foo';

exports.foo = foo;

}((this.foo['@scoped/npm-package'].bar['why-would-you-do-this'] = this.foo['@scoped/npm-package'].bar['why-would-you-do-this'] || {})));
}({}));
1 change: 1 addition & 0 deletions test/form/module-name-with-dashes/_config.js
@@ -1,6 +1,7 @@
module.exports = {
description: 'allows module name with dashes to be added to the global object',
options: {
extend: true,
moduleName: 'module-name-with-dashes'
}
};
4 changes: 2 additions & 2 deletions test/form/namespaced-named-exports/_expected/iife.js
@@ -1,10 +1,10 @@
this.foo = this.foo || {};
this.foo.bar = this.foo.bar || {};
(function (exports) {
this.foo.bar.baz = (function (exports) {
'use strict';

var answer = 42;

exports.answer = answer;

}((this.foo.bar.baz = this.foo.bar.baz || {})));
}({}));
2 changes: 1 addition & 1 deletion test/test.js
Expand Up @@ -136,7 +136,7 @@ describe( 'rollup', function () {
return rollup.rollup({ entry: 'x', plUgins: [] }).then( () => {
throw new Error( 'Missing expected error' );
}, err => {
assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, amd, banner, cache, context, dest, entry, exports, external, footer, format, globals, indent, interop, intro, legacy, moduleContext, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, pureExternalModules, sourceMap, sourceMapFile, targets, treeshake, useStrict, watch' );
assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, amd, banner, cache, context, dest, entry, exports, extend, external, footer, format, globals, indent, interop, intro, legacy, moduleContext, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, pureExternalModules, sourceMap, sourceMapFile, targets, treeshake, useStrict, watch' );
});
});

Expand Down

0 comments on commit 97baa41

Please sign in to comment.