MCPcopy
hub / github.com/vitejs/vite / ssrTransformScript

Function ssrTransformScript

packages/vite/src/node/ssr/ssrTransform.ts:59–424  ·  view source on GitHub ↗
(
  code: string,
  inMap: SourceMap | { mappings: '' } | null,
  url: string,
  originalCode: string,
)

Source from the content-addressed store, hash-verified

57}
58
59async function ssrTransformScript(
60 code: string,
61 inMap: SourceMap | { mappings: '' } | null,
62 url: string,
63 originalCode: string,
64): Promise<TransformResult | null> {
65 const s = new MagicString(code)
66
67 let ast: ESTree.Program
68 try {
69 ast = await rolldownParseAstAsync(code)
70 } catch (err) {
71 // enhance known rollup errors
72 // https://github.com/rollup/rollup/blob/42e587e0e37bc0661aa39fe7ad6f1d7fd33f825c/src/utils/bufferToAst.ts#L17-L22
73 if (err.code === 'PARSE_ERROR') {
74 err.message = `Parse failure: ${err.message}\n`
75 err.id = url
76 if (typeof err.pos === 'number') {
77 err.loc = numberToPos(code, err.pos)
78 err.loc.file = url
79 err.frame = generateCodeFrame(code, err.pos)
80 err.message += `At file: ${url}:${err.loc.line}:${err.loc.column}`
81 } else {
82 err.message += `At file: ${url}`
83 }
84 }
85 throw err
86 }
87
88 let uid = 0
89 const deps = new Set<string>()
90 const dynamicDeps = new Set<string>()
91 const idToImportMap = new Map<string, string>()
92 const declaredConst = new Set<string>()
93
94 const fileStartIndex = getFileStartIndex(code)
95 let hoistIndex = fileStartIndex
96
97 function defineImport(
98 index: number,
99 importNode: (
100 | ESTree.ImportDeclaration
101 | (ESTree.ExportNamedDeclaration & { source: ESTree.StringLiteral })
102 | ESTree.ExportAllDeclaration
103 ) & {
104 start: number
105 end: number
106 },
107 metadata?: DefineImportMetadata,
108 ) {
109 const source = importNode.source.value
110 deps.add(source)
111
112 // Reduce metadata to undefined if it's all default values
113 const metadataArg =
114 (metadata?.importedNames?.length ?? 0) > 0
115 ? `, ${JSON.stringify(metadata)}`
116 : ''

Callers 1

ssrTransformFunction · 0.85

Calls 10

numberToPosFunction · 0.90
generateCodeFrameFunction · 0.90
getFileStartIndexFunction · 0.90
combineSourcemapsFunction · 0.90
defineImportFunction · 0.85
defineExportFunction · 0.85
walkFunction · 0.85
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected