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

feat: import.meta.glob support ?raw #5545

Merged
merged 7 commits into from
Dec 28, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -45,12 +45,24 @@ const allResult = {
}
}

const rawResult = {
'/dir/baz.json': {
msg: 'baz'
}
}

test('should work', async () => {
expect(await page.textContent('.result')).toBe(
JSON.stringify(allResult, null, 2)
)
})

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

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

<script type="module" src="./dir/index.js"></script>
<script type="module">
Expand All @@ -19,3 +20,16 @@
document.querySelector('.result').textContent = JSON.stringify(res, null, 2)
})
</script>

<script type="module">
const rawModules = import.meta.globEager('/dir/*.json?raw')
const globraw = {}
Object.keys(rawModules).forEach((key) => {
globraw[key] = JSON.parse(rawModules[key])
})
document.querySelector('.globraw').textContent = JSON.stringify(
globraw,
null,
2
)
</script>
15 changes: 13 additions & 2 deletions packages/vite/src/node/importGlob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import { promises as fsp } from 'fs'
import glob from 'fast-glob'
import {
isModernFlag,
Expand All @@ -8,6 +9,8 @@ import {
import { cleanUrl } from './utils'
import { RollupError } from 'rollup'

const rawRE = /(\?|&)raw(?:&|$)/

export async function transformImportGlob(
source: string,
pos: number,
Expand Down Expand Up @@ -42,6 +45,10 @@ export async function transformImportGlob(
if (!pattern.startsWith('.') && !pattern.startsWith('/')) {
throw err(`pattern must start with "." or "/" (relative to project root)`)
}
const isRaw = rawRE.test(pattern)
if (isRaw) {
pattern = cleanUrl(pattern)
}
let base: string
let parentDepth = 0
const isAbsolute = pattern.startsWith('/')
Expand Down Expand Up @@ -79,8 +86,12 @@ export async function transformImportGlob(
;[importee] = await normalizeUrl(file, pos)
}
imports.push(importee)
const identifier = `__glob_${importIndex}_${i}`
if (isEager) {
if (isRaw) {
entries += ` ${JSON.stringify(file)}: ${JSON.stringify(
await fsp.readFile(path.join(base, file), 'utf-8')
)},`
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
} else if (isEager) {
const identifier = `__glob_${importIndex}_${i}`
importsString += `import ${
isEagerDefault ? `` : `* as `
}${identifier} from ${JSON.stringify(importee)};`
Expand Down