Releases: gfranko/amdclean
New config option, updated prefixTransform, and fixed Regression Bug
- Added the
config
option #60- This allows you to pass module information, that you set in your configuration, to your modules. Reference: http://requirejs.org/docs/api.html#config-moduleconfig
Examples
onModuleBundleComplete: function (data) {
var fs = module.require('fs'),
amdclean = module.require('amdclean'),
outputFile = data.path,
cleanedCode = amdclean.clean({
'filePath': outputFile,
'config': {
'test': {
'size': 'large'
}
}
});
fs.writeFileSync(outputFile, cleanedCode);
}
//test.js, which uses simplified CJS wrapping:
//http://requirejs.org/docs/whyamd.html#sugar
define(function (require, exports, module) {
//Will be the value 'large'
var size = module.config().size;
});
//test.js which uses a dependency array,
//it asks for the special module ID, 'module':
//https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#wiki-magic
define(['module'], function (module) {
//Will be the value 'large'
var size = module.config().size;
});
- Updated the
prefixTransform
option to pass BOTH post normalized and pre normalized module names #59
'prefixTransform': function(postNormalizedModuleName, preNormalizedModuleName) {
return postNormalizedModuleName;
}
- Fixed a regression bug that did not correctly catch all conditional AMD statements #61
Bug Fix - #50
Fixed #50 - Make sure an uninitialized variable does not cause AMDclean to error.
Bug Fixes
Bug Fixes
- No longer removes black listed parameter names unless the module is CommonJS. This was required to get the Require.js text plugin to work, since the text plugin has a parameter named
module
, which was black listed (removed) by AMDclean - Fixed the optimization of dependencies when the dependencies are more than the number of callback parameters
Complete Rewrite, CommonJS Support, New Option, Multiple Enhancements, Bug Fixes
Complete Rewrite
- AMDclean has been completely rewritten using... AMDclean. Yes, that's right; AMDclean is it's own user. Check out the source for yourself.
CommonJS Support
- AMDclean now provides full-fledged CommonJS support by working in conjunction with the Require.js cjsTranslate option. #45
- The only caveat is that the Require.js filesystem lookup is still used (sorry
npm
andnode_modules
fanboys)
New Option
- The
createAnonymousAMDModule
has been added, to easily allow library authors to expose an anonymous AMD module instead of always a named AMD module. If you would like to use it, you need to make sure to set thecreateAnonymousAMDModule
option totrue
(it isfalse
by default).
Multiple Enhancements
- Make sure to not strip out callback function parameters unless the
advancedOptimizations
option is set totrue
(helps with debugging) #44 - Make sure to still optimize a module even if there is one dependency. The important thing to check for is what is inside the callback function. #43
- Make sure to not clean optional
define()
wrappers, that are inside of conditional AMD checks, when thetransformAMDChecks
option is set totrue
. Very important for library development. #46 - Change the default
wrap
AMDclean option to be:
'wrap': {
'start': ';(function() {',
'end': '}());'
}
Reason: Lot's of people are using the Require.js wrap
option and becoming confused why all modules are being declared in the global scope. #47
- Wrap a conditional ternary operator around the module return value of scripts. #48 D3 example:
if (true) {
d3 = function () {
return typeof d3 === 'function' ? d3() : d3;
}();
}
Bug Fixes
- Comments outside of your modules are no longer being removed. This has been an annoying issue.
- Optimized modules, that return a function, do not have their function arguments hoisted by accident
Optimizations
Upgrading to 2.0
- Please make sure you are using the
onModuleBundleComplete
Require.js hook and NOT theonBuildWrite
Require.js hook. TheonBuildWrite
hook has been deprecated for AMDclean versions>= 2.0
. - The
globalObject
andglobalObjectName
options have been removed, since all modules are now hoisted to the top of a file.
Optimizations
AMDclean now uses a few different strategies to decrease file size:
Remove Unused Dependencies
AMD
define('example', ['example1', 'example2'], function() {
var test = true;
});
Standard
// Since no callback parameters were provided in the AMD code,
// the 'example1' and 'example2' parameters were removed
var example;
example = function() {
var test = true;
}();
Remove Exact Matching Dependencies
AMD
define('example', ['example1', 'example2'], function(example1, anotherExample) {
var test = true;
});
Standard
// Since the `example1` callback function parameter exactly matched
// the name of the `example1 dependency, it's parameters were removed
var example;
example = function(anotherExample) {
var test = true;
}(example2);
Hoist Common Non-Matching Dependencies
- Note - For this behavior, you must set the
aggressiveOptimizations
option totrue
AMD
define('example', ['example1'], function(firstExample) {
var test = true;
});
define('anotherExample', ['example1'], function(firstExample) {
var test = true;
});
Standard
// Since the `firstExample` callback function parameter was used more
// than once between modules, it was hoisted up and reused
var example, firstExample;
firstExample = example1;
example = function() {
var test = true;
};
anotherExample = function() {
var test = true;
};
UMD Fixes, transformAMDChecks option, Minor Optimizations,
UMD Factory Function Support
This:
(function (factory) {
if (typeof exports === 'object') {
module.exports = factory(require('backbone'), require('underscore'));
} else if (typeof define === 'function' && define.amd) {
define(['backbone', 'underscore'], factory);
}
}(function (Backbone, _) {
Backbone.Validation = (function(_){/*plugin logic*/}(_));
return Backbone.Validation;
}));
Get's Converted To:
(function (factory) {
if (typeof exports === 'object') {
module.exports = factory(amdclean['backbone'], amdclean['underscore']);
} else if (true) {
amdclean['backbonevalidation'] = function (backbone, underscore) {
return factory(backbone, underscore);
}(amdclean['backbone'], amdclean['underscore']);
}
}(function (Backbone, _) {
Backbone.Validation = (function(_){/*plugin logic*/}(_));
return Backbone.Validation;
}));
New transformAMDChecks Option
If set to false
, no conditional AMD checks will be transformed. This is a very helpful option for library authors.
Minor Optimizations
Callback parameters are no longer specified unnecessarily.
Comments Are No Longer Being Stripped
Comments are no longer getting removed inside of module definitions.
AMD Code
// Comment above the module declaration
define('example', function () {
// Comment inside the module declaration
var example;
});
Standard JavaScript Code
// Comment above the module declaration
var example = function () {
// Comment inside the module declaration
var example;
}();
Relative File Path Dependencies and CommonJS Require Includes
Relative File Path Dependencies
You can now use relative file paths (as many levels deep as you like) for your AMD dependencies, and they will work as expected. Special thanks to rstone770 for contributing this.
Check out this example:
AMD
define('./modules/example', [
'./example1',
'./example2',
'../example3'
], function(one, two, three) {
var test = true;
});
Standard JavaScript
var modules_example=function (one,two,three){
var test=true;
}(modules_example1,modules_example2,example3);
CommonJS Require Includes with the Global Object Option
You can now use the CommonJS require()
syntax with the globalObject
option.
Check out this example:
AMD
var example = require('anotherModule');
Standard JavaScript
var amdclean={};
var example=amdclean['anotherModule'];
Fixed Escodegen AMD Web Bug
- Fixed Escodegen issue when used in an AMD Web environment