module-builder icon indicating copy to clipboard operation
module-builder copied to clipboard

Type import invalid path in build

Open zojize opened this issue 6 months ago • 1 comments

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

zojize avatar Jul 19 '25 23:07 zojize

I'm running into something similar. I import with the .js extension and the output .d.ts file imports from .js.js !??? I did not realize this until I actually started using my modules and some types weren't working. I do not really want to change all imports. For now I've patched the module-builder to disable addRelativeDeclarationExtensions: true which seems to cause this. It would be nice to be able to import this module's config to change and pass off to unbuild again.

Also moduleResolution stuff has always confused me. Is this intentional and nuxt modules should be written without extensions for some reason?

AlansCodeLog avatar Oct 01 '25 00:10 AlansCodeLog