Skip to content

Commit 3a3a41b

Browse files
committed
Releasing AMDclean 2.3.0
1 parent fd3bb45 commit 3a3a41b

12 files changed

+397
-29
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,10 @@ amdclean.clean({
559559
'start': ';(function() {\n',
560560
// This string is appended to the file
561561
'end': '\n}());'
562-
}
562+
},
563+
// Configuration info for modules
564+
// Note: Further info can be found here - http://requirejs.org/docs/api.html#config-moduleconfig
565+
'config': {}
563566
})
564567
```
565568

@@ -723,6 +726,10 @@ __I don't like the way AMDclean normalizes the names of my modules with undersco
723726

724727
- You sure can. You can either use the `prefixMode` and change it to camelCase, or you can override all of the logic with your own logic by using the `prefixTransform` option hook.
725728

729+
__Require.js supports passing module information, to one or more modules, with the `config` option. Does AMDclean support this?__
730+
731+
- Yes! Make sure to set the AMDclean `config` option with whatever module information you would like available to you in your modules. Check the Require.js website for more details: http://requirejs.org/docs/api.html#config-moduleconfig
732+
726733

727734
__I can't seem to get AMDclean 2.0 to work. What gives?__
728735

build/amdclean.js

+99-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! amdclean - v2.2.8 - 2014-10-02
1+
/*! amdclean - v2.3.0 - 2014-10-08
22
* http://gregfranko.com/amdclean
33
* Copyright (c) 2014 Greg Franko */
44

@@ -96,7 +96,10 @@ _defaultOptions_ = {
9696
'end': '\n}());'
9797
},
9898
// Determines if certain aggressive file size optimization techniques will be used to transform the soure code
99-
'aggressiveOptimizations': false
99+
'aggressiveOptimizations': false,
100+
// Configuration info for modules
101+
// Note: Further info can be found here - http://requirejs.org/docs/api.html#config-moduleconfig
102+
'config': {}
100103
};
101104
// errorMsgs.js
102105
// ============
@@ -379,7 +382,7 @@ convertToIIFE = function convertToIIFE(obj) {
379382
// Returns a function expression that is executed immediately
380383
// e.g. var example = function(){}()
381384
convertToIIFEDeclaration = function convertToIIFEDeclaration(obj) {
382-
var moduleName = obj.moduleName, callbackFuncParams = obj.callbackFuncParams, isOptimized = obj.isOptimized, callback = obj.callbackFunc, node = obj.node, name = callback.name, type = callback.type, range = node.range || defaultValues.defaultRange, loc = node.loc || defaultValues.defaultLOC, callbackFunc = function () {
385+
var amdclean = this, options = amdclean.options, moduleId = obj.moduleId, moduleName = obj.moduleName, hasModuleParam = obj.hasModuleParam, hasExportsParam = obj.hasExportsParam, callbackFuncParams = obj.callbackFuncParams, isOptimized = obj.isOptimized, callback = obj.callbackFunc, node = obj.node, name = callback.name, type = callback.type, range = node.range || defaultValues.defaultRange, loc = node.loc || defaultValues.defaultLOC, callbackFunc = function () {
383386
var cbFunc = obj.callbackFunc;
384387
if (type === 'Identifier' && name !== 'undefined') {
385388
cbFunc = {
@@ -454,7 +457,40 @@ convertToIIFEDeclaration = function convertToIIFEDeclaration(obj) {
454457
};
455458
}
456459
return cbFunc;
457-
}(), dependencyNames = obj.dependencyNames, cb = function () {
460+
}(), dependencyNames = function () {
461+
var depNames = obj.dependencyNames, objExpression = {
462+
'type': 'ObjectExpression',
463+
'properties': [],
464+
'range': range,
465+
'loc': loc
466+
}, configMemberExpression = {
467+
'type': 'MemberExpression',
468+
'computed': false,
469+
'object': {
470+
'type': 'Identifier',
471+
'name': 'module'
472+
},
473+
'property': {
474+
'type': 'Identifier',
475+
'name': moduleId
476+
}
477+
}, moduleDepIndex;
478+
if (options.config && options.config[moduleId]) {
479+
if (hasExportsParam && hasModuleParam) {
480+
return [
481+
objExpression,
482+
objExpression,
483+
configMemberExpression
484+
];
485+
} else if (hasModuleParam) {
486+
moduleDepIndex = _.findIndex(depNames, function (currentDep) {
487+
return currentDep.name === '{}';
488+
});
489+
depNames[moduleDepIndex] = configMemberExpression;
490+
}
491+
}
492+
return depNames;
493+
}(), cb = function () {
458494
if (callbackFunc.type === 'Literal' || callbackFunc.type === 'Identifier' && callbackFunc.name === 'undefined' || isOptimized === true) {
459495
return callbackFunc;
460496
} else {
@@ -641,6 +677,9 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
641677
}(), originalCallbackFuncParams, hasExportsParam = function () {
642678
var cbParams = callbackFunc.params || [];
643679
return _.where(cbParams, { 'name': 'exports' }).length;
680+
}(), hasModuleParam = function () {
681+
var cbParams = callbackFunc.params || [];
682+
return _.where(cbParams, { 'name': 'module' }).length;
644683
}(), normalizeDependencyNames = {}, dependencyNames = function () {
645684
var deps = [], currentName;
646685
_.each(dependencies, function (currentDependency) {
@@ -827,9 +866,11 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
827866
});
828867
if (isDefine) {
829868
return convertToIIFEDeclaration.call(amdclean, {
869+
'moduleId': moduleId,
830870
'moduleName': moduleName,
831871
'dependencyNames': dependencyNames,
832872
'callbackFuncParams': callbackFuncParams,
873+
'hasModuleParam': hasModuleParam,
833874
'hasExportsParam': hasExportsParam,
834875
'callbackFunc': callbackFunc,
835876
'isOptimized': isOptimized,
@@ -1237,7 +1278,7 @@ generateCode = function generateCode(ast) {
12371278
// ========
12381279
// Removes any AMD and/or CommonJS trace from the provided source code
12391280
clean = function clean() {
1240-
var amdclean = this, options = amdclean.options, ignoreModules = options.ignoreModules, originalAst = {}, ast = {}, generatedCode, declarations = [], hoistedVariables = {}, hoistedCallbackParameters = {}, defaultRange = defaultValues.defaultRange, defaultLOC = defaultValues.defaultLOC;
1281+
var amdclean = this, options = amdclean.options, ignoreModules = options.ignoreModules, originalAst = {}, ast = {}, configAst = {}, generatedCode, declarations = [], hoistedVariables = {}, hoistedCallbackParameters = {}, defaultRange = defaultValues.defaultRange, defaultLOC = defaultValues.defaultLOC;
12411282
// Creates and stores an AST representation of the code
12421283
originalAst = createAst.call(amdclean);
12431284
// Loops through the AST, finds all module ids, and stores them in the current instance storedModules property
@@ -1422,6 +1463,58 @@ clean = function clean() {
14221463
});
14231464
}
14241465
});
1466+
// Adds a local module variable if a user wants local module information available to them
1467+
if (_.isObject(options.config) && !_.isEmpty(options.config)) {
1468+
configAst = function () {
1469+
var props = [];
1470+
_.each(options.config, function (val, key) {
1471+
var currentModuleConfig = options.config[key];
1472+
props.push({
1473+
'type': 'Property',
1474+
'key': {
1475+
'type': 'Literal',
1476+
'value': key
1477+
},
1478+
'value': {
1479+
'type': 'ObjectExpression',
1480+
'properties': [{
1481+
'type': 'Property',
1482+
'key': {
1483+
'type': 'Literal',
1484+
'value': 'config'
1485+
},
1486+
'value': {
1487+
'type': 'FunctionExpression',
1488+
'id': null,
1489+
'params': [],
1490+
'defaults': [],
1491+
'body': {
1492+
'type': 'BlockStatement',
1493+
'body': [{
1494+
'type': 'ReturnStatement',
1495+
'argument': createAst.call(amdclean, 'var x =' + JSON.stringify(currentModuleConfig)).body[0].declarations[0].init
1496+
}]
1497+
}
1498+
},
1499+
'kind': 'init'
1500+
}]
1501+
}
1502+
});
1503+
});
1504+
return {
1505+
'type': 'VariableDeclarator',
1506+
'id': {
1507+
'type': 'Identifier',
1508+
'name': 'module'
1509+
},
1510+
'init': {
1511+
'type': 'ObjectExpression',
1512+
'properties': props
1513+
}
1514+
};
1515+
}();
1516+
declarations.push(configAst);
1517+
}
14251518
// If there are declarations, the declarations are preprended to the beginning of the code block
14261519
if (declarations.length) {
14271520
ast.body.unshift({
@@ -1570,7 +1663,7 @@ clean = function clean() {
15701663
// The object that is publicly accessible
15711664
publicAPI = {
15721665
// Current project version number
1573-
'VERSION': '2.2.8',
1666+
'VERSION': '2.3.0',
15741667
'clean': function (options, overloadedOptions) {
15751668
// Creates a new AMDclean instance
15761669
var amdclean = new AMDclean(options, overloadedOptions), cleanedCode = amdclean.clean();

build/amdclean.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ var gulp = require('gulp'),
3838
headerText = '/*! amdclean - v' + packageJson.version + ' - ' + currentDate +
3939
'\n* http://gregfranko.com/amdclean' +
4040
'\n* Copyright (c) ' + currentYear + ' Greg Franko */\n',
41-
error = false;
41+
error = false,
42+
cachedBuiltLibText = fs.readFileSync('./src/amdclean.js', 'utf8');
43+
revertFile = function() {
44+
fs.writeFileSync('./src/amdclean.js', cachedBuiltLibText);
45+
};
4246

4347
gulp.task('build', function(cb) {
4448
requirejs.optimize({
@@ -69,12 +73,14 @@ gulp.task('build', function(cb) {
6973
});
7074
} catch (e) {
7175
error = true;
76+
revertFile();
7277
return '' + e;
7378
}
7479
}()),
7580
fullCode = headerText + licenseText + cleanedCode;
7681

7782
if (error) {
83+
revertFile();
7884
console.log('Looks like there was an error building, stopping the build... ' + cleanedCode);
7985
return;
8086
}
@@ -85,6 +91,7 @@ gulp.task('build', function(cb) {
8591
cb();
8692
}
8793
}, function(err) {
94+
revertFile();
8895
console.log('Looks like there was an error building, stopping the build... ');
8996
return cb(err); // return error
9097
});

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "amdclean",
3-
"version": "2.2.8",
3+
"version": "2.3.0",
44
"description": "A build tool that converts AMD code to standard JavaScript",
55
"main": "./src/amdclean",
66
"repository": {
@@ -41,4 +41,4 @@
4141
"node": ">= 0.8"
4242
},
4343
"license": "MIT"
44-
}
44+
}

0 commit comments

Comments
 (0)