Skip to content

Commit

Permalink
Releasing AMDclean 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gfranko committed Oct 8, 2014
1 parent fd3bb45 commit 3a3a41b
Show file tree
Hide file tree
Showing 12 changed files with 397 additions and 29 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,10 @@ amdclean.clean({
'start': ';(function() {\n',
// This string is appended to the file
'end': '\n}());'
}
},
// Configuration info for modules
// Note: Further info can be found here - http://requirejs.org/docs/api.html#config-moduleconfig
'config': {}
})
```

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

- 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.

__Require.js supports passing module information, to one or more modules, with the `config` option. Does AMDclean support this?__

- 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


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

Expand Down
105 changes: 99 additions & 6 deletions build/amdclean.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! amdclean - v2.2.8 - 2014-10-02
/*! amdclean - v2.3.0 - 2014-10-08
* http://gregfranko.com/amdclean
* Copyright (c) 2014 Greg Franko */

Expand Down Expand Up @@ -96,7 +96,10 @@ _defaultOptions_ = {
'end': '\n}());'
},
// Determines if certain aggressive file size optimization techniques will be used to transform the soure code
'aggressiveOptimizations': false
'aggressiveOptimizations': false,
// Configuration info for modules
// Note: Further info can be found here - http://requirejs.org/docs/api.html#config-moduleconfig
'config': {}
};
// errorMsgs.js
// ============
Expand Down Expand Up @@ -379,7 +382,7 @@ convertToIIFE = function convertToIIFE(obj) {
// Returns a function expression that is executed immediately
// e.g. var example = function(){}()
convertToIIFEDeclaration = function convertToIIFEDeclaration(obj) {
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 () {
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 () {
var cbFunc = obj.callbackFunc;
if (type === 'Identifier' && name !== 'undefined') {
cbFunc = {
Expand Down Expand Up @@ -454,7 +457,40 @@ convertToIIFEDeclaration = function convertToIIFEDeclaration(obj) {
};
}
return cbFunc;
}(), dependencyNames = obj.dependencyNames, cb = function () {
}(), dependencyNames = function () {
var depNames = obj.dependencyNames, objExpression = {
'type': 'ObjectExpression',
'properties': [],
'range': range,
'loc': loc
}, configMemberExpression = {
'type': 'MemberExpression',
'computed': false,
'object': {
'type': 'Identifier',
'name': 'module'
},
'property': {
'type': 'Identifier',
'name': moduleId
}
}, moduleDepIndex;
if (options.config && options.config[moduleId]) {
if (hasExportsParam && hasModuleParam) {
return [
objExpression,
objExpression,
configMemberExpression
];
} else if (hasModuleParam) {
moduleDepIndex = _.findIndex(depNames, function (currentDep) {
return currentDep.name === '{}';
});
depNames[moduleDepIndex] = configMemberExpression;
}
}
return depNames;
}(), cb = function () {
if (callbackFunc.type === 'Literal' || callbackFunc.type === 'Identifier' && callbackFunc.name === 'undefined' || isOptimized === true) {
return callbackFunc;
} else {
Expand Down Expand Up @@ -641,6 +677,9 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
}(), originalCallbackFuncParams, hasExportsParam = function () {
var cbParams = callbackFunc.params || [];
return _.where(cbParams, { 'name': 'exports' }).length;
}(), hasModuleParam = function () {
var cbParams = callbackFunc.params || [];
return _.where(cbParams, { 'name': 'module' }).length;
}(), normalizeDependencyNames = {}, dependencyNames = function () {
var deps = [], currentName;
_.each(dependencies, function (currentDependency) {
Expand Down Expand Up @@ -827,9 +866,11 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
});
if (isDefine) {
return convertToIIFEDeclaration.call(amdclean, {
'moduleId': moduleId,
'moduleName': moduleName,
'dependencyNames': dependencyNames,
'callbackFuncParams': callbackFuncParams,
'hasModuleParam': hasModuleParam,
'hasExportsParam': hasExportsParam,
'callbackFunc': callbackFunc,
'isOptimized': isOptimized,
Expand Down Expand Up @@ -1237,7 +1278,7 @@ generateCode = function generateCode(ast) {
// ========
// Removes any AMD and/or CommonJS trace from the provided source code
clean = function clean() {
var amdclean = this, options = amdclean.options, ignoreModules = options.ignoreModules, originalAst = {}, ast = {}, generatedCode, declarations = [], hoistedVariables = {}, hoistedCallbackParameters = {}, defaultRange = defaultValues.defaultRange, defaultLOC = defaultValues.defaultLOC;
var amdclean = this, options = amdclean.options, ignoreModules = options.ignoreModules, originalAst = {}, ast = {}, configAst = {}, generatedCode, declarations = [], hoistedVariables = {}, hoistedCallbackParameters = {}, defaultRange = defaultValues.defaultRange, defaultLOC = defaultValues.defaultLOC;
// Creates and stores an AST representation of the code
originalAst = createAst.call(amdclean);
// Loops through the AST, finds all module ids, and stores them in the current instance storedModules property
Expand Down Expand Up @@ -1422,6 +1463,58 @@ clean = function clean() {
});
}
});
// Adds a local module variable if a user wants local module information available to them
if (_.isObject(options.config) && !_.isEmpty(options.config)) {
configAst = function () {
var props = [];
_.each(options.config, function (val, key) {
var currentModuleConfig = options.config[key];
props.push({
'type': 'Property',
'key': {
'type': 'Literal',
'value': key
},
'value': {
'type': 'ObjectExpression',
'properties': [{
'type': 'Property',
'key': {
'type': 'Literal',
'value': 'config'
},
'value': {
'type': 'FunctionExpression',
'id': null,
'params': [],
'defaults': [],
'body': {
'type': 'BlockStatement',
'body': [{
'type': 'ReturnStatement',
'argument': createAst.call(amdclean, 'var x =' + JSON.stringify(currentModuleConfig)).body[0].declarations[0].init
}]
}
},
'kind': 'init'
}]
}
});
});
return {
'type': 'VariableDeclarator',
'id': {
'type': 'Identifier',
'name': 'module'
},
'init': {
'type': 'ObjectExpression',
'properties': props
}
};
}();
declarations.push(configAst);
}
// If there are declarations, the declarations are preprended to the beginning of the code block
if (declarations.length) {
ast.body.unshift({
Expand Down Expand Up @@ -1570,7 +1663,7 @@ clean = function clean() {
// The object that is publicly accessible
publicAPI = {
// Current project version number
'VERSION': '2.2.8',
'VERSION': '2.3.0',
'clean': function (options, overloadedOptions) {
// Creates a new AMDclean instance
var amdclean = new AMDclean(options, overloadedOptions), cleanedCode = amdclean.clean();
Expand Down
4 changes: 2 additions & 2 deletions build/amdclean.min.js

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ var gulp = require('gulp'),
headerText = '/*! amdclean - v' + packageJson.version + ' - ' + currentDate +
'\n* http://gregfranko.com/amdclean' +
'\n* Copyright (c) ' + currentYear + ' Greg Franko */\n',
error = false;
error = false,
cachedBuiltLibText = fs.readFileSync('./src/amdclean.js', 'utf8');
revertFile = function() {
fs.writeFileSync('./src/amdclean.js', cachedBuiltLibText);
};

gulp.task('build', function(cb) {
requirejs.optimize({
Expand Down Expand Up @@ -69,12 +73,14 @@ gulp.task('build', function(cb) {
});
} catch (e) {
error = true;
revertFile();
return '' + e;
}
}()),
fullCode = headerText + licenseText + cleanedCode;

if (error) {
revertFile();
console.log('Looks like there was an error building, stopping the build... ' + cleanedCode);
return;
}
Expand All @@ -85,6 +91,7 @@ gulp.task('build', function(cb) {
cb();
}
}, function(err) {
revertFile();
console.log('Looks like there was an error building, stopping the build... ');
return cb(err); // return error
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amdclean",
"version": "2.2.8",
"version": "2.3.0",
"description": "A build tool that converts AMD code to standard JavaScript",
"main": "./src/amdclean",
"repository": {
Expand Down Expand Up @@ -41,4 +41,4 @@
"node": ">= 0.8"
},
"license": "MIT"
}
}
Loading

0 comments on commit 3a3a41b

Please sign in to comment.