Skip to content

Commit 4833240

Browse files
committed
Releasing AMDClean 1.5.0
1 parent 03e61a4 commit 4833240

File tree

4 files changed

+88
-54
lines changed

4 files changed

+88
-54
lines changed

build/amdclean.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "amdclean",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"description": "A build tool that converts AMD code to standard JavaScript",
55
"main": "./src/amdclean",
66
"repository": {

src/amdclean.js

+49-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! amdclean - v1.4.1 - 2014-03-17
1+
/*! amdclean - v1.5.0 - 2014-03-17
22
* http://gregfranko.com/amdclean
33
* Copyright (c) 2014 Greg Franko; Licensed MIT*/
44

@@ -74,7 +74,7 @@
7474
// The Public API object
7575
publicAPI = {
7676
// Current project version number
77-
'VERSION': '1.4.1',
77+
'VERSION': '1.5.0',
7878
// Default Options
7979
'defaultOptions': {
8080
// The source code you would like to be 'cleaned'
@@ -99,7 +99,12 @@
9999
},
100100
// All escodegen API options are supported: https://github.com/Constellation/escodegen/wiki/API
101101
'escodegen': {
102-
'comment': true
102+
'comment': true,
103+
'format': {
104+
'indent': {
105+
'adjustMultilineComment': true
106+
}
107+
}
103108
},
104109
// If there is a comment (that contains the following text) on the same line or one line above a specific module, the module will not be removed
105110
'commentCleanName': 'amdclean',
@@ -111,6 +116,9 @@
111116
'removeAllRequires': false,
112117
// Determines if all of the 'use strict' statements will be removed
113118
'removeUseStricts': true,
119+
// Determines if conditional AMD checks are transformed
120+
// e.g. if(typeof define == 'function') {} -> if(true) {}
121+
'transformAMDChecks': true,
114122
// Allows you to pass an expression that will override shimmed modules return values
115123
// e.g. { 'backbone': 'window.Backbone' }
116124
'shimOverrides': {},
@@ -157,9 +165,9 @@
157165
// --------------------
158166
// Variable names that are not allowed as dependencies to functions
159167
'dependencyBlacklist': {
160-
'require': true,
168+
'require': 'remove',
161169
'exports': true,
162-
'module': true
170+
'module': 'remove'
163171
},
164172
// defaultLOC
165173
// ----------
@@ -259,10 +267,10 @@
259267
// Returns if the current AST node is an if statement AMD check
260268
// e.g. if(typeof define === 'function') {}
261269
'isAMDConditional': function(node) {
262-
if(node && node.type !== 'IfStatement' ||
270+
if(publicAPI.options.transformAMDChecks !== true || (node && node.type !== 'IfStatement' ||
263271
!_.isObject(node.test) ||
264272
!_.isObject(node.test.left) ||
265-
_.isNull(node.test.left.value)) {
273+
_.isNull(node.test.left.value))) {
266274
return false;
267275
}
268276
var matchObject = {
@@ -312,22 +320,26 @@
312320
// normalizeModuleName
313321
// -------------------
314322
// Returns a normalized module name (removes relative file path urls)
315-
'normalizeModuleName': function(name) {
323+
'normalizeModuleName': function(name, moduleId) {
316324
var pre_normalized,
317325
post_normalized,
318326
prefixMode = publicAPI.options.prefixMode,
319327
prefixTransform = publicAPI.options.prefixTransform,
320328
prefixTransformValue;
321329
name = name || '';
322330
if(name === '{}') {
323-
return name;
331+
if(publicAPI.dependencyBlacklist[name] === 'remove') {
332+
return '';
333+
} else {
334+
return name;
335+
}
324336
}
325337
pre_normalized = publicAPI.prefixReservedWords(name.replace(/\./g,'').
326338
replace(/[^A-Za-z0-9_$]/g,'_').
327339
replace(/^_+/,''));
328340
post_normalized = prefixMode === 'camelCase' ? publicAPI.convertToCamelCase(pre_normalized) : pre_normalized;
329341
if(_.isFunction(prefixTransform)) {
330-
prefixTransformValue = prefixTransform(post_normalized);
342+
prefixTransformValue = prefixTransform(post_normalized, moduleId);
331343
if(_.isString(prefixTransformValue) && prefixTransformValue.length) {
332344
return prefixTransformValue;
333345
}
@@ -505,9 +517,6 @@
505517
// Returns a function expression that is executed immediately
506518
// e.g. var example = function(){}()
507519
'convertToIIFEDeclaration': function(obj) {
508-
// console.log('obj.callbackFunc', obj.callbackFunc);
509-
// console.log('obj.callbackFunc.body.body', obj.callbackFunc.body.body);
510-
// return;
511520
var moduleName = obj.moduleName,
512521
callbackFuncParams = obj.callbackFuncParams,
513522
isOptimized = obj.isOptimized,
@@ -531,7 +540,7 @@
531540
'range': (cbFunc.range || publicAPI.defaultRange),
532541
'loc': (cbFunc.loc || publicAPI.defaultLOC)
533542
},
534-
'arguments': [],
543+
'arguments': callbackFuncParams,
535544
'range': (cbFunc.range || publicAPI.defaultRange),
536545
'loc': (cbFunc.loc || publicAPI.defaultLOC)
537546
},
@@ -699,10 +708,9 @@
699708
options = publicAPI.options,
700709
dependencyNames = (function() {
701710
var deps = [],
702-
iterator = -1,
703711
currentName;
704-
while(++iterator < depLength) {
705-
currentName = publicAPI.normalizeDependencyName(moduleId, dependencies[iterator]);
712+
_.each(dependencies, function(currentDependency, iterator) {
713+
currentName = publicAPI.normalizeModuleName(publicAPI.normalizeDependencyName(moduleId, currentDependency), moduleId);
706714
if(options.globalObject === true && options.globalObjectName && currentName !== '{}') {
707715
deps.push({
708716
'type': 'MemberExpression',
@@ -715,24 +723,24 @@
715723
},
716724
'property': {
717725
'type': 'Literal',
718-
'value': publicAPI.normalizeModuleName(currentName),
719-
'raw': "" + publicAPI.normalizeModuleName(currentName) + "",
726+
'value': currentName,
727+
'raw': "" + currentName + "",
720728
'range': publicAPI.defaultRange,
721729
'loc': publicAPI.defaultLOC
722730
},
723-
'name': publicAPI.normalizeModuleName(currentName),
731+
'name': currentName,
724732
'range': publicAPI.defaultRange,
725733
'loc': publicAPI.defaultLOC
726734
});
727735
} else {
728736
deps.push({
729737
'type': 'Identifier',
730-
'name': publicAPI.normalizeModuleName(currentName),
738+
'name': currentName,
731739
'range': publicAPI.defaultRange,
732740
'loc': publicAPI.defaultLOC
733741
});
734742
}
735-
}
743+
});
736744
return deps;
737745
}()),
738746
callbackFunc = (function() {
@@ -792,30 +800,27 @@
792800
hasExportsParam = false,
793801
callbackFuncParams = (function() {
794802
var deps = [],
795-
iterator = -1,
796-
currentParam,
797803
currentName,
798-
cbParams = callbackFunc.params || [];
799-
while(++iterator < depLength) {
800-
currentParam = cbParams[iterator];
804+
cbParams = callbackFunc.params || dependencyNames || [];
805+
_.each(cbParams, function(currentParam, iterator) {
801806
if(currentParam) {
802807
currentName = currentParam.name;
803808
} else {
809+
console.log('iterator', iterator);
804810
currentName = dependencyNames[iterator].name;
805811
}
806812
if(currentName === 'exports') {
807813
hasExportsParam = true;
808814
}
809-
if(currentName === '{}') {
810-
currentName = 'module';
815+
if(currentName !== '{}' && publicAPI.dependencyBlacklist[currentName] !== 'remove') {
816+
deps.push({
817+
'type': 'Identifier',
818+
'name': currentName,
819+
'range': publicAPI.defaultRange,
820+
'loc': publicAPI.defaultLOC
821+
});
811822
}
812-
deps.push({
813-
'type': 'Identifier',
814-
'name': currentName,
815-
'range': publicAPI.defaultRange,
816-
'loc': publicAPI.defaultLOC
817-
});
818-
}
823+
});
819824
return deps;
820825
}());
821826

@@ -912,10 +917,14 @@
912917
}
913918
if(Array.isArray(deps) && deps.length) {
914919
_.each(deps, function(currentDependency) {
915-
if(publicAPI.dependencyBlacklist[currentDependency.value]) {
916-
depNames.push('{}');
917-
} else {
918-
depNames.push(currentDependency.value);
920+
if(publicAPI.dependencyBlacklist[currentDependency.value] !== 'remove') {
921+
if(publicAPI.dependencyBlacklist[currentDependency.value]) {
922+
if(publicAPI.dependencyBlacklist[currentDependency.value] !== 'remove') {
923+
depNames.push('{}');
924+
}
925+
} else {
926+
depNames.push(currentDependency.value);
927+
}
919928
}
920929
});
921930
}

test/specs/convert.js

+37-12
Original file line numberDiff line numberDiff line change
@@ -150,33 +150,33 @@ describe('amdclean specs', function() {
150150
});
151151

152152
it('should support the simplified CJS wrapper', function() {
153-
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
153+
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports, bar){exports.bar = require('./bar');});",
154154
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
155-
standardJavaScript = "var foo=function (require,exports,bar){exports.bar=bar;return exports;}({},{},bar);";
155+
standardJavaScript = "var foo=function (exports,bar){exports.bar=bar;return exports;}({},bar);";
156156

157157
expect(cleanedCode).toBe(standardJavaScript);
158158
});
159159

160160
it('should support the plain simplified CJS wrapper', function() {
161161
var AMDcode = "define('foo',['require','exports','module','bar'],function(require, exports){exports.bar = require('bar');});",
162162
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
163-
standardJavaScript = "var foo=function (require,exports,module,bar){exports.bar=bar;return exports;}({},{},{},bar);";
163+
standardJavaScript = "var foo=function (exports){exports.bar=bar;return exports;}({},bar);";
164164

165165
expect(cleanedCode).toBe(standardJavaScript);
166166
});
167167

168168
it('should support global modules', function() {
169169
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
170170
cleanedCode = amdclean.clean({ globalModules: ['foo'], code: AMDcode, escodegen: { format: { compact: true } } }),
171-
standardJavaScript = "var foo=function (require,exports,bar){exports.bar=bar;return exports;}({},{},bar);window.foo=foo;";
171+
standardJavaScript = "var foo=function (exports){exports.bar=bar;return exports;}({},bar);window.foo=foo;";
172172

173173
expect(cleanedCode).toBe(standardJavaScript);
174174
});
175175

176176
it('should support storing modules inside of a global object', function() {
177177
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
178178
cleanedCode = amdclean.clean({ globalObject: true, rememberGlobalObject: false, globalObjectName: 'yeabuddy', code: AMDcode, escodegen: { format: { compact: true } } }),
179-
standardJavaScript = "var yeabuddy={};yeabuddy['foo']=function (require,exports,bar){exports.bar=yeabuddy['bar'];return exports;}({},{},yeabuddy['bar']);";
179+
standardJavaScript = "var yeabuddy={};yeabuddy['foo']=function (exports){exports.bar=yeabuddy['bar'];return exports;}({},yeabuddy['bar']);";
180180

181181
expect(cleanedCode).toBe(standardJavaScript);
182182
});
@@ -200,7 +200,7 @@ describe('amdclean specs', function() {
200200
it('should support converting define() methods with identifiers', function() {
201201
var AMDcode = "define('esprima', ['exports'], factory);",
202202
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
203-
standardJavaScript = "var esprima=function (module){return factory();}({});";
203+
standardJavaScript = "var esprima=function (){return factory();}({});";
204204

205205
expect(cleanedCode).toBe(standardJavaScript);
206206
});
@@ -229,6 +229,14 @@ describe('amdclean specs', function() {
229229
expect(cleanedCode).toBe(standardJavaScript);
230230
});
231231

232+
it('should not automatically convert conditional AMD checks if the transformAMDChecks option is set to false', function() {
233+
var AMDcode = "if(typeof define === 'function') {}",
234+
cleanedCode = amdclean.clean({ code: AMDcode, transformAMDChecks: false, escodegen: { format: { compact: true } } }),
235+
standardJavaScript = "if(typeof define==='function'){}";
236+
237+
expect(cleanedCode).toBe(standardJavaScript);
238+
});
239+
232240
describe('optimized defines', function() {
233241

234242
it('should optimize basic define() methods that return a function expression', function() {
@@ -290,7 +298,7 @@ describe('amdclean specs', function() {
290298
it('should not optimize define() methods that have one or more dependencies', function() {
291299
var AMDcode = "define('optimized', ['exampleDependency'], function () { return function ( thing ) { var anotherThing = true; return !isNaN( parseFloat( thing ) ) && isFinite( thing );};});",
292300
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
293-
standardJavaScript = "var optimized=function (exampleDependency){return function(thing){var anotherThing=true;return!isNaN(parseFloat(thing))&&isFinite(thing);};}(exampleDependency);";
301+
standardJavaScript = "var optimized=function (){return function(thing){var anotherThing=true;return!isNaN(parseFloat(thing))&&isFinite(thing);};}(exampleDependency);";
294302

295303
expect(cleanedCode).toBe(standardJavaScript);
296304
});
@@ -314,7 +322,7 @@ describe('amdclean specs', function() {
314322
it('should not optimize basic define() methods that return a literal value that have one or more dependencies', function() {
315323
var AMDcode = "define('optimized', ['someDependency'], function() { return 'Convert AMD code to standard JavaScript';});",
316324
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
317-
standardJavaScript = "var optimized=function (someDependency){return'Convert AMD code to standard JavaScript';}(someDependency);";
325+
standardJavaScript = "var optimized=function (){return'Convert AMD code to standard JavaScript';}(someDependency);";
318326

319327
expect(cleanedCode).toBe(standardJavaScript);
320328
});
@@ -368,7 +376,7 @@ describe('amdclean specs', function() {
368376
compact: true
369377
}
370378
},
371-
prefixTransform: function(moduleName) {
379+
prefixTransform: function(moduleName, moduleId) {
372380
return moduleName.substring(moduleName.lastIndexOf('_') + 1, moduleName.length);
373381
}
374382
}),
@@ -518,7 +526,7 @@ describe('amdclean specs', function() {
518526
it('should not remove require() calls with a non-empty callback function', function() {
519527
var AMDcode = "require(['testModule'], function() {var test=true;});",
520528
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
521-
standardJavaScript = "(function(testModule){var test=true;}(testModule));";
529+
standardJavaScript = "(function(){var test=true;}(testModule));";
522530

523531
expect(cleanedCode).toBe(standardJavaScript);
524532
});
@@ -543,7 +551,7 @@ describe('amdclean specs', function() {
543551
var AMDcode = "(function (root, factory) {" +
544552
"'use strict';" +
545553
"if (typeof define === 'function') {" +
546-
"define(['exports'], factory);" +
554+
"define('esprima', ['exports'], factory);" +
547555
"} else if (typeof exports !== 'undefined') {" +
548556
"factory(exports);" +
549557
"} else {" +
@@ -553,7 +561,24 @@ describe('amdclean specs', function() {
553561
"var test = true;" +
554562
"}));",
555563
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
556-
standardJavaScript = "(function(root,factory){'use strict';if(true){var =function (module){return factory();}({});}else if(typeof exports!=='undefined'){factory(exports);}else{factory(root.esprima={});}}(this,function(exports){exports=exports||{};var test=true;return exports;}));";
564+
standardJavaScript = "(function(root,factory){'use strict';if(true){var esprima=function (){return factory();}({});}else if(typeof exports!=='undefined'){factory(exports);}else{factory(root.esprima={});}}(this,function(exports){exports=exports||{};var test=true;return exports;}));";
565+
566+
expect(cleanedCode).toBe(standardJavaScript);
567+
});
568+
569+
it('should correctly convert libraries that use factory function parameters', function() {
570+
var AMDcode = "(function (factory) {" +
571+
"if (typeof exports === 'object') {" +
572+
"module.exports = factory(require('backbone'), require('underscore'));" +
573+
"} else if (typeof define === 'function' && define.amd) {" +
574+
"define('backbonevalidation', ['backbone', 'underscore'], factory);" +
575+
"}" +
576+
"}(function (Backbone, _) {" +
577+
"//= backbone-validation.js\n" +
578+
"return Backbone.Validation;" +
579+
"}));",
580+
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
581+
standardJavaScript = "(function(factory){if(typeof exports==='object'){module.exports=factory(backbone,underscore);}else if(true){var backbonevalidation=function (backbone,underscore){return factory(backbone,underscore);}(backbone,underscore);}}(function(Backbone,_){//= backbone-validation.js\nreturn Backbone.Validation;}));";
557582

558583
expect(cleanedCode).toBe(standardJavaScript);
559584
});

0 commit comments

Comments
 (0)