Skip to content

Commit

Permalink
fix: import.meta.url should not throw (#7219)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Mar 8, 2022
1 parent dc46adf commit 5de3a98
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -19,7 +19,11 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
return {
name: 'vite:asset-import-meta-url',
async transform(code, id, options) {
if (code.includes('new URL') && code.includes(`import.meta.url`)) {
if (
!options?.ssr &&
code.includes('new URL') &&
code.includes(`import.meta.url`)
) {
const importMetaUrlRE =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*,?\s*\)/g
const noCommentsCode = code
Expand All @@ -30,13 +34,6 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
while ((match = importMetaUrlRE.exec(noCommentsCode))) {
const { 0: exp, 1: rawUrl, index } = match

if (options?.ssr) {
this.error(
`\`new URL(url, import.meta.url)\` is not supported in SSR.`,
index
)
}

if (!s) s = new MagicString(code)

// potential dynamic template string
Expand Down

0 comments on commit 5de3a98

Please sign in to comment.