Skip to content

Commit

Permalink
Fix preconstruct dev with `exports: { importConditionDefaultExport:…
Browse files Browse the repository at this point in the history
… "default" }` with a `.d.ts` file
  • Loading branch information
emmatown committed May 2, 2024
1 parent 9f44a11 commit fa66c34
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-candles-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preconstruct/cli": patch
---

Fix `preconstruct dev` with `exports: { importConditionDefaultExport: "default" }` with a `.d.ts` file
50 changes: 50 additions & 0 deletions packages/cli/src/__tests__/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,53 @@ test("type: module running", async () => {
expect(stdout.toString().split("\n")).toEqual(["b", ""]);
expect(code).toBe(0);
});

test(".d.ts", async () => {
let tmpPath = await testdir({
"tsconfig.json": typescriptFixture["tsconfig.json"],
".babelrc": typescriptFixture[".babelrc"],
node_modules: typescriptFixture.node_modules,
"package.json": JSON.stringify({
name: "pkg",
main: "dist/pkg.cjs.js",
module: "dist/pkg.esm.js",
exports: {
".": {
module: "./dist/pkg.esm.js",
import: "./dist/pkg.cjs.mjs",
default: "./dist/pkg.cjs.js",
},
"./package.json": "./package.json",
},
preconstruct: {
exports: {
importConditionDefaultExport: "default",
},
},
}),
"src/index.js": ts`
export const a = "a";
export default a;
`,
"src/index.d.ts": ts`
export const a: string;
export default a;
`,
});
await dev(tmpPath);
expect(await getFiles(tmpPath, ["dist/**.d.{,m}ts"])).toMatchInlineSnapshot(`
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/pkg.cjs.d.mts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export * from "../src/index.js";
export { _default as default } from "./pkg.cjs.default.js";
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGtnLmNqcy5kLm10cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/pkg.cjs.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export * from "../src/index.js";
export { default } from "../src/index.js";
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGtnLmNqcy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2luZGV4LmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/pkg.cjs.default.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export { default as _default } from "../src/index.js"
`);
});
10 changes: 6 additions & 4 deletions packages/cli/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ export async function writeDevTSFiles(

const ext = path.extname(relativePathWithExtension).slice(1);
const mappedExt = { ts: "js", tsx: "js", mts: "mjs", cts: "cjs" }[ext];
const pathToImport = relativePathWithExtension.replace(
new RegExp(`\\.${ext}$`),
`.${mappedExt}`
);
const pathToImport = mappedExt
? relativePathWithExtension.replace(
new RegExp(`\\.${ext}$`),
`.${mappedExt}`
)
: relativePathWithExtension;
promises.push(
fs.outputFile(
dmtsReexportFilename,
Expand Down

0 comments on commit fa66c34

Please sign in to comment.