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

Dynamic Import Variables for TypeScript imports #25

Open
LaserKaspar opened this issue Jul 13, 2024 · 1 comment
Open

Dynamic Import Variables for TypeScript imports #25

LaserKaspar opened this issue Jul 13, 2024 · 1 comment

Comments

@LaserKaspar
Copy link

Works as expected

Dynamically importing TypeScript works just fine:

import("./tsfile").then(console.log);

Dynamically importing JS with a variable also works just fine:

const dynamicjsfile = "mydynamicjsfile";
import("./" + mydynamicjsfile + ".js").then(console.log);

Unexpected behaviour

For some reason importing a ts file dynamically using no file extension (which does work in the rest of the codebase thanks to vite-plugin-dynamic-import) requests exactly that file without transpiling it:

const dynamictsfile = "dynamictsfile";
import("./" + dynamictsfile).then(console.log);

It throws:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module './dynamictsfile' imported from index.bundled_t18m7musuj.mjs
    at finalizeResolution (node:internal/modules/esm/resolve:260:11)
    at moduleResolve (node:internal/modules/esm/resolve:920:10)
    at defaultResolve (node:internal/modules/esm/resolve:1119:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:542:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:511:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:241:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:474:34)
    at defaultImportModuleDynamicallyForModule (node:internal/modules/esm/utils:214:31)
    at importModuleDynamicallyCallback (node:internal/modules/esm/utils:253:12)

And adding the file-extension to adhere to the vite standard (relative, no alias, file-extension) vite/features#dynamic-import

const dynamictsfile = "dynamictsfile";
import("./" + dynamictsfile + ".ts").then(console.log);

results in forwarding it to esm without transpilation:

node:internal/modules/esm/get_format:176
  throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath);     
        ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for ./dynamictsfile.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:176:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:219:36)
    at defaultLoad (node:internal/modules/esm/load:133:22) 
    at async ModuleLoader.load (node:internal/modules/esm/loader:555:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:436:45)
    at async ModuleJob._link (node:internal/modules/esm/module_job:106:19) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Using glob import vite/features#glob-import

const modules = import.meta.glob('./*.js')
console.log(modules);

results in:

Unhandled Rejection at: Promise {
  <rejected> TypeError: (intermediate value).glob is not a 
function

reason: TypeError: (intermediate value).glob is not a function

So glob is probably not available in this stage of the compilation process / inside the compile-time context.

I hope I didn't miss something obvious & you can provide some assistance on resolving this issue.
If you need any more info I'd be happy to provide anything you need.

Thanks for your time & this awesome project!

@egoist
Copy link
Owner

egoist commented Dec 1, 2024

it's an esbuild limitation, it needs to be a string literal to be bundled

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

No branches or pull requests

2 participants