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);
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!
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too