-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: asset
new URL(,import.meta.url
match
- Loading branch information
1 parent
d7763a5
commit e7fec9c
Showing
2 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)"`, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters