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: asset new URL(,import.meta.url) match #18194

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
@@ -0,0 +1,59 @@
import { describe, expect, test } from 'vitest'
import { parseAst } from 'rollup/parseAst'
import { assetImportMetaUrlPlugin } from '../../plugins/assetImportMetaUrl'
import { resolveConfig } from '../../config'
import { PartialEnvironment } from '../../baseEnvironment'

async function createAssetImportMetaurlPluginTransform() {
const config = await resolveConfig({ configFile: false }, 'serve')
const instance = assetImportMetaUrlPlugin(config)
const environment = new PartialEnvironment('client', config)

return async (code: string) => {
// @ts-expect-error transform should exist
const result = await instance.transform.call(
{ environment, parse: parseAst },
code,
'foo.ts',
)
return result?.code || result
}
}

describe('assetImportMetaUrlPlugin', async () => {
const transform = await createAssetImportMetaurlPluginTransform()

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)"`,
)
})

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)"`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, this was ./foo/**.js.

)
})

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)"`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, this was ./foo/****.js.

)
})

test('two variables between /', async () => {
expect(
await transform(
'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)"`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, this was ./foo/****/index.js.

)
})
})
24 changes: 13 additions & 11 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,21 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {

function buildGlobPattern(ast: any) {
let pattern = ''
let lastElementIndex = -1
for (const exp of ast.expressions) {
for (let i = lastElementIndex + 1; i < ast.quasis.length; i++) {
const el = ast.quasis[i]
if (el.end < exp.start) {
pattern += el.value.raw
lastElementIndex = i
let lastIsGlob = false
for (let i = 0; i < ast.quasis.length; i++) {
const str = ast.quasis[i].value.raw
if (str) {
if (lastIsGlob && str[0] !== '/') {
pattern += '/*'
}
pattern += str
lastIsGlob = false
}

if (ast.expressions[i] && !lastIsGlob) {
pattern += '**'
lastIsGlob = true
}
pattern += '**'
}
for (let i = lastElementIndex + 1; i < ast.quasis.length; i++) {
pattern += ast.quasis[i].value.raw
}
return pattern
}
Expand Down