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 (fix: #3288) #11576

Merged
merged 6 commits into from
Jan 16, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ 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: ';;;' + result.map.mappings,
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
})
// The ${'//'} is to avoid being replaced by node/server/transformRequest
Vap0r1ze marked this conversation as resolved.
Show resolved Hide resolved
sourceMapSuffix = `\n${'//'}# sourceMappingURL=data:application/json;charset=utf-8;base64,${Buffer.from(
JSON.stringify(moduleSourceMap),
).toString('base64')}`
Vap0r1ze marked this conversation as resolved.
Show resolved Hide resolved
Vap0r1ze marked this conversation as resolved.
Show resolved Hide resolved
}

try {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const AsyncFunction = async function () {}.constructor as typeof Function
Expand All @@ -191,7 +203,9 @@ async function instantiateModule(
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,
Expand Down