@@ -150,33 +150,33 @@ describe('amdclean specs', function() {
150
150
} ) ;
151
151
152
152
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');});" ,
154
154
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);" ;
156
156
157
157
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
158
158
} ) ;
159
159
160
160
it ( 'should support the plain simplified CJS wrapper' , function ( ) {
161
161
var AMDcode = "define('foo',['require','exports','module','bar'],function(require, exports){exports.bar = require('bar');});" ,
162
162
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);" ;
164
164
165
165
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
166
166
} ) ;
167
167
168
168
it ( 'should support global modules' , function ( ) {
169
169
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});" ,
170
170
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;" ;
172
172
173
173
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
174
174
} ) ;
175
175
176
176
it ( 'should support storing modules inside of a global object' , function ( ) {
177
177
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});" ,
178
178
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']);" ;
180
180
181
181
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
182
182
} ) ;
@@ -200,7 +200,7 @@ describe('amdclean specs', function() {
200
200
it ( 'should support converting define() methods with identifiers' , function ( ) {
201
201
var AMDcode = "define('esprima', ['exports'], factory);" ,
202
202
cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
203
- standardJavaScript = "var esprima=function (module ){return factory();}({});" ;
203
+ standardJavaScript = "var esprima=function (){return factory();}({});" ;
204
204
205
205
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
206
206
} ) ;
@@ -229,6 +229,14 @@ describe('amdclean specs', function() {
229
229
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
230
230
} ) ;
231
231
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
+
232
240
describe ( 'optimized defines' , function ( ) {
233
241
234
242
it ( 'should optimize basic define() methods that return a function expression' , function ( ) {
@@ -290,7 +298,7 @@ describe('amdclean specs', function() {
290
298
it ( 'should not optimize define() methods that have one or more dependencies' , function ( ) {
291
299
var AMDcode = "define('optimized', ['exampleDependency'], function () { return function ( thing ) { var anotherThing = true; return !isNaN( parseFloat( thing ) ) && isFinite( thing );};});" ,
292
300
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);" ;
294
302
295
303
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
296
304
} ) ;
@@ -314,7 +322,7 @@ describe('amdclean specs', function() {
314
322
it ( 'should not optimize basic define() methods that return a literal value that have one or more dependencies' , function ( ) {
315
323
var AMDcode = "define('optimized', ['someDependency'], function() { return 'Convert AMD code to standard JavaScript';});" ,
316
324
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);" ;
318
326
319
327
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
320
328
} ) ;
@@ -368,7 +376,7 @@ describe('amdclean specs', function() {
368
376
compact : true
369
377
}
370
378
} ,
371
- prefixTransform : function ( moduleName ) {
379
+ prefixTransform : function ( moduleName , moduleId ) {
372
380
return moduleName . substring ( moduleName . lastIndexOf ( '_' ) + 1 , moduleName . length ) ;
373
381
}
374
382
} ) ,
@@ -518,7 +526,7 @@ describe('amdclean specs', function() {
518
526
it ( 'should not remove require() calls with a non-empty callback function' , function ( ) {
519
527
var AMDcode = "require(['testModule'], function() {var test=true;});" ,
520
528
cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
521
- standardJavaScript = "(function(testModule ){var test=true;}(testModule));" ;
529
+ standardJavaScript = "(function(){var test=true;}(testModule));" ;
522
530
523
531
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
524
532
} ) ;
@@ -543,7 +551,7 @@ describe('amdclean specs', function() {
543
551
var AMDcode = "(function (root, factory) {" +
544
552
"'use strict';" +
545
553
"if (typeof define === 'function') {" +
546
- "define(['exports'], factory);" +
554
+ "define('esprima', ['exports'], factory);" +
547
555
"} else if (typeof exports !== 'undefined') {" +
548
556
"factory(exports);" +
549
557
"} else {" +
@@ -553,7 +561,24 @@ describe('amdclean specs', function() {
553
561
"var test = true;" +
554
562
"}));" ,
555
563
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;}));" ;
557
582
558
583
expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
559
584
} ) ;
0 commit comments