feat: support for self-referencing (#16068)

This commit is contained in:
Senao 2024-03-12 21:10:04 +08:00 committed by GitHub
parent 4559ac02ed
commit 03b9674cb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 37 additions and 2 deletions

View File

@ -735,7 +735,19 @@ export function tryNodeResolve(
basedir = root basedir = root
} }
const pkg = resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache) let selfPkg = null
if (!isBuiltin(id) && !id.includes('\0') && bareImportRE.test(id)) {
// check if it's a self reference dep.
const selfPackageData = findNearestPackageData(basedir, packageCache)
selfPkg =
selfPackageData?.data.exports && selfPackageData?.data.name === pkgId
? selfPackageData
: null
}
const pkg =
selfPkg ||
resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache)
if (!pkg) { if (!pkg) {
// if import can't be found, check if it's an optional peer dep. // if import can't be found, check if it's an optional peer dep.
// if so, we can resolve to a special id that errors only when imported. // if so, we can resolve to a special id that errors only when imported.

View File

@ -14,4 +14,5 @@ test('handle nested package', async () => {
// expect(await page.textContent('.nested-e')).toBe('1') // expect(await page.textContent('.nested-e')).toBe('1')
expect(await page.textContent('.absolute-f')).toBe('F@2.0.0') expect(await page.textContent('.absolute-f')).toBe('F@2.0.0')
expect(await page.textContent('.self-referencing')).toBe('true')
}) })

View File

@ -24,6 +24,9 @@
<h2>absolute dependency path: <span class="absolute-f"></span></h2> <h2>absolute dependency path: <span class="absolute-f"></span></h2>
<h2>self referencing</h2>
<pre class="self-referencing"></pre>
<script type="module"> <script type="module">
import A from '@vitejs/test-package-a' import A from '@vitejs/test-package-a'
import B, { A as nestedA } from '@vitejs/test-package-b' import B, { A as nestedA } from '@vitejs/test-package-b'
@ -32,6 +35,7 @@
import D, { nestedD } from '@vitejs/test-package-d' import D, { nestedD } from '@vitejs/test-package-d'
// import { testExcluded } from '@vitejs/test-package-e' // import { testExcluded } from '@vitejs/test-package-e'
import F from '__F_ABSOLUTE_PACKAGE_PATH__' import F from '__F_ABSOLUTE_PACKAGE_PATH__'
import { isSelfReference } from '@vitejs/self-referencing/test'
text('.a', A) text('.a', A)
text('.b', B) text('.b', B)
@ -47,6 +51,8 @@
text('.absolute-f', F) text('.absolute-f', F)
text('.self-referencing', isSelfReference)
function text(sel, text) { function text(sel, text) {
document.querySelector(sel).textContent = text document.querySelector(sel).textContent = text
} }

View File

@ -15,6 +15,7 @@
"@vitejs/test-package-c": "link:./test-package-c", "@vitejs/test-package-c": "link:./test-package-c",
"@vitejs/test-package-d": "link:./test-package-d", "@vitejs/test-package-d": "link:./test-package-d",
"@vitejs/test-package-e": "link:./test-package-e", "@vitejs/test-package-e": "link:./test-package-e",
"@vitejs/test-package-f": "link:./test-package-f" "@vitejs/test-package-f": "link:./test-package-f",
"@vitejs/self-referencing": "link:../self-referencing"
} }
} }

View File

@ -0,0 +1 @@
export const isSelfReference = true

View File

@ -0,0 +1,8 @@
{
"name": "@vitejs/self-referencing",
"type": "module",
"exports": {
".": "./index.js",
"./test": "./test/index.js"
}
}

View File

@ -0,0 +1 @@
export { isSelfReference } from '@vitejs/self-referencing'

View File

@ -814,6 +814,9 @@ importers:
playground/nested-deps: playground/nested-deps:
dependencies: dependencies:
'@vitejs/self-referencing':
specifier: link:../self-referencing
version: link:../self-referencing
'@vitejs/test-package-a': '@vitejs/test-package-a':
specifier: link:./test-package-a specifier: link:./test-package-a
version: link:test-package-a version: link:test-package-a
@ -1252,6 +1255,8 @@ importers:
specifier: 0.10.64 specifier: 0.10.64
version: 0.10.64 version: 0.10.64
playground/self-referencing: {}
playground/ssr: playground/ssr:
devDependencies: devDependencies:
express: express: