2.0.1 • Published 5 years ago

babel-plugin-transform-require-extensions v2.0.1

Weekly downloads
12
License
MIT
Repository
github
Last release
5 years ago

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+