babel-plugin-transform-amd-to-commonjs
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -9,3 +9,4 @@ 'use strict'; | ||
AMD_DEFINE_RESULT: 'amdDefineResult', | ||
MAYBE_FUNCTION: 'maybeFunction' | ||
MAYBE_FUNCTION: 'maybeFunction', | ||
TRANSFORM_AMD_TO_COMMONJS_IGNORE: 'transform-amd-to-commonjs-ignore' | ||
}; |
'use strict'; | ||
const _require = require('./constants'), | ||
MODULE = _require.MODULE, | ||
EXPORTS = _require.EXPORTS, | ||
REQUIRE = _require.REQUIRE; // A factory function is exported in order to inject the same babel-types object | ||
const { | ||
MODULE, | ||
EXPORTS, | ||
REQUIRE, | ||
TRANSFORM_AMD_TO_COMMONJS_IGNORE | ||
} = require('./constants'); // A factory function is exported in order to inject the same babel-types object | ||
// being used by the plugin itself | ||
@@ -135,2 +137,11 @@ | ||
const hasIgnoreComment = node => { | ||
var _node$body, _node$body$; | ||
const leadingComments = ((_node$body = node.body) === null || _node$body === void 0 ? void 0 : (_node$body$ = _node$body[0]) === null || _node$body$ === void 0 ? void 0 : _node$body$.leadingComments) || []; | ||
return leadingComments.some(({ | ||
value | ||
}) => String(value).trim() === TRANSFORM_AMD_TO_COMMONJS_IGNORE); | ||
}; | ||
return { | ||
@@ -149,4 +160,5 @@ decodeDefineArguments, | ||
createFunctionCheck, | ||
isExplicitDependencyInjection | ||
isExplicitDependencyInjection, | ||
hasIgnoreComment | ||
}; | ||
}; |
'use strict'; | ||
const _require = require('./constants'), | ||
REQUIRE = _require.REQUIRE, | ||
MODULE = _require.MODULE, | ||
EXPORTS = _require.EXPORTS, | ||
DEFINE = _require.DEFINE, | ||
AMD_DEFINE_RESULT = _require.AMD_DEFINE_RESULT, | ||
MAYBE_FUNCTION = _require.MAYBE_FUNCTION; | ||
const { | ||
REQUIRE, | ||
MODULE, | ||
EXPORTS, | ||
DEFINE, | ||
AMD_DEFINE_RESULT, | ||
MAYBE_FUNCTION | ||
} = require('./constants'); | ||
@@ -16,19 +17,20 @@ const createHelpers = require('./helpers'); | ||
}) => { | ||
const _createHelpers = createHelpers({ | ||
const { | ||
decodeDefineArguments, | ||
decodeRequireArguments, | ||
isModuleOrExportsInjected, | ||
isSimplifiedCommonJSWrapper, | ||
createDependencyInjectionExpression, | ||
createRestDependencyInjectionExpression, | ||
createModuleExportsAssignmentExpression, | ||
createModuleExportsResultCheck, | ||
getUniqueIdentifier, | ||
isFunctionExpression, | ||
createFactoryReplacementExpression, | ||
createFunctionCheck, | ||
isExplicitDependencyInjection, | ||
hasIgnoreComment | ||
} = createHelpers({ | ||
types: t | ||
}), | ||
decodeDefineArguments = _createHelpers.decodeDefineArguments, | ||
decodeRequireArguments = _createHelpers.decodeRequireArguments, | ||
isModuleOrExportsInjected = _createHelpers.isModuleOrExportsInjected, | ||
isSimplifiedCommonJSWrapper = _createHelpers.isSimplifiedCommonJSWrapper, | ||
createDependencyInjectionExpression = _createHelpers.createDependencyInjectionExpression, | ||
createRestDependencyInjectionExpression = _createHelpers.createRestDependencyInjectionExpression, | ||
createModuleExportsAssignmentExpression = _createHelpers.createModuleExportsAssignmentExpression, | ||
createModuleExportsResultCheck = _createHelpers.createModuleExportsResultCheck, | ||
getUniqueIdentifier = _createHelpers.getUniqueIdentifier, | ||
isFunctionExpression = _createHelpers.isFunctionExpression, | ||
createFactoryReplacementExpression = _createHelpers.createFactoryReplacementExpression, | ||
createFunctionCheck = _createHelpers.createFunctionCheck, | ||
isExplicitDependencyInjection = _createHelpers.isExplicitDependencyInjection; | ||
}); | ||
const argumentDecoders = { | ||
@@ -43,7 +45,19 @@ [DEFINE]: decodeDefineArguments, | ||
const Program = (path, ...rest) => { | ||
const { | ||
node | ||
} = path; | ||
if (hasIgnoreComment(node)) return; | ||
path.traverse({ | ||
ExpressionStatement | ||
}, ...rest); | ||
}; | ||
const ExpressionStatement = (path, { | ||
opts | ||
}) => { | ||
const node = path.node, | ||
parent = path.parent; | ||
const { | ||
node, | ||
parent | ||
} = path; | ||
if (!t.isCallExpression(node.expression)) return; | ||
@@ -53,3 +67,5 @@ const options = Object.assign({ | ||
}, opts); | ||
const name = node.expression.callee.name; | ||
const { | ||
name | ||
} = node.expression.callee; | ||
const isDefineCall = name === DEFINE; | ||
@@ -59,7 +75,6 @@ if (isDefineCall && options.restrictToTopLevelDefine && !t.isProgram(parent)) return; | ||
if (!argumentDecoder) return; | ||
const _argumentDecoder = argumentDecoder(node.expression.arguments), | ||
dependencyList = _argumentDecoder.dependencyList, | ||
factory = _argumentDecoder.factory; | ||
const { | ||
dependencyList, | ||
factory | ||
} = argumentDecoder(node.expression.arguments); | ||
if (!t.isArrayExpression(dependencyList) && !factory) return; | ||
@@ -125,5 +140,5 @@ const isFunctionFactory = isFunctionExpression(factory); | ||
visitor: { | ||
ExpressionStatement | ||
Program | ||
} | ||
}; | ||
}; |
{ | ||
"name": "babel-plugin-transform-amd-to-commonjs", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Transforms AMD code to CommonJS", | ||
@@ -10,3 +10,5 @@ "main": "build/index.js", | ||
"build": "babel src --out-dir build", | ||
"prepack": "npm run build" | ||
"prepack": "npm run build", | ||
"contrib:add": "all-contributors add", | ||
"contrib:generate": "all-contributors generate" | ||
}, | ||
@@ -17,3 +19,3 @@ "files": [ | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=12" | ||
}, | ||
@@ -40,19 +42,22 @@ "repository": { | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.2", | ||
"@babel/core": "^7.1.2", | ||
"@babel/preset-env": "^7.1.0", | ||
"@babel/cli": "^7.17.0", | ||
"@babel/core": "^7.17.5", | ||
"@babel/preset-env": "^7.16.11", | ||
"all-contributors-cli": "^6.20.0", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-jest": "^23.2.0", | ||
"eslint": "^5.0.1", | ||
"babel-jest": "^27.5.1", | ||
"eslint": "^8.8.0", | ||
"eslint-config-msrose": "^1.0.0", | ||
"eslint-config-prettier": "^3.0.1", | ||
"eslint-plugin-jest": "^21.0.0", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"jest": "^23.2.0", | ||
"jest-diff": "^23.2.0", | ||
"prettier": "^1.14.3", | ||
"regenerator-runtime": "^0.12.0" | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-jest": "^26.1.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^27.5.1", | ||
"jest-diff": "^27.5.1", | ||
"prettier": "^2.5.1", | ||
"regenerator-runtime": "^0.13.9" | ||
}, | ||
"jest": { | ||
"setupTestFrameworkScriptFile": "<rootDir>/tests/setup.js", | ||
"setupFilesAfterEnv": [ | ||
"<rootDir>/tests/setup.js" | ||
], | ||
"collectCoverageFrom": [ | ||
@@ -59,0 +64,0 @@ "src/**/*.js" |
@@ -5,6 +5,4 @@ # 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) | ||
[![build](https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/actions/workflows/nodejs.yml/badge.svg)](https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/actions/workflows/nodejs.yml) | ||
[![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/) | ||
@@ -100,2 +98,18 @@ Babel plugin that transforms AMD to CommonJS. | ||
## Escape Hatch | ||
If you need to ignore specific modules that are picked up by the plugin (for example, those that are erroneously detected as AMD modules), you can add an ignore comment at the top of the file: | ||
``` | ||
/* transform-amd-to-commonjs-ignore */ | ||
define(['stuff', 'here'], function(donkeys, aruba) { | ||
return { | ||
llamas: donkeys.version, | ||
cows: aruba.hi | ||
}; | ||
}); | ||
``` | ||
The above module won't be transformed to CommonJS. The ignore comment must be at the beginning of the file and must be the only text in the comment block. | ||
## Details | ||
@@ -105,8 +119,4 @@ | ||
Only Node.js >= 6 is supported. For Node.js 4, please use version 0.2.2: | ||
Only LTS versions of Node.js still in maintenance will be supported going forward. Older versions of the plugin may support older versions of Node.js. See the [Node.js site](https://nodejs.org/en/about/releases/) for LTS details. | ||
``` | ||
npm install --save-dev babel-plugin-transform-amd-to-commonjs@0.2.2 | ||
``` | ||
While this plugin lists @babel/core@^7.0.0 as a peer dependency, it should still work fine with babel-core@^6.0.0. | ||
@@ -127,2 +137,6 @@ Listing this peer dependency aligns with [what is done by the core babel plugins](https://babeljs.io/docs/en/v7-migration#versioning-dependencies-blog-2017-12-27-nearing-the-70-releasehtml-peer-dependencies-integrations). | ||
#### 1.5.0 | ||
Version 1.5.0 stops building against Node.js versions less than 12.x (and the built files target Node.js 12.x), so make sure you're using at least Node.js 12.x. There are no known breaking changes caused by this, but if you for some reason cannot upgrade Node.js and are running into errors, please open an issue. | ||
#### 1.0.0 | ||
@@ -239,1 +253,13 @@ | ||
This pattern is only used if necessary. The variable `amdDefineResult` is generated to be unique in its scope. | ||
## Contributors | ||
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): | ||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
<!-- prettier-ignore --> | ||
<table><tr><td align="center"><a href="http://msrose.github.io"><img src="https://avatars3.githubusercontent.com/u/3495264?v=4" width="100px;" alt="Michael Rose"/><br /><sub><b>Michael Rose</b></sub></a><br /><a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/commits?author=msrose" title="Code">💻</a> <a href="#example-msrose" title="Examples">💡</a> <a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/commits?author=msrose" title="Documentation">📖</a> <a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/commits?author=msrose" title="Tests">⚠️</a> <a href="#infra-msrose" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td><td align="center"><a href="https://jordaneldredge.com"><img src="https://avatars2.githubusercontent.com/u/162735?v=4" width="100px;" alt="Jordan Eldredge"/><br /><sub><b>Jordan Eldredge</b></sub></a><br /><a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/commits?author=captbaritone" title="Code">💻</a> <a href="#review-captbaritone" title="Reviewed Pull Requests">👀</a></td><td align="center"><a href="https://github.com/FransBosuil"><img src="https://avatars2.githubusercontent.com/u/10304018?v=4" width="100px;" alt="FransBosuil"/><br /><sub><b>FransBosuil</b></sub></a><br /><a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/commits?author=FransBosuil" title="Code">💻</a> <a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/commits?author=FransBosuil" title="Tests">⚠️</a></td><td align="center"><a href="https://github.com/bschlenk"><img src="https://avatars2.githubusercontent.com/u/1390303?v=4" width="100px;" alt="Brian Schlenker"/><br /><sub><b>Brian Schlenker</b></sub></a><br /><a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/commits?author=bschlenk" title="Code">💻</a> <a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/issues?q=author%3Abschlenk" title="Bug reports">🐛</a> <a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/commits?author=bschlenk" title="Tests">⚠️</a></td><td align="center"><a href="https://github.com/apps/greenkeeper"><img src="https://avatars3.githubusercontent.com/in/505?v=4" width="100px;" alt="greenkeeper[bot]"/><br /><sub><b>greenkeeper[bot]</b></sub></a><br /><a href="#infra-greenkeeper[bot]" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td><td align="center"><a href="http://paquitosoftware.com"><img src="https://avatars3.githubusercontent.com/u/166022?v=4" width="100px;" alt="PaquitoSoft"/><br /><sub><b>PaquitoSoft</b></sub></a><br /><a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/issues?q=author%3APaquitoSoft" title="Bug reports">🐛</a></td><td align="center"><a href="https://philostler.com"><img src="https://avatars1.githubusercontent.com/u/244198?v=4" width="100px;" alt="Phil Ostler"/><br /><sub><b>Phil Ostler</b></sub></a><br /><a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/issues?q=author%3Aphilostler" title="Bug reports">🐛</a></td></tr><tr><td align="center"><a href="https://vaaralav.com"><img src="https://avatars0.githubusercontent.com/u/8571541?v=4" width="100px;" alt="Ville Vaarala"/><br /><sub><b>Ville Vaarala</b></sub></a><br /><a href="https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/issues?q=author%3Avaaralav" title="Bug reports">🐛</a></td></tr></table> | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27688
263
261
15