From c7d072c4957ef47151302252874e22445e0b806b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 25 Jul 2022 10:52:21 +0100 Subject: [PATCH] fix(vite): ignore baseurl when pruning manifest (#6063) --- packages/vite/src/client.ts | 3 ++- packages/vite/src/manifest.ts | 4 +++- packages/vite/src/server.ts | 2 +- test/basic.test.ts | 34 ++++++++++++++++++++++++++++++---- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/client.ts b/packages/vite/src/client.ts index 3f38ba83a0..751844e53e 100644 --- a/packages/vite/src/client.ts +++ b/packages/vite/src/client.ts @@ -94,7 +94,8 @@ export async function buildClient (ctx: ViteBuildContext) { const viteServer = await vite.createServer(clientConfig) ctx.clientServer = viteServer await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: true, isServer: false }) - const BASE_RE = new RegExp(`^${escapeRE(withTrailingSlash(withLeadingSlash(joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir))))}`) + const baseURL = joinURL(ctx.nuxt.options.app.baseURL.replace(/^\./, '') || '/', ctx.nuxt.options.app.buildAssetsDir) + const BASE_RE = new RegExp(`^${escapeRE(withTrailingSlash(withLeadingSlash(baseURL)))}`) const viteMiddleware: Connect.NextHandleFunction = (req, res, next) => { // Workaround: vite devmiddleware modifies req.url const originalURL = req.url diff --git a/packages/vite/src/manifest.ts b/packages/vite/src/manifest.ts index fdd705e410..f9236fffad 100644 --- a/packages/vite/src/manifest.ts +++ b/packages/vite/src/manifest.ts @@ -28,7 +28,9 @@ export async function writeManifest (ctx: ViteBuildContext, extraEntries: string ? devClientManifest : await fse.readJSON(resolve(clientDist, 'manifest.json')) - const BASE_RE = new RegExp(`^${escapeRE(withTrailingSlash(withoutLeadingSlash(joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir))))}`) + const buildAssetsDir = withTrailingSlash(withoutLeadingSlash(ctx.nuxt.options.app.buildAssetsDir)) + const BASE_RE = new RegExp(`^${escapeRE(buildAssetsDir)}`) + for (const key in clientManifest) { if (clientManifest[key].file) { clientManifest[key].file = clientManifest[key].file.replace(BASE_RE, '') diff --git a/packages/vite/src/server.ts b/packages/vite/src/server.ts index 0e76e49aef..d31af33434 100644 --- a/packages/vite/src/server.ts +++ b/packages/vite/src/server.ts @@ -18,7 +18,7 @@ export async function buildServer (ctx: ViteBuildContext) { const _resolve = id => resolveModule(id, { paths: ctx.nuxt.options.modulesDir }) const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, { base: ctx.nuxt.options.dev - ? joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir) + ? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir) : undefined, experimental: { renderBuiltUrl: (filename, { type, hostType }) => { diff --git a/test/basic.test.ts b/test/basic.test.ts index b4fcb492bf..fefab873f5 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -377,10 +377,32 @@ describe('dynamic paths', () => { await startServer() const html = await $fetch('/foo/assets') + for (const match of html.matchAll(/(href|src)="(.`*?)"/g)) { + const url = match[2] + expect( + url.startsWith('/foo/_other/') || + url === '/foo/public.svg' || + // TODO: webpack does not yet support dynamic static paths + (process.env.TEST_WITH_WEBPACK && url === '/public.svg') + ).toBeTruthy() + } + }) + + it('should allow setting relative baseURL', async () => { + delete process.env.NUXT_APP_BUILD_ASSETS_DIR + process.env.NUXT_APP_BASE_URL = './' + await startServer() + + const html = await $fetch('/assets') for (const match of html.matchAll(/(href|src)="(.*?)"/g)) { const url = match[2] - // TODO: webpack does not yet support dynamic static paths - expect(url.startsWith('/foo/_other/') || url === '/foo/public.svg' || (process.env.TEST_WITH_WEBPACK && url === '/public.svg')).toBeTruthy() + expect( + url.startsWith('./_nuxt/') || + url === './public.svg' || + // TODO: webpack does not yet support dynamic static paths + (process.env.TEST_WITH_WEBPACK && url === '/public.svg') + ).toBeTruthy() + expect(url.startsWith('./_nuxt/_nuxt')).toBeFalsy() } }) @@ -402,8 +424,12 @@ describe('dynamic paths', () => { const html = await $fetch('/foo/assets') for (const match of html.matchAll(/(href|src)="(.*?)"/g)) { const url = match[2] - // TODO: webpack does not yet support dynamic static paths - expect(url.startsWith('https://example.com/_cdn/') || url === 'https://example.com/public.svg' || (process.env.TEST_WITH_WEBPACK && url === '/public.svg')).toBeTruthy() + expect( + url.startsWith('https://example.com/_cdn/') || + url === 'https://example.com/public.svg' || + // TODO: webpack does not yet support dynamic static paths + (process.env.TEST_WITH_WEBPACK && url === '/public.svg') + ).toBeTruthy() } }) })