Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-module-extension-resolver

shimataro13.3kMIT1.0.0

Babel plugin that resolves and maps module extensions.

babel, babel-plugin

readme

babel-plugin-module-extension-resolver

Build Status (Windows) Build Status (macOS) Build Status (Linux) Examples Release Node.js version License

Babel plugin that resolves and maps module extensions.

Inspired by babel-plugin-extension-resolver.

Examples

By default, all extensions except .json is converted into .js. This behavior can be customized by options.

JavaScript

Directory structure:

srcdir
│ ├ index.js
│ └ lib.jsmain.jssettings.json

Input (main.js):

require("./dir/lib");
require("./dir/lib.js");    // file exists
require("./dir");           // directory has "index.js"
require("./settings");      // ".json" extension
require("./no-such-file");  // file NOT exists
require("dir");             // not begins with "."

Output:

require("./dir/lib.js");
require("./dir/lib.js");
require("./dir/index.js");
require("./settings.json");
require("./no-such-file");
require("dir");

JavaScript (.mjs extension)

Directory structure:

srcdir
│ ├ index.mjs
│ └ lib.mjsmain.mjs

.babelrc:

{
  "presets": [
    ["@babel/preset-env", {"modules": false}]
  ],
  "plugins": [
    ["module-extension-resolver", {
      "extensionsToKeep": [".mjs", ".json"]
    }]
  ]
}

Input (main.mjs):

import "./dir/lib";
import "./dir";

export * from "./dir";

async function foo() {
    await import("./dir/lib");
}

Run:

babel src --keep-file-extension

Output:

import "./dir/lib.mjs";
import "./dir/index.mjs";

export * from "./dir/index.mjs";

async function foo() {
    await import("./dir/lib.mjs");
}

TypeScript

Directory structure:

srcdir
│ ├ index.ts
│ └ lib.tsmain.ts

Input (main.ts):

import "./dir/lib";
import "./dir";

Output:

import "./dir/lib.js";
import "./dir/index.js";

For complete project, see below examples.

Language CommonJS ES Modules
ECMAScript with @babel/preset-env babel-cjs babel-esm
TypeScript with @babel/preset-typescript ts-babel-cjs ts-babel-esm
TypeScript with tsc and Babel ts-tsc-cjs ts-tsc-esm

Install

npm i -D babel-plugin-module-extension-resolver

.babelrc

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

With options:

{
  "plugins": [
    ["module-extension-resolver", {
      "srcExtensions": [".js", ".cjs", ".mjs", ".es", ".es6", ".ts", ".node", ".json"],
      "dstExtension": ".js",
      "extensionsToKeep": [".json"]
    }]
  ]
}

Options

srcExtensions

source extensions to resolve

defaults:

[
  ".js",
  ".cjs",
  ".mjs",
  ".es",
  ".es6",
  ".ts",
  ".node",
  ".json"
]

dstExtension

destination extension

defaults:

".js"

extensionsToKeep

extension to keep

defaults:

[
  ".json"
]

Changelog

See CHANGELOG.md.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

1.0.0 - 2022-12-23

Others

  • some refactorings and package update

1.0.0-rc.2 - 2021-02-10

Added

  • support dynamic imports (thanks @rozzzly)

Fixed

  • badge URL

1.0.0-rc.1 - 2020-02-16

Others

  • preparing v1

0.3.0 - 2020-02-06

Changed

  • change options
    • srcExtensions: renamed from extensions
    • dstExtension: new
    • extensionsToKeep: instead of map

0.2.0 - 2020-01-04

Changed

  • support export from syntax
  • support .cjs extension

Fixed

  • Node.js version >=8.0.0

0.1.1 - 2020-01-02

Fixed

  • plugin name

0.1.0 - 2020-01-02

  • First release.