fix(vite): ignore baseurl when pruning manifest (#6063)

This commit is contained in:
Daniel Roe 2022-07-25 10:52:21 +01:00 committed by GitHub
parent 8fe2d7d31a
commit c7d072c495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 7 deletions

View File

@ -94,7 +94,8 @@ export async function buildClient (ctx: ViteBuildContext) {
const viteServer = await vite.createServer(clientConfig) const viteServer = await vite.createServer(clientConfig)
ctx.clientServer = viteServer ctx.clientServer = viteServer
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: true, isServer: false }) 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) => { const viteMiddleware: Connect.NextHandleFunction = (req, res, next) => {
// Workaround: vite devmiddleware modifies req.url // Workaround: vite devmiddleware modifies req.url
const originalURL = req.url const originalURL = req.url

View File

@ -28,7 +28,9 @@ export async function writeManifest (ctx: ViteBuildContext, extraEntries: string
? devClientManifest ? devClientManifest
: await fse.readJSON(resolve(clientDist, 'manifest.json')) : 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) { for (const key in clientManifest) {
if (clientManifest[key].file) { if (clientManifest[key].file) {
clientManifest[key].file = clientManifest[key].file.replace(BASE_RE, '') clientManifest[key].file = clientManifest[key].file.replace(BASE_RE, '')

View File

@ -18,7 +18,7 @@ export async function buildServer (ctx: ViteBuildContext) {
const _resolve = id => resolveModule(id, { paths: ctx.nuxt.options.modulesDir }) const _resolve = id => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, { const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
base: ctx.nuxt.options.dev 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, : undefined,
experimental: { experimental: {
renderBuiltUrl: (filename, { type, hostType }) => { renderBuiltUrl: (filename, { type, hostType }) => {

View File

@ -377,10 +377,32 @@ describe('dynamic paths', () => {
await startServer() await startServer()
const html = await $fetch('/foo/assets') 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)) { for (const match of html.matchAll(/(href|src)="(.*?)"/g)) {
const url = match[2] const url = match[2]
expect(
url.startsWith('./_nuxt/') ||
url === './public.svg' ||
// TODO: webpack does not yet support dynamic static paths // 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() (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') const html = await $fetch('/foo/assets')
for (const match of html.matchAll(/(href|src)="(.*?)"/g)) { for (const match of html.matchAll(/(href|src)="(.*?)"/g)) {
const url = match[2] const url = match[2]
expect(
url.startsWith('https://example.com/_cdn/') ||
url === 'https://example.com/public.svg' ||
// TODO: webpack does not yet support dynamic static paths // 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() (process.env.TEST_WITH_WEBPACK && url === '/public.svg')
).toBeTruthy()
} }
}) })
}) })