From 1d7623e19244c8e4a33ca170b00d0f73e4e14d40 Mon Sep 17 00:00:00 2001 From: GrygrFlzr Date: Tue, 9 Mar 2021 00:11:50 +0700 Subject: [PATCH] fix: Improve injectQuery path handling close #2422 --- packages/vite/src/node/utils.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 892d27fec1e72d..482e344ad5f0f1 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -3,7 +3,7 @@ import chalk from 'chalk' import fs from 'fs' import os from 'os' import path from 'path' -import { parse as parseUrl } from 'url' +import { pathToFileURL, URL } from 'url' import { FS_PREFIX, DEFAULT_EXTENSIONS, VALID_ID_PREFIX } from './constants' import resolve from 'resolve' import builtins from 'builtin-modules' @@ -119,7 +119,14 @@ export function removeImportQuery(url: string) { } export function injectQuery(url: string, queryToInject: string) { - const { pathname, search, hash } = parseUrl(url) + let resolvedUrl = new URL(url, 'relative:///') + if (resolvedUrl.protocol !== 'relative:') { + resolvedUrl = pathToFileURL(url) + } + let { protocol, pathname, search, hash } = resolvedUrl + if (protocol === 'file:') { + pathname = pathname.slice(1) + } return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${ hash || '' }`