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): avoid transforming json file in ssrTransform #6597

Merged
merged 15 commits into from May 10, 2022
25 changes: 25 additions & 0 deletions packages/vite/src/node/ssr/ssrTransform.ts
Expand Up @@ -26,6 +26,31 @@ export const ssrExportAllKey = `__vite_ssr_exportAll__`
export const ssrImportMetaKey = `__vite_ssr_import_meta__`

export async function ssrTransform(
code: string,
inMap: SourceMap | null,
url?: string
bluwy marked this conversation as resolved.
Show resolved Hide resolved
): Promise<TransformResult | null> {
if (url && url.endsWith('.json')) {
return ssrTransformJSON(code, inMap)
}
return ssrTransformScript(code, inMap, url!)
}

// json don't need to parse code it must be `ExportDefaultDeclaration`
// so replace the "export default" to ssr "export default"
async function ssrTransformJSON(
code: string,
inMap: SourceMap | null
): Promise<TransformResult> {
return {
code: code.replace('export default', `${ssrModuleExportsKey}.default =`),
map: inMap,
deps: [],
dynamicDeps: []
}
}

async function ssrTransformScript(
code: string,
inMap: SourceMap | null,
url: string
Expand Down