Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: handle relative path glob raw import, fix #7307 (#7371)
  • Loading branch information
toshify committed 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
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
@@ -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
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.