Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/reactioncommerce/babel-remove-es-create-require
https://github.com/reactioncommerce/babel-remove-es-create-require
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/reactioncommerce/babel-remove-es-create-require
- Owner: reactioncommerce
- License: apache-2.0
- Created: 2019-10-17T20:29:58.000Z (about 5 years ago)
- Default Branch: trunk
- Last Pushed: 2019-10-17T22:45:33.000Z (about 5 years ago)
- Last Synced: 2024-10-30T15:13:27.814Z (2 months ago)
- Language: JavaScript
- Size: 43 KB
- Stars: 1
- Watchers: 6
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @reactioncommerce/babel-remove-es-create-require
A [Babel](https://babeljs.io) plugin.
If you create a Node 12 project with ECMAScript (ES) modules enabled, you will likely be doing this in a few places to import from CommonJS modules:
```js
import { createRequire } from "module";const require = createRequire(import.meta.url);
```This is the official way to get the `require` function in a module file.
Now let's say you want to add some Jest tests. Unfortunately Jest does not yet natively parse ES modules, and it doesn't like `import.meta`, so you'll need Babel configured for tests only.
Install `@babel/core`, `babel-jest`, `@babel/preset-env`, and two plugins that take care of transforming your ES modules:
```sh
npm i --save-dev @babel/core @babel/preset-env babel-jest babel-plugin-transform-import-meta babel-plugin-transform-es2015-modules-commonjs
```The `babel-plugin-transform-es2015-modules-commonjs` plugin does most of the transformation to CommonJS and `babel-plugin-transform-import-meta` transforms the `import.meta` code into something Jest will accept. But there's still a problem...
```js
const require = createRequire(import.meta.url);
```Those pesky `createRequire` calls are still in there, and now they're causing confusion. We no longer need them because `require` will now work automatically based on the file being CommonJS.
This is where this package comes in. It does only one thing: removes all lines where you're calling `createRequire`. Install it the same way you installed the other Babel plugins:
```sh
npm i --save-dev @reactioncommerce/babel-remove-es-create-require
```Then add it to your Babel config plugins list. Here's an example `babel.config.js` file that should work for a Node 12 project with ES modules enabled.
```js
module.exports = function (api) {
api.cache(false);return {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "12"
}
}
]
],
plugins: [
"babel-plugin-transform-import-meta",
"module:@reactioncommerce/babel-remove-es-create-require",
"transform-es2015-modules-commonjs"
]
};
};
```