mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
feat: support for self-referencing (#16068)
This commit is contained in:
parent
4559ac02ed
commit
03b9674cb2
@ -735,7 +735,19 @@ export function tryNodeResolve(
|
||||
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 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.
|
||||
|
@ -14,4 +14,5 @@ test('handle nested package', async () => {
|
||||
// expect(await page.textContent('.nested-e')).toBe('1')
|
||||
|
||||
expect(await page.textContent('.absolute-f')).toBe('F@2.0.0')
|
||||
expect(await page.textContent('.self-referencing')).toBe('true')
|
||||
})
|
||||
|
@ -24,6 +24,9 @@
|
||||
|
||||
<h2>absolute dependency path: <span class="absolute-f"></span></h2>
|
||||
|
||||
<h2>self referencing</h2>
|
||||
<pre class="self-referencing"></pre>
|
||||
|
||||
<script type="module">
|
||||
import A from '@vitejs/test-package-a'
|
||||
import B, { A as nestedA } from '@vitejs/test-package-b'
|
||||
@ -32,6 +35,7 @@
|
||||
import D, { nestedD } from '@vitejs/test-package-d'
|
||||
// import { testExcluded } from '@vitejs/test-package-e'
|
||||
import F from '__F_ABSOLUTE_PACKAGE_PATH__'
|
||||
import { isSelfReference } from '@vitejs/self-referencing/test'
|
||||
|
||||
text('.a', A)
|
||||
text('.b', B)
|
||||
@ -47,6 +51,8 @@
|
||||
|
||||
text('.absolute-f', F)
|
||||
|
||||
text('.self-referencing', isSelfReference)
|
||||
|
||||
function text(sel, text) {
|
||||
document.querySelector(sel).textContent = text
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
"@vitejs/test-package-c": "link:./test-package-c",
|
||||
"@vitejs/test-package-d": "link:./test-package-d",
|
||||
"@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"
|
||||
}
|
||||
}
|
||||
|
1
playground/self-referencing/index.js
Normal file
1
playground/self-referencing/index.js
Normal file
@ -0,0 +1 @@
|
||||
export const isSelfReference = true
|
8
playground/self-referencing/package.json
Normal file
8
playground/self-referencing/package.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "@vitejs/self-referencing",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./index.js",
|
||||
"./test": "./test/index.js"
|
||||
}
|
||||
}
|
1
playground/self-referencing/test/index.js
Normal file
1
playground/self-referencing/test/index.js
Normal file
@ -0,0 +1 @@
|
||||
export { isSelfReference } from '@vitejs/self-referencing'
|
@ -814,6 +814,9 @@ importers:
|
||||
|
||||
playground/nested-deps:
|
||||
dependencies:
|
||||
'@vitejs/self-referencing':
|
||||
specifier: link:../self-referencing
|
||||
version: link:../self-referencing
|
||||
'@vitejs/test-package-a':
|
||||
specifier: link:./test-package-a
|
||||
version: link:test-package-a
|
||||
@ -1252,6 +1255,8 @@ importers:
|
||||
specifier: 0.10.64
|
||||
version: 0.10.64
|
||||
|
||||
playground/self-referencing: {}
|
||||
|
||||
playground/ssr:
|
||||
devDependencies:
|
||||
express:
|
||||
|
Loading…
Reference in New Issue
Block a user