nuxt / nuxt/module-builder

Type import invalid path in build

Open
#647 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
275
Forks
35
Avg merge
4h 42m
Merged PRs (30d)
6

Description

Description

Types imported from parent folders have incorrect import paths and do not get recognized by TS due to the incorrect .js extension

Expected Behavior

Type import paths should end with .mts

Reproduction

https://codesandbox.io/p/devbox/l8c85t?file=%2Fdist%2Fruntime%2Futils%2Ffoo.d.ts%3A3%2C1

See the build output in dist. Type Foo imported from src/types.ts in the file src/runtime/server/utils/foo.ts using this import statement import type { Foo } from "../../../types";, got transformed to ../../../types.js which does not have a declaration file, and produces incorrect type.

Workaround

This is how I currently work around this issue.

import fs from 'node:fs'
import path from 'node:path'
export default defineBuildConfig({
  hooks: {
    'rollup:done': function (ctx) {
      const dts = path.resolve(ctx.options.outDir, 'types.d.mts')
      if (fs.existsSync(dts))
        fs.cpSync(dts, path.resolve(ctx.options.outDir, 'types.d.ts'))
    },
  },
})

Related Issue

#308 seems relevant, but I don't know how it's fixed for them

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the build with src/runtime/server/utils/foo.ts importing Foo from src/types.ts, then inspect the generated declaration in dist. Compare the emitted ../../../types.js path with the expected .mts path and verify that Foo is recognized by TypeScript without the rollup:done workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.