babel-plugin-transform-require-extensions

2.0.1 • Public • Published

babel-plugin-transform-require-extensions

npm version CI status

A Babel plugin that transforms specified require path file extensions.

This is useful for building dual ESM/CJS packages. File names with extensions are mandatory in ESM import specifiers in .mjs files. After transpiling this to CJS in .js files, the hardcoded .mjs extensions in the require paths must be transformed to .js. Alternatively, sometimes it’s safe to remove the extensions as Node.js in CJS mode can automatically resolve .js file extensions.

Setup

To install from npm run:

npm install babel-plugin-transform-require-extensions --save-dev

Configure Babel to use the plugin:

{ "plugins": ["transform-require-extensions"] }

This plugin must run after any ESM to CJS transpilation occurs.

By default .mjs extensions are transformed to .js. The extensions option can be used to specify extension replacements. For example, to remove .mjs extensions:

{
  "plugins": [
    [
      "transform-require-extensions",
      {
        "extensions": {
          ".mjs": ""
        }
      }
    ]
  ]
}

Support

  • Node.js v10+

Package Sidebar

Install

npm i babel-plugin-transform-require-extensions

Weekly Downloads

5

Version

2.0.1

License

MIT

Unpacked Size

5.07 kB

Total Files

4

Last publish

Collaborators

  • jaydenseric