@@ -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