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

Function generateGlueCode

packages/vite/src/node/plugins/wasm.ts:217–276  ·  view source on GitHub ↗
(
  wasmInfo: WasmInfo,
  names: { initWasm: string; wasmUrl: string },
)

Source from the content-addressed store, hash-verified

215}
216
217function generateGlueCode(
218 wasmInfo: WasmInfo,
219 names: { initWasm: string; wasmUrl: string },
220): string {
221 const importStatements = wasmInfo.imports.map(({ from }, i) => {
222 return `import * as __vite__wasmImport_${i} from ${JSON.stringify(from)};`
223 })
224
225 const importObject: SimpleObject = wasmInfo.imports.map(
226 ({ from, names: importNames }, i) => {
227 return {
228 key: JSON.stringify(from),
229 value: importNames.map((name) => {
230 return {
231 key: JSON.stringify(name),
232 value: `__vite__wasmImport_${i}[${JSON.stringify(name)}]`,
233 }
234 }),
235 }
236 },
237 )
238
239 const initCode = `const __vite__wasmModule = (await ${names.initWasm}(${codegenSimpleObject(importObject)}, ${names.wasmUrl})).exports;`
240
241 if (wasmInfo.exports.length === 0) {
242 return [...importStatements, initCode].join('\n')
243 }
244
245 const exportStatements: string[] = []
246 const nameMap = new Map<string, string>()
247 for (const [index, name] of wasmInfo.exports.entries()) {
248 if (isValidJsDeclareName(name)) {
249 exportStatements.push(` ${name},`)
250 } else {
251 const placeholderName = `__vite__wasmExport_${index}`
252 exportStatements.push(` ${JSON.stringify(name)}: ${placeholderName},`)
253 nameMap.set(name, placeholderName)
254 }
255 }
256
257 if (nameMap.size > 0) {
258 exportStatements.unshift(`const {`)
259 exportStatements.push(`} = __vite__wasmModule;`)
260 exportStatements.push(`export {`)
261 for (const name of wasmInfo.exports) {
262 const localName = nameMap.get(name)
263 if (localName) {
264 exportStatements.push(` ${localName} as ${JSON.stringify(name)},`)
265 } else {
266 exportStatements.push(` ${name},`)
267 }
268 }
269 exportStatements.push(`};`)
270 } else {
271 exportStatements.unshift(`export const {`)
272 exportStatements.push(`} = __vite__wasmModule;`)
273 }
274

Callers 1

handlerFunction · 0.85

Calls 4

codegenSimpleObjectFunction · 0.85
isValidJsDeclareNameFunction · 0.85
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected