Skip to content

Commit

Permalink
fix: asset new URL(,import.meta.url match
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Sep 25, 2024
1 parent d7763a5 commit e7fec9c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
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)"`,
)
})

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

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

0 comments on commit e7fec9c

Please sign in to comment.