-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vitest): support assets in new URL in Vite 5 (#4258)
- Loading branch information
1 parent
e7e8c3c
commit d280f48
Showing
5 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
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
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
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,38 @@ | ||
import { stripLiteral } from 'strip-literal' | ||
import type { Plugin } from 'vite' | ||
|
||
const metaUrlLength = 'import.meta.url'.length | ||
const locationString = 'self.location'.padEnd(metaUrlLength, ' ') | ||
|
||
// Vite transforms new URL('./path', import.meta.url) to new URL('/path.js', import.meta.url) | ||
// This makes "href" equal to "http://localhost:3000/path.js" in the browser, but if we keep it like this, | ||
// then in tests the URL will become "file:///path.js". | ||
// To battle this, we replace "import.meta.url" with "self.location" in the code to keep the browser behavior. | ||
export function NormalizeURLPlugin(): Plugin { | ||
return { | ||
name: 'vitest:normalize-url', | ||
enforce: 'post', | ||
transform(code, id, options) { | ||
const ssr = options?.ssr === true | ||
if (ssr || !code.includes('new URL') || !code.includes('import.meta.url')) | ||
return | ||
|
||
const cleanString = stripLiteral(code) | ||
const assetImportMetaUrlRE | ||
= /\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/g | ||
|
||
let updatedCode = code | ||
let match: RegExpExecArray | null | ||
while ((match = assetImportMetaUrlRE.exec(cleanString))) { | ||
const { 0: exp, index } = match | ||
const metaUrlIndex = index + exp.indexOf('import.meta.url') | ||
updatedCode = updatedCode.slice(0, metaUrlIndex) + locationString + updatedCode.slice(metaUrlIndex + metaUrlLength) | ||
} | ||
|
||
return { | ||
code: updatedCode, | ||
map: null, | ||
} | ||
}, | ||
} | ||
} |
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
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