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: make glob pattern supports nested paths #19622

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,35 @@ async function createAssetImportMetaurlPluginTransform() {
describe('assetImportMetaUrlPlugin', async () => {
const transform = await createAssetImportMetaurlPluginTransform()

test('no file extension specified', async () => {
expect(
await transform('new URL(`./foo/${filePath}`, import.meta.url)'),
).toMatchInlineSnapshot(
`"new URL((import.meta.glob("./foo/**", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${filePath}\`], import.meta.url)"`,
)
})

test('variable between /', async () => {
expect(
await transform('new URL(`./foo/${dir}/index.js`, import.meta.url)'),
).toMatchInlineSnapshot(
`"new URL((import.meta.glob("./foo/*/index.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}/index.js\`], import.meta.url)"`,
`"new URL((import.meta.glob("./foo/**/index.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}/index.js\`], import.meta.url)"`,
)
})

test('variable before non-/', async () => {
expect(
await transform('new URL(`./foo/${dir}.js`, import.meta.url)'),
).toMatchInlineSnapshot(
`"new URL((import.meta.glob("./foo/*.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}.js\`], import.meta.url)"`,
`"new URL((import.meta.glob("./foo/**/*.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}.js\`], import.meta.url)"`,
)
})

test('two variables', async () => {
expect(
await transform('new URL(`./foo/${dir}${file}.js`, import.meta.url)'),
).toMatchInlineSnapshot(
`"new URL((import.meta.glob("./foo/*.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}\${file}.js\`], import.meta.url)"`,
`"new URL((import.meta.glob("./foo/**/*.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}\${file}.js\`], import.meta.url)"`,
)
})

Expand All @@ -53,7 +61,7 @@ describe('assetImportMetaUrlPlugin', async () => {
'new URL(`./foo/${dir}${dir2}/index.js`, import.meta.url)',
),
).toMatchInlineSnapshot(
`"new URL((import.meta.glob("./foo/*/index.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}\${dir2}/index.js\`], import.meta.url)"`,
`"new URL((import.meta.glob("./foo/**/index.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}\${dir2}/index.js\`], import.meta.url)"`,
)
})

Expand Down
28 changes: 21 additions & 7 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,35 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
}
}

const extRE = /^\.[a-zA-Z0-9]+$/
function buildGlobPattern(ast: any) {
let pattern = ''
let lastIsGlob = false
let hasGlob = false
for (let i = 0; i < ast.quasis.length; i++) {
const str = ast.quasis[i].value.raw
if (str) {
pattern += str
lastIsGlob = false
if (!str) {
continue
}

if (ast.expressions[i] && !lastIsGlob) {
pattern += '*'
lastIsGlob = true
const isExt = extRE.test(str)

if (isExt) {
const endsWithStar = pattern.endsWith('*')
if (endsWithStar && hasGlob) {
pattern += '/*'
} else if (!endsWithStar) {
pattern += '*'
}
}

pattern += str

if (!hasGlob && !isExt) {
pattern += '**'
hasGlob = true
}
}

return pattern
}

Expand Down
Loading