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

Function defineImport

packages/vite/src/node/ssr/ssrTransform.ts:97–135  ·  view source on GitHub ↗
(
    index: number,
    importNode: (
      | ESTree.ImportDeclaration
      | (ESTree.ExportNamedDeclaration & { source: ESTree.StringLiteral })
      | ESTree.ExportAllDeclaration
    ) & {
      start: number
      end: number
    },
    metadata?: DefineImportMetadata,
  )

Source from the content-addressed store, hash-verified

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 : ''
117
118 const importId = `__vite_ssr_import_${uid++}__`
119 const transformedImport = `const ${importId} = await ${ssrImportKey}(${JSON.stringify(
120 source,
121 )}${metadataArg});\n`
122
123 s.update(importNode.start, importNode.end, transformedImport)
124
125 if (importNode.start === index) {
126 // no need to hoist, but update hoistIndex to keep the order
127 hoistIndex = importNode.end
128 } else {
129 // There will be an error if the module is called before it is imported,
130 // so the module import statement is hoisted to the top
131 s.move(importNode.start, importNode.end, index)
132 }
133
134 return importId
135 }
136
137 function defineExport(name: string, local = name) {
138 // wrap with try/catch to fallback to `undefined` for backward compat.

Callers 1

ssrTransformScriptFunction · 0.85

Calls 1

addMethod · 0.80

Tested by

no test coverage detected