babel-plugin-extension-resolver

Resolve imports to various extensions with a babel plugin

View the Project on GitHub

current version Build Status Coverage Status Mutation testing badge semantic-release Commitizen friendly

Resolve imports to more file extensions with a babel plugin

Example

With this file structure:

src/index.js
src/other.node.js
src/other.js
src/another.js

In:

// src/index.js
import other from './other';
import another from './another';

require('./other');
require('./another');

Out:

// lib/index.js
import other from './other.node.js';
import another from './another.js';

require('./other.node.js');
require('./another.js');

Install

npm install --save babel-plugin-extension-resolver

.babelrc

{
  "plugins": ["extension-resolver"]
}

With options:

{
  "plugins": [["extension-resolver", {
    "extensions": [".cool.js", ".js"]
  }]]
}

Options

extensions

Defaults: (similar to how create-react-app does it for web)

[
  '.node.mjs',
  '.mjs',
  '.node.js',
  '.js',
  '.node.ts',
  '.ts',
  '.node.tsx',
  '.tsx',
  '.json',
  '.node.jsx',
  '.jsx',
  '.node',
]

Warning: This overrides ALL extension resolution, notably .json and .node. Make sure to re-include all extensions that matter.