Skip to content

Commit

Permalink
fix: handle relative path glob raw import, fix vitejs#7307
Browse files Browse the repository at this point in the history
  • Loading branch information
toshify committed Mar 18, 2022
1 parent 1e9615d commit 6eb23fa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/playground/glob-import/__tests__/glob-import.spec.ts
Expand Up @@ -66,6 +66,11 @@ const rawResult = {
}
}

// issue #7307
const relativeRawResult = {
'../dynamic-import/nested/shared.js': 'export const n = 1\n'
}

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

// issue #7307
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
20 changes: 20 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,22 @@
2
)
</script>

<script type="module">
// issue #7307
const relativeRawModules = import.meta.globEager(
'../dynamic-import/nested/shar*.js',
{
as: 'raw'
}
)
const relativeGlobRaw = {}
Object.keys(relativeRawModules).forEach((key) => {
relativeGlobRaw[key] = relativeRawModules[key]
})
document.querySelector('.relative-glob-raw').textContent = JSON.stringify(
relativeGlobRaw,
null,
2
)
</script>
3 changes: 2 additions & 1 deletion packages/vite/src/node/importGlob.ts
Expand Up @@ -146,8 +146,9 @@ export async function transformImportGlob(
)
)
}
// issue #7307
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 6eb23fa

Please sign in to comment.