Skip to content

Commit 6004e6a

Browse files
committed
Re: #35, #32 - Releasing AMDClean v1.3.0
1 parent 769b263 commit 6004e6a

File tree

4 files changed

+89
-50
lines changed

4 files changed

+89
-50
lines changed

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.

package.json

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

src/amdclean.js

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! amdclean - v1.2.1 - 2014-02-17
1+
/*! amdclean - v1.3.0 - 2014-03-03
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.2.1',
77+
'VERSION': '1.3.0',
7878
// Default Options
7979
'defaultOptions': {
8080
// The source code you would like to be 'cleaned'
@@ -559,57 +559,54 @@
559559
}());
560560
return updatedNode;
561561
},
562-
'normalizeDepId': function(moduleId, dep) {
563-
if(!moduleId || !dep) {
562+
// isRelativeFilePath
563+
// ------------------
564+
// Returns a boolean that determines if the file path provided is a relative file path
565+
// e.g. ../exampleModule -> true
566+
isRelativeFilePath: function(path) {
567+
var segments = path.split('/');
568+
569+
return segments.length !== -1 && (segments[0] === '.' || segments[0] === '..');
570+
},
571+
// normalizeDependencyName
572+
// -----------------------
573+
// Returns a normalized dependency name that handles relative file paths
574+
'normalizeDependencyName': function(moduleId, dep) {
575+
if(!moduleId || !dep || !publicAPI.isRelativeFilePath(dep)) {
564576
return dep;
565577
}
566578

567-
var normalizePath,
568-
baseName,
569-
isRelative;
579+
var normalizePath = function(path) {
580+
var segments = path.split('/'),
581+
normalizedSegments;
570582

571-
normalizePath = function(path) {
572-
var segments = path.split('/'),
573-
normalizedSegments;
574-
575-
normalizedSegments = _.reduce(segments, function(memo, segment) {
576-
switch(segment) {
577-
case '.':
578-
break;
579-
case '..':
580-
memo.pop();
581-
break;
582-
default:
583-
memo.push(segment);
584-
}
583+
normalizedSegments = _.reduce(segments, function(memo, segment) {
584+
switch(segment) {
585+
case '.':
586+
break;
587+
case '..':
588+
memo.pop();
589+
break;
590+
default:
591+
memo.push(segment);
592+
}
585593

586-
return memo;
587-
}, []);
588-
return normalizedSegments.join('/');
589-
};
594+
return memo;
595+
}, []);
596+
return normalizedSegments.join('/');
597+
},
590598
baseName = function(path) {
591599
var segments = path.split('/');
592600

593601
segments.pop();
594602
return segments.join('/');
595603
};
596-
isRelative = function(path) {
597-
var segments = path.split('/');
598-
599-
if(segments.length === 1) {
600-
return false;
601-
}
602-
return (segments[0] === '.' || segments[0] === '..');
603-
};
604-
if(!isRelative(dep)) {
605-
return dep;
606-
}
607-
return normalizePath([baseName(moduleId), dep].join('/'));
604+
return normalizePath([baseName(moduleId), dep].join('/'));
608605
},
609606
// convertToFunctionExpression
610607
// ---------------------------
611608
// Returns either an IIFE or variable declaration.
612-
// Internally calls either convertToIIFE() or convertToIIFEDeclaration().
609+
// Internally calls either convertToIIFE() or convertToIIFEDeclaration()
613610
'convertToFunctionExpression': function(obj) {
614611
var isDefine = obj.isDefine,
615612
isRequire = obj.isRequire,
@@ -625,7 +622,7 @@
625622
iterator = -1,
626623
currentName;
627624
while(++iterator < depLength) {
628-
currentName = publicAPI.normalizeDepId(moduleId, dependencies[iterator]);
625+
currentName = publicAPI.normalizeDependencyName(moduleId, dependencies[iterator]);
629626
if(options.globalObject === true && options.globalObjectName && currentName !== '{}') {
630627
deps.push({
631628
'type': 'MemberExpression',
@@ -1019,6 +1016,7 @@
10191016
if(ast && _.isArray(ast.body)) {
10201017
estraverse.replace(ast, {
10211018
enter: function(node, parent) {
1019+
var normalizedModuleName;
10221020
if(node === undefined || node.type === 'EmptyStatement') {
10231021
_.each(parent.body, function(currentNode, iterator) {
10241022
if(currentNode === undefined || currentNode.type === 'EmptyStatement') {
@@ -1027,10 +1025,27 @@
10271025
});
10281026
} else if(publicAPI.isRequireExpression(node)) {
10291027
if(node['arguments'] && node['arguments'][0] && node['arguments'][0].value) {
1030-
return {
1031-
'type': 'Identifier',
1032-
'name': publicAPI.normalizeModuleName(node['arguments'][0].value)
1033-
};
1028+
normalizedModuleName = publicAPI.normalizeModuleName(node['arguments'][0].value);
1029+
if(options.globalObject === true && (options.globalObjectName && _.isString(options.globalObjectName) && options.globalObjectName.length)) {
1030+
return {
1031+
'type': 'MemberExpression',
1032+
'computed': true,
1033+
'object': {
1034+
'type': 'Identifier',
1035+
'name': options.globalObjectName
1036+
},
1037+
'property': {
1038+
'type': 'Literal',
1039+
'value': normalizedModuleName,
1040+
'raw': normalizedModuleName
1041+
}
1042+
};
1043+
} else {
1044+
return {
1045+
'type': 'Identifier',
1046+
'name': normalizedModuleName
1047+
};
1048+
}
10341049
} else {
10351050
return node;
10361051
}

test/specs/convert.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,30 @@ describe('amdclean specs', function() {
3737
expect(cleanedCode).toBe(standardJavaScript);
3838
});
3939

40-
it('should correctly normalize relative file paths in deps', function() {
40+
it('should correctly normalize relative file paths dependencies', function() {
4141
var AMDcode = "define('./modules/example', ['./example1', './example2', '../example3'], function(one, two, three) {var test = true;});",
4242
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
4343
standardJavaScript = "var modules_example=function (one,two,three){var test=true;}(modules_example1,modules_example2,example3);";
4444

4545
expect(cleanedCode).toBe(standardJavaScript);
4646
});
4747

48+
it('should correctly normalize relative file paths dependencies with the globalObject option', function() {
49+
var AMDcode = "define('./modules/example', ['./example1', './example2', '../example3'], function(one, two, three) {var test = true;});",
50+
cleanedCode = amdclean.clean({ globalObject: true, rememberGlobalObject: false, code: AMDcode, escodegen: { format: { compact: true } } }),
51+
standardJavaScript = "var amdclean={};amdclean['modules_example']=function (one,two,three){var test=true;}(amdclean['modules_example1'],amdclean['modules_example2'],amdclean['example3']);";
52+
53+
expect(cleanedCode).toBe(standardJavaScript);
54+
});
55+
56+
it('should correctly normalize multi-level relative file paths dependencies', function() {
57+
var AMDcode = "define('./foo/prototype/subModule/myModule', ['example1','example2', '/anotherModule/example3', '../../example4','../anotherModule/example5'], function(one, two, three, four, five) { var test = true;});",
58+
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
59+
standardJavaScript = "var foo_prototype_subModule_myModule=function (one,two,three,four,five){var test=true;}(example1,example2,anotherModule_example3,foo_example4,foo_prototype_anotherModule_example5);";
60+
61+
expect(cleanedCode).toBe(standardJavaScript);
62+
});
63+
4864
it('should correctly normalize multi-level relative file paths', function() {
4965
var AMDcode = "define('./foo/prototype/commonMethodName.js', ['example1', 'example2'], function(one, two) { var test = true;});",
5066
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
@@ -127,8 +143,8 @@ describe('amdclean specs', function() {
127143

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

133149
expect(cleanedCode).toBe(standardJavaScript);
134150
});
@@ -345,8 +361,8 @@ describe('amdclean specs', function() {
345361

346362
it('should convert object return values to a global object', function() {
347363
var AMDcode = "define('third', { exampleProp: 'This is an example' });",
348-
cleanedCode = amdclean.clean({ globalObject: true, code: AMDcode, escodegen: { format: { compact: true } } }),
349-
standardJavaScript = "amdclean['third']={exampleProp:'This is an example'};";
364+
cleanedCode = amdclean.clean({ globalObject: true, rememberGlobalObject: false, code: AMDcode, escodegen: { format: { compact: true } } }),
365+
standardJavaScript = "var amdclean={};amdclean['third']={exampleProp:'This is an example'};";
350366

351367
expect(cleanedCode).toBe(standardJavaScript);
352368
});
@@ -363,6 +379,14 @@ describe('amdclean specs', function() {
363379
expect(cleanedCode).toBe(standardJavaScript);
364380
});
365381

382+
it('should convert CommonJS require() calls correctly with the globalObject option', function() {
383+
var AMDcode = "var example = require('anotherModule');",
384+
cleanedCode = amdclean.clean({ code: AMDcode, globalObject: true, rememberGlobalObject: false, escodegen: { format: { compact: true } } }),
385+
standardJavaScript = "var amdclean={};var example=amdclean['anotherModule'];";
386+
387+
expect(cleanedCode).toBe(standardJavaScript);
388+
});
389+
366390
it('should convert CommonJS require() calls with file paths', function() {
367391
var AMDcode = "var example = require('./anotherModule');",
368392
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),

0 commit comments

Comments
 (0)