Skip to content

Commit

Permalink
Re: #35, #32 - Releasing AMDClean v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gfranko committed Mar 4, 2014
1 parent 769b263 commit 6004e6a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 50 deletions.
2 changes: 1 addition & 1 deletion build/amdclean.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amdclean",
"version": "1.2.1",
"version": "1.3.0",
"description": "A build tool that converts AMD code to standard JavaScript",
"main": "./src/amdclean",
"repository": {
Expand Down
101 changes: 58 additions & 43 deletions src/amdclean.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! amdclean - v1.2.1 - 2014-02-17
/*! amdclean - v1.3.0 - 2014-03-03
* http://gregfranko.com/amdclean
* Copyright (c) 2014 Greg Franko; Licensed MIT*/

Expand Down Expand Up @@ -74,7 +74,7 @@
// The Public API object
publicAPI = {
// Current project version number
'VERSION': '1.2.1',
'VERSION': '1.3.0',
// Default Options
'defaultOptions': {
// The source code you would like to be 'cleaned'
Expand Down Expand Up @@ -559,57 +559,54 @@
}());
return updatedNode;
},
'normalizeDepId': function(moduleId, dep) {
if(!moduleId || !dep) {
// isRelativeFilePath
// ------------------
// Returns a boolean that determines if the file path provided is a relative file path
// e.g. ../exampleModule -> true
isRelativeFilePath: function(path) {
var segments = path.split('/');

return segments.length !== -1 && (segments[0] === '.' || segments[0] === '..');
},
// normalizeDependencyName
// -----------------------
// Returns a normalized dependency name that handles relative file paths
'normalizeDependencyName': function(moduleId, dep) {
if(!moduleId || !dep || !publicAPI.isRelativeFilePath(dep)) {
return dep;
}

var normalizePath,
baseName,
isRelative;
var normalizePath = function(path) {
var segments = path.split('/'),
normalizedSegments;

normalizePath = function(path) {
var segments = path.split('/'),
normalizedSegments;

normalizedSegments = _.reduce(segments, function(memo, segment) {
switch(segment) {
case '.':
break;
case '..':
memo.pop();
break;
default:
memo.push(segment);
}
normalizedSegments = _.reduce(segments, function(memo, segment) {
switch(segment) {
case '.':
break;
case '..':
memo.pop();
break;
default:
memo.push(segment);
}

return memo;
}, []);
return normalizedSegments.join('/');
};
return memo;
}, []);
return normalizedSegments.join('/');
},
baseName = function(path) {
var segments = path.split('/');

segments.pop();
return segments.join('/');
};
isRelative = function(path) {
var segments = path.split('/');

if(segments.length === 1) {
return false;
}
return (segments[0] === '.' || segments[0] === '..');
};
if(!isRelative(dep)) {
return dep;
}
return normalizePath([baseName(moduleId), dep].join('/'));
return normalizePath([baseName(moduleId), dep].join('/'));
},
// convertToFunctionExpression
// ---------------------------
// Returns either an IIFE or variable declaration.
// Internally calls either convertToIIFE() or convertToIIFEDeclaration().
// Internally calls either convertToIIFE() or convertToIIFEDeclaration()
'convertToFunctionExpression': function(obj) {
var isDefine = obj.isDefine,
isRequire = obj.isRequire,
Expand All @@ -625,7 +622,7 @@
iterator = -1,
currentName;
while(++iterator < depLength) {
currentName = publicAPI.normalizeDepId(moduleId, dependencies[iterator]);
currentName = publicAPI.normalizeDependencyName(moduleId, dependencies[iterator]);
if(options.globalObject === true && options.globalObjectName && currentName !== '{}') {
deps.push({
'type': 'MemberExpression',
Expand Down Expand Up @@ -1019,6 +1016,7 @@
if(ast && _.isArray(ast.body)) {
estraverse.replace(ast, {
enter: function(node, parent) {
var normalizedModuleName;
if(node === undefined || node.type === 'EmptyStatement') {
_.each(parent.body, function(currentNode, iterator) {
if(currentNode === undefined || currentNode.type === 'EmptyStatement') {
Expand All @@ -1027,10 +1025,27 @@
});
} else if(publicAPI.isRequireExpression(node)) {
if(node['arguments'] && node['arguments'][0] && node['arguments'][0].value) {
return {
'type': 'Identifier',
'name': publicAPI.normalizeModuleName(node['arguments'][0].value)
};
normalizedModuleName = publicAPI.normalizeModuleName(node['arguments'][0].value);
if(options.globalObject === true && (options.globalObjectName && _.isString(options.globalObjectName) && options.globalObjectName.length)) {
return {
'type': 'MemberExpression',
'computed': true,
'object': {
'type': 'Identifier',
'name': options.globalObjectName
},
'property': {
'type': 'Literal',
'value': normalizedModuleName,
'raw': normalizedModuleName
}
};
} else {
return {
'type': 'Identifier',
'name': normalizedModuleName
};
}
} else {
return node;
}
Expand Down
34 changes: 29 additions & 5 deletions test/specs/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,30 @@ describe('amdclean specs', function() {
expect(cleanedCode).toBe(standardJavaScript);
});

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

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

it('should correctly normalize relative file paths dependencies with the globalObject option', function() {
var AMDcode = "define('./modules/example', ['./example1', './example2', '../example3'], function(one, two, three) {var test = true;});",
cleanedCode = amdclean.clean({ globalObject: true, rememberGlobalObject: false, code: AMDcode, escodegen: { format: { compact: true } } }),
standardJavaScript = "var amdclean={};amdclean['modules_example']=function (one,two,three){var test=true;}(amdclean['modules_example1'],amdclean['modules_example2'],amdclean['example3']);";

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

it('should correctly normalize multi-level relative file paths dependencies', function() {
var AMDcode = "define('./foo/prototype/subModule/myModule', ['example1','example2', '/anotherModule/example3', '../../example4','../anotherModule/example5'], function(one, two, three, four, five) { var test = true;});",
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
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);";

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

it('should correctly normalize multi-level relative file paths', function() {
var AMDcode = "define('./foo/prototype/commonMethodName.js', ['example1', 'example2'], function(one, two) { var test = true;});",
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
Expand Down Expand Up @@ -127,8 +143,8 @@ describe('amdclean specs', function() {

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

expect(cleanedCode).toBe(standardJavaScript);
});
Expand Down Expand Up @@ -345,8 +361,8 @@ describe('amdclean specs', function() {

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

expect(cleanedCode).toBe(standardJavaScript);
});
Expand All @@ -363,6 +379,14 @@ describe('amdclean specs', function() {
expect(cleanedCode).toBe(standardJavaScript);
});

it('should convert CommonJS require() calls correctly with the globalObject option', function() {
var AMDcode = "var example = require('anotherModule');",
cleanedCode = amdclean.clean({ code: AMDcode, globalObject: true, rememberGlobalObject: false, escodegen: { format: { compact: true } } }),
standardJavaScript = "var amdclean={};var example=amdclean['anotherModule'];";

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

it('should convert CommonJS require() calls with file paths', function() {
var AMDcode = "var example = require('./anotherModule');",
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
Expand Down

0 comments on commit 6004e6a

Please sign in to comment.