Relative imports in files inside folders resolve from src/ instead of the importing file
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 65/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, typescript
- Domain
- frontend
Research direction
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.
Written by the indexing model from the issue text.
Description
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
- Sibling
./d.jsincomponents/c.js:- SFC Playground: https://play.vuejs.org/#eNpdjkEKwjAQRa8yZNONJIh2I7HgPQoiccSKTUIyLdrSuztNQdrsMpn3359RXLyXfYfiJHQ0ofEEEanzVW2b1rtAMMIXJngE10IhlXH8a9FSVEa+YlFbrZYcJ3ggbP37RsgTgH7uqzHlJ634PcMrQOxEpuMr/q3DqvW+VOEn7Yyzkdh6hiGXzCBLNuDAYJFMGchGTl97DLFxlmMHWcpjKaYfq0tfAA==
@vue/replpreview (main): https://repl-vuejs.vercel.app/#eNpdjkEKwjAQRa8yZNONJIh2I7HgPQoiccSKTUIyLdrSuztNQdrsMpn3359RXLyXfYfiJHQ0ofEEEanzVW2b1rtAMMIXJngE10IhlXH8a9FSVEa+YlFbrZYcJ3ggbP37RsgTgH7uqzHlJ634PcMrQOxEpuMr/q3DqvW+VOEn7Yyzkdh6hiGXzCBLNuDAYJFMGchGTl97DLFxlmMHWcpjKaYfq0tfAA==
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'
-
Same setup when
d.js(export const z = 'from src/d.js') also exists at root: -
Parent
../d.jsincomponents/c.jsimporting rootd.js:
Actual
- Preview fails with
File "./d.js" does not exist.. - When
src/d.jsalso exists,components/c.jscompiles toconst __import_1__ = __modules__["src/d.js"]and silently rendersfrom src/d.jsinstead offrom components/d.js. - With
import { z } from '../d.js', theimportstatement is left untouched insrc/components/c.js,src/d.jsis not compiled, and the preview stays blank (export { z as y } from '../d.js'emits__export__(__module__, "y", () => z), andexport * from '../d.js'throwsFile "../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/
- Dominant language
- TypeScript
- Stars
- 1.1k
- Forks
- 222
- Avg merge
- 2h 10m
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from vuejs/repl
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Feat request: export injectKeyPropsMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 1/5 Under an hour Newbie friendliness 74/100
-
Difficulty 4/5 3-5 days Newbie friendliness 58/100
-
using Sandbox with clearConsole false,there would be a warning message on the browser's consoleOpen
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Using `setFiles` removes tsconfig.jsonPossibly taken @ifer47 claimed this 121 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 25/100
Similar issues
-
approved check:passed streams:remove
Difficulty 1/5 Under an hour Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Monthly Not a priority
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Expensify/App#107229 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
GitTools/actions#2297 · 1 reaction ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
hiero-ledger/hiero-sdk-js#4793 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
microsoft/vscode-cosmosdb#3594 · 1 comment ·
Maintainers usually reply within 1 day