Skip to content

Commit 1c4cc50

Browse files
committed
Merge pull request #90 from matthiaspalmer/dev
Dev
2 parents 0e6504b + 13473d5 commit 1c4cc50

File tree

9 files changed

+64
-31
lines changed

9 files changed

+64
-31
lines changed

build/amdclean.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! amdclean - v2.6.0 - 2015-04-02
1+
/*! amdclean - v2.6.1 - 2015-04-10
22
* http://gregfranko.com/amdclean
33
* Copyright (c) 2015 Greg Franko */
44

@@ -26,7 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
2727
*/
2828

29-
;(function(esprima, estraverse, escodegen, _) {
29+
;var sourcemapToAst;(function(esprima, estraverse, escodegen, _) {
3030
// defaultOptions.js
3131
// =================
3232
// AMDclean default options
@@ -1038,7 +1038,10 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
10381038
if (dependencyBlacklist[currentDependency.value] && !shouldOptimize) {
10391039
depNames.push(currentDependency.value);
10401040
} else if (dependencyBlacklist[currentDependency.value] !== 'remove') {
1041-
if (dependencyBlacklist[currentDependency.value]) {
1041+
if (currentDependency.value === 'exports') {
1042+
depNames.push(moduleName);
1043+
amdclean.exportsModules[moduleName] = true;
1044+
} else if (dependencyBlacklist[currentDependency.value]) {
10421045
depNames.push('{}');
10431046
} else {
10441047
depNames.push(currentDependency.value);
@@ -1430,6 +1433,10 @@ clean = function clean() {
14301433
// Creates variable declarations for each AMD module/callback parameter that needs to be hoisted
14311434
_.each(hoistedVariables, function (moduleValue, moduleName) {
14321435
if (!_.contains(options.ignoreModules, moduleName)) {
1436+
var _initValue = amdclean.exportsModules[moduleName] !== true ? null : {
1437+
type: 'ObjectExpression',
1438+
properties: []
1439+
};
14331440
declarations.push({
14341441
'type': 'VariableDeclarator',
14351442
'id': {
@@ -1438,7 +1445,7 @@ clean = function clean() {
14381445
'range': defaultRange,
14391446
'loc': defaultLOC
14401447
},
1441-
'init': null,
1448+
'init': _initValue,
14421449
'range': defaultRange,
14431450
'loc': defaultLOC
14441451
});
@@ -1521,7 +1528,7 @@ clean = function clean() {
15211528
};
15221529
(function () {
15231530
(function (root, factory, undefined) {
1524-
1531+
'use strict';
15251532
// Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, and plain browser loading
15261533
if (typeof define === 'function' && define.amd) {
15271534
factory.amd = true;
@@ -1547,7 +1554,7 @@ clean = function clean() {
15471554
root.amdclean = factory(null, root);
15481555
}
15491556
}(this, function cleanamd(amdDependencies, context) {
1550-
1557+
'use strict';
15511558
// Third-Party Dependencies
15521559
// Note: These dependencies are hoisted to the top (as local variables) at build time (Look in the gulpfile.js file and the AMDclean wrap option for more details)
15531560
sourcemapToAst = function () {
@@ -1642,6 +1649,10 @@ clean = function clean() {
16421649
// --------
16431650
// All of the stored program comments
16441651
this.comments = [];
1652+
// exportsModules
1653+
// --------
1654+
// An object that stores a map of all modules that makes use of the exports parameter in define. Useful when declaring variables and making sure circular dependencies work correctly.
1655+
this.exportsModules = {};
16451656
// options
16461657
// -------
16471658
// Merged user options and default options

build/amdclean.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ gulp.task('build', function(cb) {
6767
'wrap': {
6868
// All of the third party dependencies are hoisted here
6969
// It's a hack, but it's not too painful
70-
'start': ';(function(esprima, estraverse, escodegen, _) {\n',
71-
'end': '}(typeof esprima !== "undefined" ? esprima: null, typeof estraverse !== "undefined" ? estraverse: null, typeof escodegen !== "undefined" ? escodegen: null, typeof _ !== "undefined" ? _ : null));'
70+
'start': ';(function(esprima, estraverse, escodegen, _, sourcemapToAst) {\n',
71+
'end': '}(typeof esprima !== "undefined" ? esprima: null, typeof estraverse !== "undefined" ? estraverse: null, typeof escodegen !== "undefined" ? escodegen: null, typeof _ !== "undefined" ? _ : null, typeof sourcemapToAst !== "undefined" ? sourcemapToAst : null));'
7272
},
7373
'createAnonymousAMDModule': true
7474
});
@@ -136,4 +136,4 @@ gulp.task('watch', function() {
136136
watcher.on('change', function(event) {
137137
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
138138
});
139-
});
139+
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "amdclean",
3-
"version": "2.6.0",
3+
"version": "2.6.1",
44
"description": "A build tool that converts AMD code to standard JavaScript",
55
"main": "./src/amdclean",
66
"repository": {
@@ -42,4 +42,4 @@
4242
"node": ">= 0.8"
4343
},
4444
"license": "MIT"
45-
}
45+
}

src/amdclean.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! amdclean - v2.6.0 - 2015-04-02
1+
/*! amdclean - v2.6.1 - 2015-04-10
22
* http://gregfranko.com/amdclean
33
* Copyright (c) 2015 Greg Franko */
44

@@ -26,7 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
2727
*/
2828

29-
;(function(esprima, estraverse, escodegen, _) {
29+
;var sourcemapToAst;(function(esprima, estraverse, escodegen, _) {
3030
// defaultOptions.js
3131
// =================
3232
// AMDclean default options
@@ -1038,7 +1038,10 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
10381038
if (dependencyBlacklist[currentDependency.value] && !shouldOptimize) {
10391039
depNames.push(currentDependency.value);
10401040
} else if (dependencyBlacklist[currentDependency.value] !== 'remove') {
1041-
if (dependencyBlacklist[currentDependency.value]) {
1041+
if (currentDependency.value === 'exports') {
1042+
depNames.push(moduleName);
1043+
amdclean.exportsModules[moduleName] = true;
1044+
} else if (dependencyBlacklist[currentDependency.value]) {
10421045
depNames.push('{}');
10431046
} else {
10441047
depNames.push(currentDependency.value);
@@ -1430,6 +1433,10 @@ clean = function clean() {
14301433
// Creates variable declarations for each AMD module/callback parameter that needs to be hoisted
14311434
_.each(hoistedVariables, function (moduleValue, moduleName) {
14321435
if (!_.contains(options.ignoreModules, moduleName)) {
1436+
var _initValue = amdclean.exportsModules[moduleName] !== true ? null : {
1437+
type: 'ObjectExpression',
1438+
properties: []
1439+
};
14331440
declarations.push({
14341441
'type': 'VariableDeclarator',
14351442
'id': {
@@ -1438,7 +1445,7 @@ clean = function clean() {
14381445
'range': defaultRange,
14391446
'loc': defaultLOC
14401447
},
1441-
'init': null,
1448+
'init': _initValue,
14421449
'range': defaultRange,
14431450
'loc': defaultLOC
14441451
});
@@ -1521,7 +1528,7 @@ clean = function clean() {
15211528
};
15221529
(function () {
15231530
(function (root, factory, undefined) {
1524-
1531+
'use strict';
15251532
// Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, and plain browser loading
15261533
if (typeof define === 'function' && define.amd) {
15271534
factory.amd = true;
@@ -1547,7 +1554,7 @@ clean = function clean() {
15471554
root.amdclean = factory(null, root);
15481555
}
15491556
}(this, function cleanamd(amdDependencies, context) {
1550-
1557+
'use strict';
15511558
// Third-Party Dependencies
15521559
// Note: These dependencies are hoisted to the top (as local variables) at build time (Look in the gulpfile.js file and the AMDclean wrap option for more details)
15531560
sourcemapToAst = function () {
@@ -1642,6 +1649,10 @@ clean = function clean() {
16421649
// --------
16431650
// All of the stored program comments
16441651
this.comments = [];
1652+
// exportsModules
1653+
// --------
1654+
// An object that stores a map of all modules that makes use of the exports parameter in define. Useful when declaring variables and making sure circular dependencies work correctly.
1655+
this.exportsModules = {};
16451656
// options
16461657
// -------
16471658
// Merged user options and default options

src/modules/clean.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ define([
244244
// Creates variable declarations for each AMD module/callback parameter that needs to be hoisted
245245
_.each(hoistedVariables, function(moduleValue, moduleName) {
246246
if (!_.contains(options.ignoreModules, moduleName)) {
247+
var _initValue = amdclean.exportsModules[moduleName] !== true ? null : { type: 'ObjectExpression', properties: [] };
248+
247249
declarations.push({
248250
'type': 'VariableDeclarator',
249251
'id': {
@@ -252,7 +254,7 @@ define([
252254
'range': defaultRange,
253255
'loc': defaultLOC
254256
},
255-
'init': null,
257+
'init': _initValue,
256258
'range': defaultRange,
257259
'loc': defaultLOC
258260
});
@@ -342,4 +344,4 @@ define([
342344

343345
return generatedCode;
344346
};
345-
});
347+
});

src/modules/convertDefinesAndRequires.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ define([
4141
dependencyBlacklist = defaultValues.dependencyBlacklist,
4242
shouldOptimize;
4343

44+
4445
startLineNumber = isDefine || isRequire ? node.expression.loc.start.line : node && node.loc && node.loc.start ? node.loc.start.line : null;
4546
shouldBeIgnored = (amdclean.matchingCommentLineNumbers[startLineNumber] || amdclean.matchingCommentLineNumbers[startLineNumber - 1]);
4647

@@ -120,7 +121,10 @@ define([
120121
if (dependencyBlacklist[currentDependency.value] && !shouldOptimize) {
121122
depNames.push(currentDependency.value);
122123
} else if (dependencyBlacklist[currentDependency.value] !== 'remove') {
123-
if (dependencyBlacklist[currentDependency.value]) {
124+
if (currentDependency.value === "exports") {
125+
depNames.push(moduleName);
126+
amdclean.exportsModules[moduleName] = true;
127+
} else if (dependencyBlacklist[currentDependency.value]) {
124128
depNames.push('{}');
125129
} else {
126130
depNames.push(currentDependency.value);
@@ -294,4 +298,4 @@ define([
294298
return node;
295299
}
296300
};
297-
});
301+
});

src/modules/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ require([
149149
// All of the stored program comments
150150
this.comments = [];
151151

152+
// exportsModules
153+
// --------
154+
// An object that stores a map of all modules that makes use of the exports parameter in define. Useful when declaring variables and making sure circular dependencies work correctly.
155+
this.exportsModules = {};
156+
152157
// options
153158
// -------
154159
// Merged user options and default options
@@ -183,4 +188,4 @@ require([
183188

184189
return publicAPI;
185190
}));
186-
});
191+
});

test/specs/convert.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('amdclean specs', function() {
181181
'globalModules': ['foo']
182182
}),
183183
cleanedCode = amdclean.clean(AMDcode, options),
184-
standardJavaScript = "var foo;foo=function (exports){exports.bar=bar;return exports;}({});window.foo=foo;";
184+
standardJavaScript = "var foo={};foo=function (exports){exports.bar=bar;return exports;}(foo);window.foo=foo;";
185185

186186
expect(cleanedCode).toBe(standardJavaScript);
187187
});
@@ -236,7 +236,7 @@ describe('amdclean specs', function() {
236236
it('should support converting define() methods with identifiers', function() {
237237
var AMDcode = "define('esprima', ['exports'], factory);",
238238
cleanedCode = amdclean.clean(AMDcode, defaultOptions),
239-
standardJavaScript = "var esprima;esprima=function (){return typeof factory==='function'?factory():factory;}();";
239+
standardJavaScript = "var esprima={};esprima=function (){return typeof factory==='function'?factory():factory;}();";
240240

241241
expect(cleanedCode).toBe(standardJavaScript);
242242
});
@@ -603,23 +603,23 @@ describe('amdclean specs', function() {
603603
it('should support the simplified CJS wrapper', function() {
604604
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports, bar){exports.bar = require('./bar');});",
605605
cleanedCode = amdclean.clean(AMDcode, defaultOptions),
606-
standardJavaScript = "var foo;foo=function (exports,bar){exports.bar=bar;return exports;}({},bar);";
606+
standardJavaScript = "var foo={};foo=function (exports,bar){exports.bar=bar;return exports;}(foo,bar);";
607607

608608
expect(cleanedCode).toBe(standardJavaScript);
609609
});
610610

611611
it('should support the plain simplified CJS wrapper', function() {
612612
var AMDcode = "define('foo',['require','exports','module','bar'],function(require, exports){exports.bar = require('bar');});",
613613
cleanedCode = amdclean.clean(AMDcode, defaultOptions),
614-
standardJavaScript = "var foo;foo=function (exports){exports.bar=bar;return exports;}({});";
614+
standardJavaScript = "var foo={};foo=function (exports){exports.bar=bar;return exports;}(foo);";
615615

616616
expect(cleanedCode).toBe(standardJavaScript);
617617
});
618618

619619
it('should support the plain simplified CJS wrapper and not bomb when a variable is not initialized', function() {
620620
var AMDcode = "define('has',['require','exports','module'],function( require, exports, module ){exports.all = function( subject, properties ){if(subject === undefined || typeof subject != 'object'){return false;}var i = 0,len = properties.length,prop; //<--- error thrown because this isn't initialized\nfor(; i < len; i++){prop = properties[i];if(!(prop in subject)){return false;}}return true;};});",
621621
cleanedCode = amdclean.clean(AMDcode, defaultOptions),
622-
standardJavaScript = "var has;has=function (exports){exports.all=function(subject,properties){if(subject===undefined||typeof subject!='object'){return false;}var i=0,len=properties.length,prop;//<--- error thrown because this isn't initialized\nfor(;i<len;i++){prop=properties[i];if(!(prop in subject)){return false;}}return true;};return exports;}({});";
622+
standardJavaScript = "var has={};has=function (exports){exports.all=function(subject,properties){if(subject===undefined||typeof subject!='object'){return false;}var i=0,len=properties.length,prop;//<--- error thrown because this isn't initialized\nfor(;i<len;i++){prop=properties[i];if(!(prop in subject)){return false;}}return true;};return exports;}(has);";
623623

624624
expect(cleanedCode).toBe(standardJavaScript);
625625
});
@@ -656,7 +656,7 @@ describe('amdclean specs', function() {
656656

657657
it('should support the Require.js optimizer cjsTranslate option that converts CommonJS modules to AMD modules', function(done) {
658658
var cleanedCode,
659-
standardJavaScript = "var commonjs3,commonjs2,commonjs4,commonjs1;commonjs3=function (exports){exports.exampleFunc=function(){var test=true;return test;};return exports;}({});commonjs2=function (exports){exports={'exampleBool':true,'exampleFunc':commonjs3.exampleFunc};return exports;}({});commonjs4=function (exports){exports.test='this is a test';return exports;}({});commonjs1=function (exports,__commonjs2__,_commonjs4_){var commonjs2=__commonjs2__;var _commonjs2_='blah';var commonjs4=_commonjs4_;commonjs2.exampleFunc();return exports;}({},commonjs2,commonjs4);";
659+
standardJavaScript = "var commonjs3={},commonjs2={},commonjs4={},commonjs1={};commonjs3=function (exports){exports.exampleFunc=function(){var test=true;return test;};return exports;}(commonjs3);commonjs2=function (exports){exports={'exampleBool':true,'exampleFunc':commonjs3.exampleFunc};return exports;}(commonjs2);commonjs4=function (exports){exports.test='this is a test';return exports;}(commonjs4);commonjs1=function (exports,__commonjs2__,_commonjs4_){var commonjs2=__commonjs2__;var _commonjs2_='blah';var commonjs4=_commonjs4_;commonjs2.exampleFunc();return exports;}(commonjs1,commonjs2,commonjs4);";
660660

661661
requirejs.optimize({
662662
'baseUrl': './test/',
@@ -945,4 +945,4 @@ describe('amdclean specs', function() {
945945
});
946946
});
947947

948-
});
948+
});

0 commit comments

Comments
 (0)