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

Vite doesn't support Deno's jsr: imports #26492

Open
Ciantic opened this issue Oct 23, 2024 · 7 comments
Open

Vite doesn't support Deno's jsr: imports #26492

Ciantic opened this issue Oct 23, 2024 · 7 comments
Labels
needs investigation requires further investigation before determining if it is an issue or not

Comments

@Ciantic
Copy link

Ciantic commented Oct 23, 2024

Deno version: deno --version
deno 2.0.2 (stable, release, x86_64-pc-windows-msvc)
v8 12.9.202.13-rusty
typescript 5.6.2

Here is a repo: https://github.com/Ciantic/deno-pure-solid-start

11.50.18 [vite] Error when evaluating SSR module C:/Source/JavaScript/solid-start-test/deno-solid-test/src/routes/index.tsx?pick=default&pick=$css: failed to import "jsr:@db/sqlite@0.12"
|- Error: Cannot find module 'jsr:@db/sqlite@0.12' imported from 'C:/Source/JavaScript/solid-start-test/deno-solid-test/src/db/db.ts'
    at nodeImport (C:\Source\JavaScript\solid-start-test\deno-solid-test\node_modules\.deno\vite@5.4.9\node_modules\vite\dist\node\chunks\dep-Cyk9bIUq.js:53036:19)        
    at ssrImport (C:\Source\JavaScript\solid-start-test\deno-solid-test\node_modules\.deno\vite@5.4.9\node_modules\vite\dist\node\chunks\dep-Cyk9bIUq.js:52903:22)
    at undefined
    at async instantiateModule (C:\Source\JavaScript\solid-start-test\deno-solid-test\node_modules\.deno\vite@5.4.9\node_modules\vite\dist\node\chunks\dep-Cyk9bIUq.js:52961:5)

Discord link

@lucacasonato lucacasonato added the needs investigation requires further investigation before determining if it is an issue or not label Oct 24, 2024
@Ciantic
Copy link
Author

Ciantic commented Oct 24, 2024

Here is same project with Bun for comparison:

https://github.com/Ciantic/bun-solid-project

@Ciantic
Copy link
Author

Ciantic commented Oct 26, 2024

I have investigated more, it comes from vite's ESM Module importer in here:

https://github.com/vitejs/vite/blob/main/packages/vite/src/module-runner/esmEvaluator.ts#L32-L38

    const initModule = new AsyncFunction(
      ssrModuleExportsKey,
      ssrImportMetaKey,
      ssrImportKey,
      ssrDynamicImportKey,
      ssrExportAllKey,
      // source map should already be inlined by Vite
      '"use strict";' + code,
    )

    await initModule(
      context[ssrModuleExportsKey],
      context[ssrImportMetaKey],
      context[ssrImportKey],
      context[ssrDynamicImportKey],
      context[ssrExportAllKey],
    )

Some reasons it works in bun with special imports but not with deno.

@Ciantic
Copy link
Author

Ciantic commented Oct 26, 2024

Ahaa! Found the culprit! Vite has whitelisted what are Deno/Bun imports here:

https://github.com/vitejs/vite/blob/fce3b09dcf405072b0e992fc55800e0a58aa88b7/packages/vite/src/node/utils.ts#L91-L98

It is missing jsr!

NOTICE also very important comment:

// Supported by Node, Deno, Bun
const NODE_BUILTIN_NAMESPACE = 'node:'
// Supported by Deno
const NPM_BUILTIN_NAMESPACE = 'npm:'
// Supported by Bun
const BUN_BUILTIN_NAMESPACE = 'bun:'
// Some runtimes like Bun injects namespaced modules here, which is not a node builtin
const nodeBuiltins = builtinModules.filter((id) => !id.includes(':'))

// TODO: Use `isBuiltin` from `node:module`, but Deno doesn't support it
export function isBuiltin(id: string): boolean {
  if (process.versions.deno && id.startsWith(NPM_BUILTIN_NAMESPACE)) return true
  if (process.versions.bun && id.startsWith(BUN_BUILTIN_NAMESPACE)) return true
  return isNodeBuiltin(id)
}

Deno is missing isBuiltin support so they have whitelisted things!

@Ciantic
Copy link
Author

Ciantic commented Oct 26, 2024

I made a pull request to support jsr: imports in vite:

vitejs/vite#18479

It is not fully resolved yet, I get more errors

@Ciantic Ciantic changed the title Vinxi project such as solid-start doesn't work with Deno 2.0 Vite doesn't support Deno's jsr: imports Oct 26, 2024
@fro-profesional
Copy link

Why can't you use the deno official vite plugin for that? https://github.com/denoland/deno-vite-plugin

@fro-profesional
Copy link

Why can't you use the deno official vite plugin for that? https://github.com/denoland/deno-vite-plugin

Never mind, I tried and it did resolve the imports but got a different error, I'll create a repro for that and create and issue with deno/vinxi unsure where the issue is, but jsr resolver is working

@Ciantic
Copy link
Author

Ciantic commented Oct 26, 2024

I'm starting to think this is not possible, because jsr: imports are not possible in Deno's node compatibility mode.

With pure deno mode I have test repo:

https://github.com/Ciantic/deno-pure-solid-start

But it yields even more weirder errors:

[h3] [unhandled] Error: Failed to load url ./src/database.ts (resolved id: ./src/database.ts) in deno::TypeScript::@db/sqlite::C:/Users/jarip/AppData/Local/deno/remote/https/jsr.io/ff2127150c5fc24dbc6b1daba62c248f566cce7c9346539da1003b4019d972c2. Does the file exist?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation requires further investigation before determining if it is an issue or not
Projects
None yet
Development

No branches or pull requests

3 participants