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

Function genFunctionPreamble

packages/compiler-core/src/codegen.ts:408–468  ·  packages/compiler-core/src/codegen.ts::genFunctionPreamble
(ast: RootNode, context: CodegenContext)

Source from the content-addressed store, hash-verified

406}
407
408function genFunctionPreamble(ast: RootNode, context: CodegenContext) {
409 const {
410 ssr,
411 prefixIdentifiers,
412 push,
413 newline,
414 runtimeModuleName,
415 runtimeGlobalName,
416 ssrRuntimeModuleName,
417 } = context
418 const VueBinding =
419 !__BROWSER__ && ssr
420 ? `require(${JSON.stringify(runtimeModuleName)})`
421 : runtimeGlobalName
422 class="cm">// Generate const declaration for helpers
423 class="cm">// In prefix mode, we place the const declaration at top so it's done
424 class="cm">// only once; But if we not prefixing, we place the declaration inside the
425 class="cm">// with block so it doesn't incur the `in` check cost for every helper access.
426 const helpers = Array.from(ast.helpers)
427 if (helpers.length > 0) {
428 if (!__BROWSER__ && prefixIdentifiers) {
429 push(
430 `const { ${helpers.map(aliasHelper).join(class="st">', ')} } = ${VueBinding}\n`,
431 NewlineType.End,
432 )
433 } else {
434 class="cm">// class="st">"with" mode.
435 class="cm">// save Vue in a separate variable to avoid collision
436 push(`const _Vue = ${VueBinding}\n`, NewlineType.End)
437 class="cm">// in class="st">"with" mode, helpers are declared inside the with block to avoid
438 class="cm">// has check cost, but hoists are lifted out of the function - we need
439 class="cm">// to provide the helper here.
440 if (ast.hoists.length) {
441 const staticHelpers = [
442 CREATE_VNODE,
443 CREATE_ELEMENT_VNODE,
444 CREATE_COMMENT,
445 CREATE_TEXT,
446 CREATE_STATIC,
447 ]
448 .filter(helper => helpers.includes(helper))
449 .map(aliasHelper)
450 .join(class="st">', ')
451 push(`const { ${staticHelpers} } = _Vue\n`, NewlineType.End)
452 }
453 }
454 }
455 class="cm">// generate variables for ssr helpers
456 if (!__BROWSER__ && ast.ssrHelpers && ast.ssrHelpers.length) {
457 class="cm">// ssr guarantees prefixIdentifier: true
458 push(
459 `const { ${ast.ssrHelpers
460 .map(aliasHelper)
461 .join(class="st">', ')} } = require(class="st">"${ssrRuntimeModuleName}")\n`,
462 NewlineType.End,
463 )
464 }
465 genHoists(ast.hoists, context)

Callers 1

generateFunction · 0.85

Calls 5

genHoistsFunction · 0.85
newlineFunction · 0.85
mapMethod · 0.80
pushFunction · 0.70
filterMethod · 0.65

Tested by

no test coverage detected