egoist/esbuild-register

bug: import() doesn't support ts

privatenumber opened this issue · 4 comments

Description

Importing a .ts file via import() doesn't resolve either due to not supporting extension-less imports, or not accepting .ts as a valid extension.

Reproduction

Import without extension

import('../src/manifest') // importing `../src/manifest.ts`

Error:

(node:4657) UnhandledPromiseRejectionWarning: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/src/manifest' imported from /scripts/prepare.ts
    at new NodeError (internal/errors.js:322:7)
    at finalizeResolution (internal/modules/esm/resolve.js:308:11)
    at moduleResolve (internal/modules/esm/resolve.js:731:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:842:11)
    at Loader.resolve (internal/modules/esm/loader.js:89:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:242:28)
    at Loader.import (internal/modules/esm/loader.js:177:28)
    at importModuleDynamically (internal/modules/cjs/loader.js:1028:27)
    at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:30:14)
    at writeManifest (/scripts/prepare.ts:37:26)

Import with extension

import('../src/manifest.ts')

Error:

(node:4716) UnhandledPromiseRejectionWarning: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /src/manifest.ts
    at new NodeError (internal/errors.js:322:7)
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
    at Loader.getFormat (internal/modules/esm/loader.js:105:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:243:31)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at Loader.import (internal/modules/esm/loader.js:177:17)
    at writeManifest (/scripts/prepare.ts:37:26)
    at async Promise.all (index 0)
    at /scripts/prepare.ts:54:2

@privatenumber I have the same issue, did you managed to fix it?

This also happened to me, I think it's something because of ES modules.

this needs --experimental-loader support I think https://nodejs.org/api/esm.html#esm_transpiler_loader

and @antfu has a package for this https://github.com/antfu/esbuild-node-loader

Closing as I use tsx now, which doesn't have this problem.