Relative imports in files inside folders resolve from src/ instead of the importing file

Aperta
#407 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
65/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, typescript
Ambito
frontend

Direzione di ricerca

Start in src/output/moduleCompiler.ts, in processModule, where local specifiers are rewritten with source.replace(/^./+/, 'src/') and only the './' prefix is checked for ImportDeclaration, ExportNamedDeclaration and dynamic import(). Resolve './' and '../' against the directory of the importing filename and normalize '.' and '..' segments. Done when the components/c.js and '../d.js' cases in the issue compile to the correct module keys, which you can check by reproducing them in the playground.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

When a file is placed in a subfolder (for example components/c.js, which FileSelector, setFiles, and Monaco/Volar all accept), relative imports in that file are resolved from src/ instead of from the importing file's directory, and ../ specifiers are not resolved as local modules.

Reproduction
  1. Sibling ./d.js in components/c.js:

App.vue

<script setup>
import { y } from './components/c.js'
</script>

<template>
  <h1>{{ y }}</h1>
</template>

components/c.js

import { z } from './d.js'
export const y = z

components/d.js

export const z = 'from components/d.js'
  1. Same setup when d.js (export const z = 'from src/d.js') also exists at root:

  2. Parent ../d.js in components/c.js importing root d.js:

Actual
  1. Preview fails with File "./d.js" does not exist..
  2. When src/d.js also exists, components/c.js compiles to const __import_1__ = __modules__["src/d.js"] and silently renders from src/d.js instead of from components/d.js.
  3. With import { z } from '../d.js', the import statement is left untouched in src/components/c.js, src/d.js is not compiled, and the preview stays blank (export { z as y } from '../d.js' emits __export__(__module__, "y", () => z), and export * from '../d.js' throws File "../d.js" does not exist.).
Expected

Relative specifiers (./ and ../, including re-exports and dynamic import()) resolve against the directory of the importing filename and normalize . and .., so ./d.js in src/components/c.js resolves to src/components/d.js and ../d.js resolves to src/d.js.

Cause

In src/output/moduleCompiler.ts, processModule rewrites local specifiers with source.replace(/^\.\/+/, 'src/') without using filename's directory, and only checks source.startsWith('./') for ImportDeclaration, ExportNamedDeclaration, and dynamic import().

Versions

@vue/[email protected] and main (59212b9), [email protected] (verified on play.vuejs.org, repl-vuejs.vercel.app in Chromium 156, and Node v26.13.0).

Credit: https://ucsbplse.github.io/

Lingua principale
TypeScript
Stelle
1.1k
Fork
222
Merge medio
2h 10m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vuejs/repl

Tutte le issue di vuejs/repl

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.