fix(nuxt): add type exports and generate runtime .d.ts files
In v1.2.6, imports from #logto fail with "File '.../handler.d.ts' is not a module" because package.json lacks type exports and @nuxt/module-builder generates empty .d.ts files for runtime sources.
Changes
-
package.json: Added
typesfield pointing to./dist/types.d.mtsin exports and root level -
build.config.ts: Post-build hook runs mkdist with
declaration: trueto regenerate runtime .d.ts files with actual type definitions - tsconfig.json: Excluded build.config.ts and playground from type checking
Result
// Previously failed with "File is not a module"
import { logtoEventHandler } from "#logto";
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig(event);
await logtoEventHandler(event, config); // Now properly typed
const accessToken = await event.context.logtoClient.getAccessToken();
return { accessToken };
});
The #logto alias resolves to dist/runtime/utils/handler.js, which now has a populated handler.d.ts with proper type declarations.
Original prompt
This section details on the original issue you should resolve
<issue_title>bug: @logto/nuxt 1.2.6 Can't import { logtoEventHandler } from "#logto";</issue_title> <issue_description>### Describe the bug
After update to version 1.2.6 Can't import { logtoEventHandler } from "#logto" in server middleware any more. Error message:
File '/node_modules/.pnpm/@[email protected][email protected]/node_modules/@logto/nuxt/dist/runtime/utils/handler.d.ts' is not a module.All typed files (e.g : dist/runtime/utils/handler.d.ts) are empty.
After compared package.json between v1.2.4 and v1.2.6 No types been exported
package.json v1.2.4
"exports": { ".": { "import": "./dist/module.mjs", "types": "./dist/module.d.ts" } }, "types": "./dist/module.d.ts",package.json v1.2.6
"exports": { ".": { "import": "./dist/module.mjs", } }Expected behavior
import { logtoEventHandler } from "#logto"; export default defineEventHandler(async (event) => { const config = useRuntimeConfig(event); await logtoEventHandler(event, config); const accessToken = await event.context.logtoClient.getAccessToken(); return { accessToken }; });How to reproduce?
Install version 1.2.6, then
// server/middleware/auth.ts import { logtoEventHandler } from "#logto"; export default defineEventHandler(async (event) => { const config = useRuntimeConfig(event); await logtoEventHandler(event, config); const accessToken = await event.context.logtoClient.getAccessToken(); return { accessToken }; });Environment
Self-hosted (Docker image)
Screenshots
No response</issue_description>
Comments on the Issue (you are @copilot in this section)
@wangsijie @Drunkenpilot could you please try to use it in ESM project? Which is `"type": "module"` in `package.json`.@wangsijie @Drunkenpilot Hi, thanks for the report, I can reproduce this issue, any suggestions? It does not work by `"types": "./dist/module.d.ts"`
- Fixes logto-io/js#985
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.