MCPcopy
hub / github.com/vuejs/core / compileScript

Function compileScript

packages/compiler-sfc/src/compileScript.ts:168–1119  ·  view source on GitHub ↗
(
  sfc: SFCDescriptor,
  options: SFCScriptCompileOptions,
)

Source from the content-addressed store, hash-verified

166 * normal `<script>` + `<script setup>` if both are present.
167 */
168export function compileScript(
169 sfc: SFCDescriptor,
170 options: SFCScriptCompileOptions,
171): SFCScriptBlock {
172 if (!options.id) {
173 warnOnce(
174 `compileScript now requires passing the \`id\` option.\n` +
175 `Upgrade your vite or vue-loader version for compatibility with ` +
176 `the latest experimental proposals.`,
177 )
178 }
179
180 const { script, scriptSetup, source, filename } = sfc
181 const hoistStatic = options.hoistStatic !== false && !script
182 const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
183 const scriptLang = script && script.lang
184 const scriptSetupLang = scriptSetup && scriptSetup.lang
185 const isJSOrTS =
186 isJS(scriptLang, scriptSetupLang) || isTS(scriptLang, scriptSetupLang)
187
188 if (script && scriptSetup && scriptLang !== scriptSetupLang) {
189 throw new Error(
190 `[@vue/compiler-sfc] <script> and <script setup> must have the same ` +
191 `language type.`,
192 )
193 }
194
195 if (!scriptSetup) {
196 if (!script) {
197 throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
198 }
199
200 // normal <script> only
201 if (script.lang && !isJSOrTS) {
202 // do not process non js/ts script blocks
203 return script
204 }
205
206 const ctx = new ScriptCompileContext(sfc, options)
207 return processNormalScript(ctx, scopeId)
208 }
209
210 if (scriptSetupLang && !isJSOrTS) {
211 // do not process non js/ts script blocks
212 return scriptSetup
213 }
214
215 const ctx = new ScriptCompileContext(sfc, options)
216
217 // metadata that needs to be returned
218 // const ctx.bindingMetadata: BindingMetadata = {}
219 const scriptBindings: Record<string, BindingTypes> = Object.create(null)
220 const setupBindings: Record<string, BindingTypes> = Object.create(null)
221
222 let defaultExport: Node | undefined
223 let hasAwait = false
224 let hasInlinedSsrRenderFn = false
225

Callers 2

compileSFCScriptFunction · 0.90

Calls 15

errorMethod · 0.95
helperMethod · 0.95
warnOnceFunction · 0.90
isJSFunction · 0.90
isTSFunction · 0.90
processNormalScriptFunction · 0.90
getImportedNameFunction · 0.90
processDefinePropsFunction · 0.90
processDefineEmitsFunction · 0.90
processDefineOptionsFunction · 0.90
processDefineSlotsFunction · 0.90
processDefineExposeFunction · 0.90

Tested by 1

compileSFCScriptFunction · 0.72