Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
babel-plugin-transform-amd-to-commonjs
Advanced tools
Babel plugin that transforms AMD to CommonJS.
npm install --save-dev babel-plugin-transform-amd-to-commonjs
Add the transform to your .babelrc:
{
"plugins": ["transform-amd-to-commonjs"]
}
Input (define):
define(['jquery', 'underscore', 'myModule'], function($, _) {
var $divs = $('div');
return {
divs: _.filter($divs, function(div) {
return div.hasChildNodes();
});
};
});
Output (define):
module.exports = function() {
var $ = require('jquery');
var _ = require('underscore');
require('myModule');
return {
divs: _.filter($divs, function(div) {
return div.hasChildNodes();
});
};
}();
Input (require):
require(['jquery', 'underscore', 'myModule'], function($, _) {
$(document).append($('<div>').text(_.random(10)));
require(['anotherModule'], function(anotherModule) {
anotherModule.doSomeStuff(_.random(10));
});
});
Output (require):
(function() {
var $ = require('jquery');
var _ = require('underscore');
require('myModule');
$(document).append($('<div>').text(_.random(10)));
(function() {
var anotherModule = require('anotherModule');
anotherModule.doSomeStuff(_.random(10));
})();
})();
AMD is interpreted as described and implemented by RequireJS.
define
function will be transformed.require
where it is given an array of dependencies as its first argument will be transformed.
require
, module
, and exports
in an AMD module will not generate a call to require, but instead defer to the global require, module, and exports assumed to be in the CommonJS environment you are transforming to.
define
) is ignored, since the module ID in CommonJS is determined by the resolved filename.If you would like support for lower versions of node (4+), please submit an issue.
Make sure that you have only one AMD module defined per file, otherwise you'll experience strange results once transformed to the CommonJS format.
The following will not be transformed, since the plugin does not traverse the arguments to define or require:
var deps = ['one', 'two'];
var factory = function(one, two) {
one.doStuff();
return two.doStuff();
};
define(deps, factory);
If you want to be able to define your modules as above, please submit an issue. Otherwise, please define your modules as:
define(['one', 'two'], function(one, two) {
one.doStuff();
return two.doStuff();
});
require
, module
, or exports
as dependenciesIt is strongly advised to simply use return statements to define your AMD module's exports. That being said, the plugin takes into account the cases where you may have injected them as dependencies. Beware of the following gotchas when using this pattern:
If you're injecting module
, exports
, and/or require
as dependencies, they must be injected as string literals,
otherwise you'll end up with things like require('module')
.
If you inject module
or exports
as a dependency in your AMD module, the plugin assumes that you are using them to set the exports of your module.
Therefore, a return value of the IIFE that wraps your module will not be assigned to module.exports
,
That means if you're using AMD in a funky way, some strange things can happen.
For example, you can require exports
, set some values on it, but then override them with a return value (you really shouldn't do this though):
define(['exports'], function(exports) {
exports.stuff = 'hi';
return {
override: 'lol no stuff for you';
};
});
// exported: { override: 'lol no stuff for you' };
This transforms to the following IIFE (with the return value not assigned to module.exports
because you've injected exports
as a dependency):
(function() {
exports.stuff = 'hi';
return {
override: 'lol no stuff for you';
};
})();
// exported: { stuff: 'hi' }
In order to account for this possible error, it would have to be transformed to something disgusting like this instead:
(function() {
var returnValue = (function() {
exports.stuff = 'hi';
return {
override: 'lol no stuff for you';
};
})();
if(typeof returnValue !== 'undefined') {
module.exports = returnValue;
}
})();
Doing this transform for this specific case introduces relatively significant complexity into the code for almost no gain. If you want this edge case to be accounted for, please submit an issue.
FAQs
Transforms AMD code to CommonJS
The npm package babel-plugin-transform-amd-to-commonjs receives a total of 10,692 weekly downloads. As such, babel-plugin-transform-amd-to-commonjs popularity was classified as popular.
We found that babel-plugin-transform-amd-to-commonjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.