Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stubbed d.ts file should import file with extension in ESM package #370

Closed
yshrsmz opened this issue Feb 8, 2024 · 2 comments · Fixed by #371
Closed

stubbed d.ts file should import file with extension in ESM package #370

yshrsmz opened this issue Feb 8, 2024 · 2 comments · Fixed by #371

Comments

@yshrsmz
Copy link
Contributor

yshrsmz commented Feb 8, 2024

Environment

Node.js: v20.11.0
unbuild: 2.0.0

Reproduction

await writeFile(
output + ".d.ts",
[
`export * from ${JSON.stringify(resolvedEntryWithoutExt)};`,
hasDefaultExport
? `export { default } from ${JSON.stringify(
resolvedEntryWithoutExt,
)};`
: "",
].join("\n"),
);

Describe the bug

Say I have monorepo entirely build with ESM
Each package is declared as "type": "module" in its package.json

below is the basic directory structure

.
├── apps/
│   └── api/  // ESM module
├── packages/
│   └── db-provider  // ESM module
│       ├── dist/
│       │   ├── index.d.ts
│       │   └── ...
│       └── src/
│           └── index.ts
├── package.json

When I execute unbuild --stub in db-provider package, it generates 3 stubbed files in dist/ directory: index.d.ts, index.cjs, index.mjs.

JS files are fine, the problem is index.d.ts

export * from "/Users/yshrsmz/repos/github.com/yshrsmz/sample/packages/db-provider/src/index";

In ESM module we need to import files with extension, but there's no extension in the generated code.
And this causes resolution errors in consumer(api) package. (such as Module '"db-provider"' has no exported member 'ConnectionOptions'.).

I looked into the unbuild code and found out it always uses file name without the extension

await writeFile(
output + ".d.ts",
[
`export * from ${JSON.stringify(resolvedEntryWithoutExt)};`,
hasDefaultExport
? `export { default } from ${JSON.stringify(
resolvedEntryWithoutExt,
)};`
: "",
].join("\n"),
);

btw manually adding .js or .ts temporarily solves the issue

Additional context

No response

Logs

No response

@yshrsmz
Copy link
Contributor Author

yshrsmz commented Feb 8, 2024

My current workaround is to add .js extension by sed command.

"scripts": {
  "stub-fix": "if [[ \"$OSTYPE\" == \"darwin\"* ]]; then pnpm run stub-fix:darwin; else pnpm run stub-fix:linux; fi",
  "stub-fix:darwin": "find ./dist -type f -name \"*.ts\" -exec sed -i '' 's/\";$/.js\"/g' {} \\;",
  "stub-fix:linux": "find ./dist -type f -name \"*.ts\" -exec sed -i 's/\";$/.js\"/g' {} \\;",
  "stub": "unbuild --stub && pnpm run stub-fix",
}

@yshrsmz yshrsmz changed the title stubbed d.ts fIle should import file with extension in ESM package stubbed d.ts file should import file with extension in ESM package Feb 8, 2024
@MaximusHaximus
Copy link

@pi0 Would really like if this problem got resolved; in its current state, I get TS errors either when stubbing, or when building, depending on how I configure my package.json for packages in the workspace that I link to eachother. The easy stubbing functionality in unbuild was a really attractive feature when considering using it for our projects.

@pi0 pi0 closed this as completed in #371 Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants