From 4b72208e31bb4ee8c89e613922cf6ae7ca0daed2 Mon Sep 17 00:00:00 2001 From: pengboUESTC Date: Mon, 26 Sep 2022 10:48:38 +0800 Subject: [PATCH 1/3] feat: format config.base --- packages/vite/src/node/config.ts | 40 +++++++++++++++----------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 46ee9476677c2d..72e33601f8d21d 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' @@ -807,33 +807,31 @@ 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 { - // ensure leading slash - if (!base.startsWith('/')) { - logger.warn( - colors.yellow( - colors.bold(`(!) "base" option should start with a slash.`) - ) - ) - base = '/' + base - } + // leading slash warn + if (!base.startsWith('/')) { + logger.warn( + colors.yellow(colors.bold(`(!) "base" option should start with a slash.`)) + ) } - - // ensure ending slash + // ending slash warn if (!base.endsWith('/')) { logger.warn( colors.yellow(colors.bold(`(!) "base" option should end with a slash.`)) ) + } + + if (!isBuild || !isExternalUrl(base)) { + base = new URL(base, 'http://vitejs.dev').pathname + } + // ensure leading slash + if (!base.startsWith('/')) { + base = '/' + base + } + // ensure ending slash + if (!base.endsWith('/')) { base += '/' } From f0a329bbd0d9dc639324133b339e47f14a9d1967 Mon Sep 17 00:00:00 2001 From: pengboUESTC Date: Mon, 26 Sep 2022 10:52:53 +0800 Subject: [PATCH 2/3] chore: comment --- packages/vite/src/node/config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 72e33601f8d21d..ebece3d3fe1bf1 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -810,13 +810,13 @@ export function resolveBaseUrl( return '/' } - // leading slash warn + // no leading slash warn if (!base.startsWith('/')) { logger.warn( colors.yellow(colors.bold(`(!) "base" option should start with a slash.`)) ) } - // ending slash warn + // no ending slash warn if (!base.endsWith('/')) { logger.warn( colors.yellow(colors.bold(`(!) "base" option should end with a slash.`)) From 07eb8f6f07a39d8d2a6190ad18d24c48efd839a4 Mon Sep 17 00:00:00 2001 From: pengboUESTC Date: Thu, 13 Oct 2022 20:23:46 +0800 Subject: [PATCH 3/3] feat: external url --- packages/vite/src/node/config.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 07c680e247a5a7..88be3e5e505303 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -816,8 +816,10 @@ export function resolveBaseUrl( return '/' } + // external URL flag + const isExternal = isExternalUrl(base) // no leading slash warn - if (!base.startsWith('/')) { + if (!isExternal && !base.startsWith('/')) { logger.warn( colors.yellow(colors.bold(`(!) "base" option should start with a slash.`)) ) @@ -829,12 +831,13 @@ export function resolveBaseUrl( ) } - if (!isBuild || !isExternalUrl(base)) { + // 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('/')) { - base = '/' + base + // ensure leading slash + if (!base.startsWith('/')) { + base = '/' + base + } } // ensure ending slash if (!base.endsWith('/')) {