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

Function genFunctionPreamble

packages/compiler-core/src/codegen.ts:408–468  ·  view source on GitHub ↗
(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 // Generate const declaration for helpers
423 // In prefix mode, we place the const declaration at top so it's done
424 // only once; But if we not prefixing, we place the declaration inside the
425 // 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(', ')} } = ${VueBinding}\n`,
431 NewlineType.End,
432 )
433 } else {
434 // "with" mode.
435 // save Vue in a separate variable to avoid collision
436 push(`const _Vue = ${VueBinding}\n`, NewlineType.End)
437 // in "with" mode, helpers are declared inside the with block to avoid
438 // has check cost, but hoists are lifted out of the function - we need
439 // 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(', ')
451 push(`const { ${staticHelpers} } = _Vue\n`, NewlineType.End)
452 }
453 }
454 }
455 // generate variables for ssr helpers
456 if (!__BROWSER__ && ast.ssrHelpers && ast.ssrHelpers.length) {
457 // ssr guarantees prefixIdentifier: true
458 push(
459 `const { ${ast.ssrHelpers
460 .map(aliasHelper)
461 .join(', ')} } = require("${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