From 16e41234d5de6e38759c7c1547cc971f133eb85b Mon Sep 17 00:00:00 2001 From: pengbo <57180744+PengBoUESTC@users.noreply.github.com> Date: Mon, 7 Nov 2022 17:41:01 +0800 Subject: [PATCH] fix: inconsistent handling of non-ASCII `base` in `resolveConfig` and dev server (#10247) --- packages/vite/src/node/config.ts | 39 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index ca87709cca3627..f42d568a4e7af4 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1,6 +1,6 @@ import fs from 'node:fs' import path from 'node:path' -import { parse as parseUrl, pathToFileURL } from 'node:url' +import { pathToFileURL } from 'node:url' import { performance } from 'node:perf_hooks' import { createRequire } from 'node:module' import colors from 'picocolors' @@ -808,33 +808,34 @@ export function resolveBaseUrl( ) ) ) - base = '/' + return '/' } - // external URL - if (isExternalUrl(base)) { - if (!isBuild) { - // get base from full url during dev - const parsed = parseUrl(base) - base = parsed.pathname || '/' - } - } else { + // external URL flag + const isExternal = isExternalUrl(base) + // no leading slash warn + if (!isExternal && !base.startsWith('/')) { + logger.warn( + colors.yellow(colors.bold(`(!) "base" option should start with a slash.`)) + ) + } + // no ending slash warn + if (!base.endsWith('/')) { + logger.warn( + colors.yellow(colors.bold(`(!) "base" option should end with a slash.`)) + ) + } + + // parse base when command is serve or base is not External URL + if (!isBuild || !isExternal) { + base = new URL(base, 'http://vitejs.dev').pathname // ensure leading slash if (!base.startsWith('/')) { - logger.warn( - colors.yellow( - colors.bold(`(!) "base" option should start with a slash.`) - ) - ) base = '/' + base } } - // ensure ending slash if (!base.endsWith('/')) { - logger.warn( - colors.yellow(colors.bold(`(!) "base" option should end with a slash.`)) - ) base += '/' }