rnx-kit/packages/babel-plugin-import-path-re...
renovate[bot] 57013d2191
fix(deps): update eslint (major) (#3394)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Tommy Nguyen <4123478+tido64@users.noreply.github.com>
2024-10-17 14:41:52 +00:00
..
src chore: allow `rnx-align-deps.mjs` to skip the build step (#2656) 2023-09-12 17:27:29 +02:00
test chore(deps): update babel monorepo to v7.25.4 (#3323) 2024-09-02 09:56:06 +00:00
CHANGELOG.md RELEASING: Releasing 44 package(s) (#3342) 2024-09-11 08:59:50 +02:00
README.md docs: automate removal of badges from READMEs (#2324) 2023-04-14 08:24:28 +02:00
eslint.config.js chore: migrate to ESLint flat config (#2782) 2023-11-03 14:02:18 +01:00
package.json fix(deps): update eslint (major) (#3394) 2024-10-17 14:41:52 +00:00
tsconfig.json feat(tsconfig): base TypeScript configs for working with Node (#2886) 2023-12-18 12:05:07 +01:00

README.md

@rnx-kit/babel-plugin-import-path-remapper

Build npm version

@rnx-kit/babel-plugin-import-path-remapper remaps **/lib/** imports to **/src/**. This is useful for packages that are not correctly exporting everything via their index.ts, but you still want to consume the TypeScript files rather than the transpiled JavaScript.

Usage

Add @rnx-kit/babel-plugin-import-path-remapper to your babel.config.js under plugins. For example, to remap all paths under the @rnx-kit scope:

// babel.config.js
module.exports = {
  presets: ["module:metro-react-native-babel-preset"],
  overrides: [
    {
      test: /\.tsx?$/,
      plugins: [
        // @babel/plugin-transform-typescript doesn't support `const enum`s.
        // See https://babeljs.io/docs/en/babel-plugin-transform-typescript#caveats
        // for more details.
        "const-enum",

        [
          "@rnx-kit/babel-plugin-import-path-remapper",
          { test: (source) => source.startsWith("@rnx-kit/") },
        ],
      ],
    },
  ],
};

Or, if you're using @rnx-kit/metro-config:

// babel.config.js
const { makeBabelConfig } = require("@rnx-kit/metro-config");
module.exports = makeBabelConfig([
  [
    "@rnx-kit/babel-plugin-import-path-remapper",
    { test: (source) => source.startsWith("@rnx-kit/") },
  ],
]);

Options

Option Type Description
test (source: string) => boolean [Required] A function returning whether the passed source should be redirected to another module.
remap (moduleName: string, path: string) => string [Optional] A function returning the module that should be used instead, e.g. contoso/index.js -> contoso/index.ts.