Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle relative path glob raw import, fix #7307 #7371

Merged
merged 2 commits into from Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"><!--issue #7307--></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
toshify marked this conversation as resolved.
Show resolved Hide resolved
const relativeRawModules = import.meta.globEager(
'../dynamic-import/nested/shar*.js',
toshify marked this conversation as resolved.
Show resolved Hide resolved
{
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