Resolve imports to various extensions with a babel plugin
Resolve imports to more file extensions with a babel plugin
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');
npm install --save babel-plugin-extension-resolver
.babelrc
{
"plugins": ["extension-resolver"]
}
With options:
{
"plugins": [["extension-resolver", {
"extensions": [".cool.js", ".js"]
}]]
}
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.