Skip to content

Commit

Permalink
fix: handle relative path glob raw import, fix #7307 (#7371)
Browse files Browse the repository at this point in the history
  • Loading branch information
toshify authored Mar 27, 2022
1 parent 8ac4b12 commit 7f8dc58
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/playground/glob-import/__tests__/glob-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ const rawResult = {
}
}

const relativeRawResult = {
'../glob-import/dir/baz.json': {
msg: 'baz'
}
}

test('should work', async () => {
expect(await page.textContent('.result')).toBe(
JSON.stringify(allResult, null, 2)
Expand All @@ -81,6 +87,12 @@ test('import glob raw', async () => {
)
})

test('import relative glob raw', async () => {
expect(await page.textContent('.relative-glob-raw')).toBe(
JSON.stringify(relativeRawResult, null, 2)
)
})

if (!isBuild) {
test('hmr for adding/removing files', async () => {
addFile('dir/a.js', '')
Expand Down
19 changes: 19 additions & 0 deletions packages/playground/glob-import/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<pre class="result"></pre>
<pre class="result-node_modules"></pre>
<pre class="globraw"></pre>
<pre class="relative-glob-raw"></pre>

<script type="module" src="./dir/index.js"></script>
<script type="module">
Expand Down Expand Up @@ -52,3 +53,21 @@
2
)
</script>

<script type="module">
const relativeRawModules = import.meta.globEager(
'../glob-import/dir/*.json',
{
as: 'raw'
}
)
const relativeGlobRaw = {}
Object.keys(relativeRawModules).forEach((key) => {
relativeGlobRaw[key] = JSON.parse(relativeRawModules[key])
})
document.querySelector('.relative-glob-raw').textContent = JSON.stringify(
relativeGlobRaw,
null,
2
)
</script>
2 changes: 1 addition & 1 deletion packages/vite/src/node/importGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export async function transformImportGlob(
)
}
entries += ` ${JSON.stringify(file)}: ${JSON.stringify(
await fsp.readFile(path.join(base, file), 'utf-8')
await fsp.readFile(path.join(base, files[i]), 'utf-8')
)},`
} else {
const importeeUrl = isCSSRequest(importee) ? `${importee}?used` : importee
Expand Down

0 comments on commit 7f8dc58

Please sign in to comment.