feat(vite): preserve vite sourcemaps for nitro build (#28521)

This commit is contained in:
Daniel Roe 2024-08-13 17:01:13 +01:00 committed by GitHub
parent e52dff0596
commit 7f199d014f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,63 @@
import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { dirname, resolve } from 'pathe'
import type { Plugin as RollupPlugin } from 'rollup'
import type { Plugin as VitePlugin } from 'vite'
export const createSourcemapPreserver = () => {
let outputDir: string
const ids = new Set<string>()
const vitePlugin = {
name: 'nuxt:sourcemap-export',
configResolved (config) {
outputDir = config.build.outDir
},
async writeBundle (_options, bundle) {
for (const chunk of Object.values(bundle)) {
if (chunk.type !== 'chunk' || !chunk.map) { continue }
const id = resolve(outputDir, chunk.fileName)
ids.add(id)
const dest = id + '.map.json'
await mkdir(dirname(dest), { recursive: true })
await writeFile(dest, JSON.stringify({
file: chunk.map.file,
mappings: chunk.map.mappings,
names: chunk.map.names,
sources: chunk.map.sources,
sourcesContent: chunk.map.sourcesContent,
version: chunk.map.version,
}))
}
},
} satisfies VitePlugin
const nitroPlugin = {
name: 'nuxt:sourcemap-import',
async load (id) {
id = resolve(id)
if (!ids.has(id)) { return }
const [code, map] = await Promise.all([
readFile(id, 'utf-8').catch(() => undefined),
readFile(id + '.map.json', 'utf-8').catch(() => undefined),
])
if (!code) {
this.warn('Failed loading file')
return null
}
return {
code,
map,
}
},
} satisfies RollupPlugin
return {
vitePlugin,
nitroPlugin,
}
}

View File

@ -5,11 +5,13 @@ import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
import { logger, resolvePath, tryImportModule } from '@nuxt/kit' import { logger, resolvePath, tryImportModule } from '@nuxt/kit'
import { joinURL, withTrailingSlash, withoutLeadingSlash } from 'ufo' import { joinURL, withTrailingSlash, withoutLeadingSlash } from 'ufo'
import type { ViteConfig } from '@nuxt/schema' import type { ViteConfig } from '@nuxt/schema'
import defu from 'defu'
import type { ViteBuildContext } from './vite' import type { ViteBuildContext } from './vite'
import { createViteLogger } from './utils/logger' import { createViteLogger } from './utils/logger'
import { initViteNodeServer } from './vite-node' import { initViteNodeServer } from './vite-node'
import { writeManifest } from './manifest' import { writeManifest } from './manifest'
import { transpile } from './utils/transpile' import { transpile } from './utils/transpile'
import { createSourcemapPreserver } from './plugins/nitro-sourcemap'
export async function buildServer (ctx: ViteBuildContext) { export async function buildServer (ctx: ViteBuildContext) {
const helper = ctx.nuxt.options.nitro.imports !== false ? '' : 'globalThis.' const helper = ctx.nuxt.options.nitro.imports !== false ? '' : 'globalThis.'
@ -121,6 +123,17 @@ export async function buildServer (ctx: ViteBuildContext) {
} }
} }
// tell rollup's nitro build about the original sources of the generated vite server build
if (ctx.nuxt.options.sourcemap.server && !ctx.nuxt.options.dev) {
const { vitePlugin, nitroPlugin } = createSourcemapPreserver()
serverConfig.plugins!.push(vitePlugin)
ctx.nuxt.hook('nitro:build:before', (nitro) => {
nitro.options.rollupConfig = defu(nitro.options.rollupConfig, {
plugins: [nitroPlugin],
})
})
}
serverConfig.customLogger = createViteLogger(serverConfig) serverConfig.customLogger = createViteLogger(serverConfig)
await ctx.nuxt.callHook('vite:extendConfig', serverConfig, { isClient: false, isServer: true }) await ctx.nuxt.callHook('vite:extendConfig', serverConfig, { isClient: false, isServer: true })