Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): load sourcemaps alongside modules #11780

Merged
merged 8 commits into from
Apr 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(ssr): load sourcemaps alongside modules (fix: #3288) (#11576)
Vap0r1ze authored and sapphi-red committed Jan 21, 2023
commit cba08056fa2446713e3d01607e69b2956b1eff4b
27 changes: 24 additions & 3 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import {
import { transformRequest } from '../server/transformRequest'
import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve'
import { tryNodeResolve } from '../plugins/resolve'
import { genSourceMapUrl } from '../server/sourcemap'
import {
ssrDynamicImportKey,
ssrExportAllKey,
@@ -25,6 +26,16 @@ interface SSRContext {

type SSRModule = Record<string, any>

// eslint-disable-next-line @typescript-eslint/no-empty-function
const AsyncFunction = async function () {}.constructor as typeof Function
let fnDeclarationLineCount = 0
{
const body = '/*code*/'
const source = new AsyncFunction('a', 'b', body).toString()
fnDeclarationLineCount =
source.slice(0, source.indexOf(body)).split('\n').length - 1
}

const pendingModules = new Map<string, Promise<SSRModule>>()
const pendingImports = new Map<string, string[]>()

@@ -181,17 +192,27 @@ async function instantiateModule(
}
}

let sourceMapSuffix = ''
if (result.map) {
const moduleSourceMap = Object.assign({}, result.map, {
// offset the first three lines of the module (function declaration and 'use strict')
mappings: ';'.repeat(fnDeclarationLineCount + 1) + result.map.mappings,
})
sourceMapSuffix =
'\n//# sourceMappingURL=' + genSourceMapUrl(moduleSourceMap)
}

try {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const AsyncFunction = async function () {}.constructor as typeof Function
const initModule = new AsyncFunction(
`global`,
ssrModuleExportsKey,
ssrImportMetaKey,
ssrImportKey,
ssrDynamicImportKey,
ssrExportAllKey,
'"use strict";' + result.code + `\n//# sourceURL=${mod.url}`,
'"use strict";\n' +
result.code +
`\n//# sourceURL=${mod.url}${sourceMapSuffix}`,
)
await initModule(
context.global,