Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-amd-to-commonjs

Package Overview
Dependencies
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-amd-to-commonjs - npm Package Compare versions

Comparing version 0.2.2 to 1.0.0

build/constants.js

39

package.json
{
"name": "babel-plugin-transform-amd-to-commonjs",
"version": "0.2.2",
"version": "1.0.0",
"description": "Transforms AMD code to CommonJS",
"main": "build.js",
"main": "build/index.js",
"scripts": {
"test": "jest",
"lint": "eslint .",
"prepublish": "babel index.js --out-file build.js"
"build": "babel src --out-dir build",
"prepack": "npm run build"
},
"files": [
"build"
],
"engines": {
"node": ">=4"
"node": ">=6"
},

@@ -33,16 +37,21 @@ "repository": {

"babel-core": "^6.20.0",
"babel-jest": "^20.0.3",
"babel-preset-es2015": "^6.24.1",
"eslint": "^3.12.0",
"eslint-config-msrose": "^0.1.0",
"jest": "^20.0.4",
"regenerator-runtime": "^0.10.5"
"babel-jest": "^22.0.1",
"babel-preset-env": "^1.6.1",
"eslint": "^4.5.0",
"eslint-config-msrose": "^1.0.0",
"eslint-config-prettier": "^2.3.0",
"eslint-plugin-jest": "^21.0.0",
"eslint-plugin-prettier": "^2.2.0",
"jest": "^22.0.1",
"jest-diff": "^22.0.1",
"prettier": "^1.6.1",
"regenerator-runtime": "^0.11.0"
},
"jest": {
"testPathIgnorePatterns": [
"/node_modules/",
".eslintrc",
"<rootDir>/__tests__/custom-matchers.js"
]
"setupTestFrameworkScriptFile": "<rootDir>/tests/setup.js",
"collectCoverageFrom": [
"src/**/*.js"
],
"testEnvironment": "node"
}
}
# babel-plugin-transform-amd-to-commonjs
[![npm version](https://badge.fury.io/js/babel-plugin-transform-amd-to-commonjs.svg)](https://badge.fury.io/js/babel-plugin-transform-amd-to-commonjs)
[![npm version](https://img.shields.io/npm/v/babel-plugin-transform-amd-to-commonjs.svg)](https://www.npmjs.com/package/babel-plugin-transform-amd-to-commonjs)
[![npm downloads](https://img.shields.io/npm/dm/babel-plugin-transform-amd-to-commonjs.svg)](https://npm-stat.com/charts.html?package=babel-plugin-transform-amd-to-commonjs)
[![Build Status](https://travis-ci.org/msrose/babel-plugin-transform-amd-to-commonjs.svg?branch=master)](https://travis-ci.org/msrose/babel-plugin-transform-amd-to-commonjs)
[![codecov](https://codecov.io/gh/msrose/babel-plugin-transform-amd-to-commonjs/branch/master/graph/badge.svg)](https://codecov.io/gh/msrose/babel-plugin-transform-amd-to-commonjs)
[![devDependencies Status](https://david-dm.org/msrose/babel-plugin-transform-amd-to-commonjs/dev-status.svg)](https://david-dm.org/msrose/babel-plugin-transform-amd-to-commonjs?type=dev)
[![Greenkeeper badge](https://badges.greenkeeper.io/msrose/babel-plugin-transform-amd-to-commonjs.svg)](https://greenkeeper.io/)
Babel plugin that transforms AMD to CommonJS.
[Check out the example project](https://github.com/msrose/transform-amd-to-commonjs-example), which uses this plugin to allow [jest](https://facebook.github.io/jest/) to synchronously `require` AMD modules.
## Usage
[Check out the example project](https://github.com/msrose/transform-amd-to-commonjs-example) that has Jest tests synchronously `require`ing AMD modules.
```

@@ -25,11 +27,13 @@ npm install --save-dev babel-plugin-transform-amd-to-commonjs

Input (define):
## Examples
### Define
Input:
```javascript
define(['jquery', 'underscore', 'myModule'], function($, _) {
var $divs = $('div');
// ...
return {
divs: _.filter($divs, function(div) {
return div.hasChildNodes();
});
// ...
};

@@ -39,3 +43,3 @@ });

Output (define):
Output:

@@ -47,6 +51,5 @@ ```javascript

require('myModule');
// ...
return {
divs: _.filter($divs, function(div) {
return div.hasChildNodes();
});
// ...
};

@@ -56,9 +59,12 @@ }();

Input (require):
### Require
Input:
```javascript
// Nested requires
require(['jquery', 'underscore', 'myModule'], function($, _) {
$(document).append($('<div>').text(_.random(10)));
// ...
require(['anotherModule'], function(anotherModule) {
anotherModule.doSomeStuff(_.random(10));
// ...
});

@@ -68,3 +74,3 @@ });

Output (require):
Output:

@@ -76,6 +82,6 @@ ```javascript

require('myModule');
$(document).append($('<div>').text(_.random(10)));
// ...
(function() {
var anotherModule = require('anotherModule');
anotherModule.doSomeStuff(_.random(10));
// ...
})();

@@ -87,7 +93,16 @@ })();

AMD is interpreted as described and implemented by [RequireJS](http://requirejs.org/).
### Supported Versions
- Only **top-level** calls to a `define` function will be transformed.
- **All** calls to `require` where it is given an array of dependencies as its first argument will be transformed.
- If you would like the option to only transform top-level require calls, please file an issue.
Only Node.js >= 6 is supported. For Node.js 4, please use version 0.2.2:
```
npm install --save-dev babel-plugin-transform-amd-to-commonjs@0.2.2
```
### AMD
AMD is interpreted as described by the [AMD specification](https://github.com/amdjs/amdjs-api/blob/master/AMD.md).
- Only _top-level_ calls to a `define` function will be transformed.
- _All_ calls to `require` where it is given an array of dependencies as its first argument will be transformed.
- Explicitly requiring `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.

@@ -97,2 +112,15 @@ - The same is true for the [simplified CommonJS wrapper](http://requirejs.org/docs/api.html#cjsmodule).

### Upgrading Versions
#### 1.0.0
- Versions >= 0.2.1 and &lt; 1.0.0 support Node.js 4.
1.0.0 and above only support Node.js >= 6.
To upgrade to 1.0.0, first upgrade to Node.js >= 6.
- If everything works fine with &lt; 1.0.0, you should just be able to drop in >= 1.0.0 after upgrading Node.js.
If you have any issues, there is one more edge-case breaking change that _might_ be affecting you (but probably is not):
- 1.0.0 accounts for the case where you're using a combination of return statements and module/exports to define the exports of your AMD modules.
Earlier versions don't account for this case, so if you're upgrading, make sure that each AMD module only uses either return statements _or_ module/exports to define its exports.
See [#26](https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/pull/26) and the [caveats](#injecting-require-module-or-exports-as-dependencies) section of the README for more details.
## Caveats

@@ -106,6 +134,7 @@

The following will not be transformed, since the plugin does not traverse the arguments to define or require:
The following will _not_ be transformed, since the plugin does not traverse the arguments to define or require:
```javascript
var deps = ['one', 'two'];
// DON'T DO THIS! It won't be transformed.
var dependencies = ['one', 'two'];
var factory = function(one, two) {

@@ -115,3 +144,3 @@ one.doStuff();

};
define(deps, factory);
define(dependencies, factory);
```

@@ -130,53 +159,32 @@

It 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:
It 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')`.
- Returning any value other than `undefined` from a factory function will override anything you assign to `module` or `exports`.
This behaviour is in accordance with the AMD specification.
Unless you're doing something really weird in your modules, you don't have to worry about this case, but the plugin handles it by performing a check as needed on the return value of the factory function.
For example:
- 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`,
Input (AMD):
```javascript
define(['module'], function(module) {
module.exports = { hey: 'boi' };
return { value: 22 };
});
```
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):
Output (CommonJS):
```javascript
var amdDefineResult = function() {
module.exports = { hey: 'boi' };
return { value: 22 };
}();
typeof amdDefineResult !== 'undefined' && (module.exports = amdDefineResult);
```
```javascript
define(['exports'], function(exports) {
exports.stuff = 'hi';
return {
override: 'lol no stuff for you';
};
});
// exported: { override: 'lol no stuff for you' };
```
Note that `{ value: 22 }` is correctly exported in both cases. Without the `typeof amdDefineResult !== 'undefined'` check in place, `{ hey: 'boi' }` would have been erroneously exported once transformed to CommonJS.
This transforms to the following IIFE (with the return value not assigned to `module.exports` because you've injected `exports` as a dependency):
```javascript
(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:
```javascript
(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.
This pattern is only used if necessary. The variable `amdDefineResult` is generated to be unique in its scope.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc