mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
feat(vite): preserve vite sourcemaps for nitro build (#28521)
This commit is contained in:
parent
14f2b5b31f
commit
9397def672
63
packages/vite/src/plugins/nitro-sourcemap.ts
Normal file
63
packages/vite/src/plugins/nitro-sourcemap.ts
Normal 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,
|
||||
}
|
||||
}
|
@ -5,11 +5,13 @@ import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
|
||||
import { logger, resolvePath, tryResolveModule } from '@nuxt/kit'
|
||||
import { joinURL, withTrailingSlash, withoutLeadingSlash } from 'ufo'
|
||||
import type { ViteConfig } from '@nuxt/schema'
|
||||
import defu from 'defu'
|
||||
import type { ViteBuildContext } from './vite'
|
||||
import { createViteLogger } from './utils/logger'
|
||||
import { initViteNodeServer } from './vite-node'
|
||||
import { writeManifest } from './manifest'
|
||||
import { transpile } from './utils/transpile'
|
||||
import { createSourcemapPreserver } from './plugins/nitro-sourcemap'
|
||||
|
||||
export async function buildServer (ctx: ViteBuildContext) {
|
||||
const helper = ctx.nuxt.options.nitro.imports !== false ? '' : 'globalThis.'
|
||||
@ -120,6 +122,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)
|
||||
|
||||
await ctx.nuxt.callHook('vite:extendConfig', serverConfig, { isClient: false, isServer: true })
|
||||
|
Loading…
Reference in New Issue
Block a user