denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.01k stars 5.39k forks source link

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

Open Ciantic opened 1 month ago

Ciantic commented 1 month ago

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

Ciantic commented 4 weeks ago

Here is same project with Bun for comparison:

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

Ciantic commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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

https://github.com/vitejs/vite/pull/18479

It is not fully resolved yet, I get more errors

fro-profesional commented 3 weeks ago

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

fro-profesional commented 3 weeks ago

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 commented 3 weeks ago

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?
thesmartwon commented 1 week ago

Duplicate of #23929.