Skip to content

Commit ce5d2f9

Browse files
committed
Added the new createAnonymousAMDModule option and adjusted the way the third-party dependencies work
1 parent 9fc6c98 commit ce5d2f9

File tree

8 files changed

+106
-52
lines changed

8 files changed

+106
-52
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ amdclean.clean({
534534
// Determines if conditional AMD checks are transformed
535535
// e.g. if(typeof define == 'function') {} -> if(true) {}
536536
'transformAMDChecks': true,
537+
// Determines if a named or anonymous AMD module will be created inside of your conditional AMD check
538+
// Note: This is only applicable to JavaScript libraries, do not change this for web apps
539+
// If set to true: e.g. define('example', [], function() {}) -> define([], function() {})
540+
'createAnonymousAMDModule': false,
537541
// Allows you to pass an expression that will override shimmed modules return
538542
// values e.g. { 'backbone': 'window.Backbone' }
539543
'shimOverrides': {},
@@ -700,6 +704,10 @@ __I am building a JavaScript library and want to provide conditional AMD support
700704

701705
2. Make sure that you have a comment (that matches your AMDclean `commentCleanName` option) one line above your conditional AMD if statement
702706

707+
__I am building a JavaScript library and want to create a conditional anonymous AMD module, but Require.js and AMDclean seems to always setting a module ID. How do I fix this?__
708+
709+
- It's easy, just make sure to set the `createAnonymousAMDModule` option to `true`,
710+
703711

704712
__I don't like the way AMDclean normalizes the names of my modules with underscores. Can I change this?__
705713

build/amdclean.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
2727
*/
2828

29-
;(function() {
30-
var esprima, estraverse, escodegen, _;
29+
;(function(esprima, estraverse, escodegen, _) {
3130
// defaultOptions.js
3231
// =================
3332
// AMDclean default options
34-
var defaultOptions, errorMsgs, defaultValues, utils, convertToIIFE, convertToIIFEDeclaration, normalizeModuleName, convertToFunctionExpression, convertToObjectDeclaration, createAst, convertDefinesAndRequires, traverseAndUpdateAst, getNormalizedModuleName, findAndStoreAllModuleIds, generateCode, clean, index;
33+
var defaultOptions, errorMsgs, defaultValues, utils, convertToIIFE, convertToIIFEDeclaration, normalizeModuleName, convertToFunctionExpression, convertToObjectDeclaration, createAst, convertDefinesAndRequires, traverseAndUpdateAst, getNormalizedModuleName, findAndStoreAllModuleIds, generateCode, clean;
3534
defaultOptions = {
3635
'code': '',
3736
'filePath': '',
@@ -52,6 +51,7 @@ defaultOptions = {
5251
'removeAllRequires': false,
5352
'removeUseStricts': true,
5453
'transformAMDChecks': true,
54+
'createAnonymousAMDModule': false,
5555
'shimOverrides': {},
5656
'prefixMode': 'standard',
5757
'prefixTransform': function (moduleName) {
@@ -802,15 +802,22 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
802802
}
803803
// If the AMD conditional statement should not be transformed
804804
if (options.transformAMDChecks === false) {
805-
// Add the module name to the ignore list
806-
if (node.consequent && _.isArray(node.consequent.body) && node.consequent.body.length) {
807-
moduleToBeIgnored = node.consequent.body[0];
808-
if (moduleToBeIgnored.expression && moduleToBeIgnored.expression.arguments && moduleToBeIgnored.expression.arguments.length) {
809-
if (moduleToBeIgnored.expression.arguments[0] && moduleToBeIgnored.expression.arguments[0].value) {
810-
amdclean.conditionalModulesToIgnore[moduleToBeIgnored.expression.arguments[0].value] = true;
805+
estraverse.traverse(node, {
806+
'enter': function (node) {
807+
if (utils.isDefine(node)) {
808+
if (node.expression && node.expression.arguments && node.expression.arguments.length) {
809+
// Add the module name to the ignore list
810+
if (node.expression.arguments[0].type === 'Literal' && node.expression.arguments[0].value) {
811+
amdclean.conditionalModulesToIgnore[node.expression.arguments[0].value] = true;
812+
if (options.createAnonymousAMDModule === true) {
813+
amdclean.storedModules[node.expression.arguments[0].value] = false;
814+
node.expression.arguments.shift();
815+
}
816+
}
817+
}
811818
}
812819
}
813-
}
820+
});
814821
}
815822
}
816823
if (isDefine || isRequire) {
@@ -1292,12 +1299,19 @@ clean = function clean() {
12921299
factory.env = 'web';
12931300
}
12941301
factory.amd = true;
1295-
index = factory({
1296-
'esprima': esprima,
1297-
'estraverse': estraverse,
1298-
'escodegen': escodegen,
1299-
'underscore': underscore
1300-
}, root);
1302+
define([
1303+
'esprima',
1304+
'estraverse',
1305+
'escodegen',
1306+
'underscore'
1307+
], function (esprima, estraverse, escodegen, underscore) {
1308+
return factory({
1309+
'esprima': esprima,
1310+
'estraverse': estraverse,
1311+
'escodegen': escodegen,
1312+
'underscore': underscore
1313+
}, root);
1314+
});
13011315
} else if (typeof exports !== 'undefined') {
13021316
factory.env = 'node';
13031317
module.exports = factory(null, root);
@@ -1406,4 +1420,4 @@ clean = function clean() {
14061420
}();
14071421
return publicAPI;
14081422
}));
1409-
}());}());
1423+
}());}(typeof esprima !== "undefined" ? esprima: null, typeof estraverse !== "undefined" ? estraverse: null, typeof escodegen !== "undefined" ? escodegen: null, typeof _ !== "undefined" ? _ : null));

build/amdclean.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ gulp.task('build', function() {
4545
'findNestedDependencies': false,
4646
'baseUrl': './src/modules/',
4747
'optimize': 'none',
48-
'include': ['index'],
48+
'paths': {
49+
'amdclean': 'index'
50+
},
51+
'include': ['amdclean'],
4952
'out': './src/amdclean.js',
5053
'onModuleBundleComplete': function(data) {
5154
var outputFile = data.path,
@@ -60,9 +63,10 @@ gulp.task('build', function() {
6063
'wrap': {
6164
// All of the third party dependencies are hoisted here
6265
// It's a hack, but it's not too painful
63-
'start': ';(function() {\nvar esprima, estraverse, escodegen, _;\n',
64-
'end': '}());'
65-
}
66+
'start': ';(function(esprima, estraverse, escodegen, _) {\n',
67+
'end': '}(typeof esprima !== "undefined" ? esprima: null, typeof estraverse !== "undefined" ? estraverse: null, typeof escodegen !== "undefined" ? escodegen: null, typeof _ !== "undefined" ? _ : null));'
68+
},
69+
'createAnonymousAMDModule': true
6670
});
6771
} catch(e) {
6872
error = true;

src/amdclean.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
2727
*/
2828

29-
;(function() {
30-
var esprima, estraverse, escodegen, _;
29+
;(function(esprima, estraverse, escodegen, _) {
3130
// defaultOptions.js
3231
// =================
3332
// AMDclean default options
34-
var defaultOptions, errorMsgs, defaultValues, utils, convertToIIFE, convertToIIFEDeclaration, normalizeModuleName, convertToFunctionExpression, convertToObjectDeclaration, createAst, convertDefinesAndRequires, traverseAndUpdateAst, getNormalizedModuleName, findAndStoreAllModuleIds, generateCode, clean, index;
33+
var defaultOptions, errorMsgs, defaultValues, utils, convertToIIFE, convertToIIFEDeclaration, normalizeModuleName, convertToFunctionExpression, convertToObjectDeclaration, createAst, convertDefinesAndRequires, traverseAndUpdateAst, getNormalizedModuleName, findAndStoreAllModuleIds, generateCode, clean;
3534
defaultOptions = {
3635
'code': '',
3736
'filePath': '',
@@ -52,6 +51,7 @@ defaultOptions = {
5251
'removeAllRequires': false,
5352
'removeUseStricts': true,
5453
'transformAMDChecks': true,
54+
'createAnonymousAMDModule': false,
5555
'shimOverrides': {},
5656
'prefixMode': 'standard',
5757
'prefixTransform': function (moduleName) {
@@ -802,15 +802,22 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
802802
}
803803
// If the AMD conditional statement should not be transformed
804804
if (options.transformAMDChecks === false) {
805-
// Add the module name to the ignore list
806-
if (node.consequent && _.isArray(node.consequent.body) && node.consequent.body.length) {
807-
moduleToBeIgnored = node.consequent.body[0];
808-
if (moduleToBeIgnored.expression && moduleToBeIgnored.expression.arguments && moduleToBeIgnored.expression.arguments.length) {
809-
if (moduleToBeIgnored.expression.arguments[0] && moduleToBeIgnored.expression.arguments[0].value) {
810-
amdclean.conditionalModulesToIgnore[moduleToBeIgnored.expression.arguments[0].value] = true;
805+
estraverse.traverse(node, {
806+
'enter': function (node) {
807+
if (utils.isDefine(node)) {
808+
if (node.expression && node.expression.arguments && node.expression.arguments.length) {
809+
// Add the module name to the ignore list
810+
if (node.expression.arguments[0].type === 'Literal' && node.expression.arguments[0].value) {
811+
amdclean.conditionalModulesToIgnore[node.expression.arguments[0].value] = true;
812+
if (options.createAnonymousAMDModule === true) {
813+
amdclean.storedModules[node.expression.arguments[0].value] = false;
814+
node.expression.arguments.shift();
815+
}
816+
}
817+
}
811818
}
812819
}
813-
}
820+
});
814821
}
815822
}
816823
if (isDefine || isRequire) {
@@ -1292,12 +1299,19 @@ clean = function clean() {
12921299
factory.env = 'web';
12931300
}
12941301
factory.amd = true;
1295-
index = factory({
1296-
'esprima': esprima,
1297-
'estraverse': estraverse,
1298-
'escodegen': escodegen,
1299-
'underscore': underscore
1300-
}, root);
1302+
define([
1303+
'esprima',
1304+
'estraverse',
1305+
'escodegen',
1306+
'underscore'
1307+
], function (esprima, estraverse, escodegen, underscore) {
1308+
return factory({
1309+
'esprima': esprima,
1310+
'estraverse': estraverse,
1311+
'escodegen': escodegen,
1312+
'underscore': underscore
1313+
}, root);
1314+
});
13011315
} else if (typeof exports !== 'undefined') {
13021316
factory.env = 'node';
13031317
module.exports = factory(null, root);
@@ -1406,4 +1420,4 @@ clean = function clean() {
14061420
}();
14071421
return publicAPI;
14081422
}));
1409-
}());}());
1423+
}());}(typeof esprima !== "undefined" ? esprima: null, typeof estraverse !== "undefined" ? estraverse: null, typeof escodegen !== "undefined" ? escodegen: null, typeof _ !== "undefined" ? _ : null));

src/modules/convertDefinesAndRequires.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,22 @@ define([
6666

6767
// If the AMD conditional statement should not be transformed
6868
if(options.transformAMDChecks === false) {
69-
70-
// Add the module name to the ignore list
71-
if(node.consequent && _.isArray(node.consequent.body) && node.consequent.body.length) {
72-
73-
moduleToBeIgnored = node.consequent.body[0];
74-
75-
if(moduleToBeIgnored.expression && moduleToBeIgnored.expression.arguments && moduleToBeIgnored.expression.arguments.length) {
76-
77-
if(moduleToBeIgnored.expression.arguments[0] && moduleToBeIgnored.expression.arguments[0].value) {
78-
amdclean.conditionalModulesToIgnore[moduleToBeIgnored.expression.arguments[0].value] = true;
69+
estraverse.traverse(node, {
70+
'enter': function(node) {
71+
if(utils.isDefine(node)) {
72+
if(node.expression && node.expression.arguments && node.expression.arguments.length) {
73+
// Add the module name to the ignore list
74+
if(node.expression.arguments[0].type === 'Literal' && node.expression.arguments[0].value) {
75+
amdclean.conditionalModulesToIgnore[node.expression.arguments[0].value] = true;
76+
if(options.createAnonymousAMDModule === true) {
77+
amdclean.storedModules[node.expression.arguments[0].value] = false;
78+
node.expression.arguments.shift();
79+
}
80+
}
81+
}
7982
}
80-
8183
}
82-
}
83-
84+
});
8485
}
8586
}
8687

src/modules/defaultOptions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ define({
4040
// Determines if conditional AMD checks are transformed
4141
// e.g. if(typeof define == 'function') {} -> if(true) {}
4242
'transformAMDChecks': true,
43+
// Determines if a named or anonymous AMD module will be created inside of your conditional AMD check
44+
// Note: This is only applicable to JavaScript libraries, do not change this for web apps
45+
// If set to true: e.g. define('example', [], function() {}) -> define([], function() {})
46+
'createAnonymousAMDModule': false,
4347
// Allows you to pass an expression that will override shimmed modules return values
4448
// e.g. { 'backbone': 'window.Backbone' }
4549
'shimOverrides': {},

test/specs/convert.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ describe('amdclean specs', function() {
231231
expect(cleanedCode).toBe(standardJavaScript);
232232
});
233233

234+
it('should create an anonymous AMD module, if the transformAMDChecks option is set to false and the createAnonymousAMDModule option is set to true', function() {
235+
var AMDcode = "if(typeof define === 'function') { define('test', [], function() {}); }",
236+
options = _.merge(_.cloneDeep(defaultOptions), { 'transformAMDChecks': false, 'createAnonymousAMDModule': true }),
237+
cleanedCode = amdclean.clean(AMDcode, options),
238+
standardJavaScript = "if(typeof define==='function'){define([],function(){});}";
239+
240+
expect(cleanedCode).toBe(standardJavaScript);
241+
});
242+
234243
describe('optimized defines', function() {
235244

236245
it('should optimize basic define() methods that return a function expression', function() {

0 commit comments

Comments
 (0)