Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-remapper-exports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/babel-plugin-import-path-remapper": patch
---

Fix lib/ → src/ remapping for packages with an `exports` field in package.json. Previously, packages declaring `exports` were skipped entirely, causing bundlers to load pre-built files instead of source during development.
25 changes: 18 additions & 7 deletions packages/babel-plugin-import-path-remapper/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,36 @@ function findMainSourceFile(sourcePath, requester, customRemap) {
resolveOptions
);

const { main, exports } = readPackage(manifestPath);
if (exports) {
// Skip packages that declare entry points
return;
const { main, exports: pkgExports } = readPackage(manifestPath);

// Determine the main entry point from `main` or `exports["."]`
let mainEntry = typeof main === "string" ? main : undefined;
if (!mainEntry && pkgExports) {
const root = pkgExports["."];
if (typeof root === "string") {
mainEntry = root;
} else if (root && typeof root === "object") {
// Prefer a TypeScript source entry (.ts/.tsx but not .d.ts)
const tsEntry = Object.values(root).find(
(v) => typeof v === "string" && /\.tsx?$/.test(v) && !/\.d\.ts$/.test(v)
);
mainEntry = tsEntry || root.default || root.require || root.import;
}
}

if (customRemap) {
return customRemap(
sourcePath,
typeof main === "string" ? main : undefined,
typeof mainEntry === "string" ? mainEntry : undefined,
requester
);
}

if (typeof main !== "string") {
if (typeof mainEntry !== "string") {
return;
}

const remappedPath = `${sourcePath}/${main.replace(
const remappedPath = `${sourcePath}/${mainEntry.replace(
/^(?:\.\/)?lib\/(.*)\.js/,
"src/$1.ts"
)}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
exports.A = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@rnx-kit/dts-example",
"version": "0.0.0-dev",
"exports": {
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
exports.A = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@rnx-kit/ts-example",
"version": "0.0.0-dev",
"exports": {
".": {
"types": "./lib/index.d.ts",
"source": "./src/index.ts",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const A = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@rnx-kit/example",
"version": "0.0.0-dev",
"main": "lib/index.js",
"exports": {
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,48 @@ describe("@rnx-kit/babel-plugin-import-path-remapper", () => {
`import(/* webpackChunkName: "example" */"@rnx-kit/example/__mocks__/lib/index");`
);
});

it("remaps barrel import for package with exports field", () => {
process.chdir("test/__fixtures__/with-exports");
equal(
transform(`import { A } from "@rnx-kit/example";`),
`import { A } from "@rnx-kit/example/src/index.ts";`
);
});

it("remaps barrel require for package with exports field", () => {
process.chdir("test/__fixtures__/with-exports");
equal(
transform(`require("@rnx-kit/example");`),
`require("@rnx-kit/example/src/index.ts");`
);
});

it("uses custom remap for package with exports field", () => {
process.chdir("test/__fixtures__/with-exports");
equal(
transform(`import { A } from "@rnx-kit/example";`, {
test: isRNXKit,
remap: (moduleName: string, path: string) =>
`${moduleName}/__mocks__/${path}`,
}),
`import { A } from "@rnx-kit/example/__mocks__/lib/index.js";`
);
});

it("prefers TypeScript source entry in exports over default", () => {
process.chdir("test/__fixtures__/with-exports-ts");
equal(
transform(`import { A } from "@rnx-kit/ts-example";`),
`import { A } from "@rnx-kit/ts-example/./src/index.ts";`
);
});

it("does not pick .d.ts entries as the main source", () => {
process.chdir("test/__fixtures__/with-exports-dts");
equal(
transform(`import { A } from "@rnx-kit/dts-example";`),
`import { A } from "@rnx-kit/dts-example/src/index.ts";`
);
});
});
Loading