Skip to content

Commit 7f8dc58

Browse files
authored
fix: handle relative path glob raw import, fix #7307 (#7371)
1 parent 8ac4b12 commit 7f8dc58

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

packages/playground/glob-import/__tests__/glob-import.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ const rawResult = {
6666
}
6767
}
6868

69+
const relativeRawResult = {
70+
'../glob-import/dir/baz.json': {
71+
msg: 'baz'
72+
}
73+
}
74+
6975
test('should work', async () => {
7076
expect(await page.textContent('.result')).toBe(
7177
JSON.stringify(allResult, null, 2)
@@ -81,6 +87,12 @@ test('import glob raw', async () => {
8187
)
8288
})
8389

90+
test('import relative glob raw', async () => {
91+
expect(await page.textContent('.relative-glob-raw')).toBe(
92+
JSON.stringify(relativeRawResult, null, 2)
93+
)
94+
})
95+
8496
if (!isBuild) {
8597
test('hmr for adding/removing files', async () => {
8698
addFile('dir/a.js', '')

packages/playground/glob-import/index.html

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<pre class="result"></pre>
22
<pre class="result-node_modules"></pre>
33
<pre class="globraw"></pre>
4+
<pre class="relative-glob-raw"></pre>
45

56
<script type="module" src="./dir/index.js"></script>
67
<script type="module">
@@ -52,3 +53,21 @@
5253
2
5354
)
5455
</script>
56+
57+
<script type="module">
58+
const relativeRawModules = import.meta.globEager(
59+
'../glob-import/dir/*.json',
60+
{
61+
as: 'raw'
62+
}
63+
)
64+
const relativeGlobRaw = {}
65+
Object.keys(relativeRawModules).forEach((key) => {
66+
relativeGlobRaw[key] = JSON.parse(relativeRawModules[key])
67+
})
68+
document.querySelector('.relative-glob-raw').textContent = JSON.stringify(
69+
relativeGlobRaw,
70+
null,
71+
2
72+
)
73+
</script>

packages/vite/src/node/importGlob.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export async function transformImportGlob(
147147
)
148148
}
149149
entries += ` ${JSON.stringify(file)}: ${JSON.stringify(
150-
await fsp.readFile(path.join(base, file), 'utf-8')
150+
await fsp.readFile(path.join(base, files[i]), 'utf-8')
151151
)},`
152152
} else {
153153
const importeeUrl = isCSSRequest(importee) ? `${importee}?used` : importee

0 commit comments

Comments
 (0)