[{"data":1,"prerenderedAt":1097,"buildId":1098},["ShallowReactive",2],{"page:\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fcontext":3},{"id":4,"title":5,"body":6,"description":1079,"extension":1080,"frontmatter":1081,"heading":1086,"meta":1088,"navigation":1090,"order":1084,"path":1091,"seo":1092,"sitemap":1093,"source":1094,"stem":1095,"__hash__":1096},"docs\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fcontext.md","Context - this, ctx and useKubb() in every hook",{"type":7,"value":8,"toc":1066},"minimark",[9,34,39,48,218,227,295,299,308,455,463,762,767,778,784,812,886,889,968,972,991],[10,11,12,13,17,18,21,22,25,26,29,30,33],"p",{},"Kubb runs every plugin hook with one context object. In each hook, ",[14,15,16],"code",{},"this",", the ",[14,19,20],{},"ctx"," argument and ",[14,23,24],{},"useKubb()"," are that same object, also after an ",[14,27,28],{},"await",". Composables read it, so they work in every hook, not only in ",[14,31,32],{},"setup",".",[35,36,38],"h2",{"id":37},"where-code-runs","Where code runs",[10,40,41,42,44,45,47],{},"The context depends on where your code runs. Printer handlers and macro callbacks are the exceptions: they get their own small context as ",[14,43,16],{}," and as their second argument, while ",[14,46,24],{}," keeps returning the context of the hook that runs them.",[49,50,51,73],"table",{},[52,53,54],"thead",{},[55,56,57,61,68],"tr",{},[58,59,60],"th",{},"Code",[58,62,63,65,66],{},[14,64,16],{}," and ",[14,67,20],{},[58,69,70,72],{},[14,71,24],{}," returns",[74,75,76,91,117,131,153,171,181,201],"tbody",{},[55,77,78,86,89],{},[79,80,81,65,83],"td",{},[14,82,32],{},[14,84,85],{},"kubb:plugin:setup",[79,87,88],{},"The setup context",[79,90,88],{},[55,92,93,112,115],{},[79,94,95,96,99,100,99,103,99,106,99,109],{},"Events Kubb fires: ",[14,97,98],{},"kubb:build:start",", ",[14,101,102],{},"kubb:plugin:start",[14,104,105],{},"kubb:plugin:end",[14,107,108],{},"kubb:plugins:end",[14,110,111],{},"kubb:build:end",[79,113,114],{},"The plugin's own visit context",[79,116,114],{},[55,118,119,125,128],{},[79,120,121,122],{},"Events fired inside a hook, with ",[14,123,124],{},"ctx.hooks.callHook(...)",[79,126,127],{},"The context of the hook that fired the event",[79,129,130],{},"The context of the hook that fired it",[55,132,133,148,151],{},[79,134,135,136,99,139,99,142,99,145],{},"Visit hooks: ",[14,137,138],{},"kubb:plugin:visit:start",[14,140,141],{},":schema",[14,143,144],{},":operation",[14,146,147],{},":end",[79,149,150],{},"The plugin's visit context",[79,152,150],{},[55,154,155,166,169],{},[79,156,157,158,161,162,165],{},"Template ",[14,159,160],{},"render"," and the ",[14,163,164],{},"kubb:generate:*"," events",[79,167,168],{},"The template context",[79,170,168],{},[55,172,173,176,179],{},[79,174,175],{},"The printer factory",[79,177,178],{},"The printer context",[79,180,178],{},[55,182,183,190,199],{},[79,184,185],{},[186,187,189],"a",{"href":188},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fast#printer-handlers-and-context","Printer handlers",[79,191,192,193,65,196],{},"The printer context, with ",[14,194,195],{},"transform",[14,197,198],{},"base",[79,200,168],{},[55,202,203,209,215],{},[79,204,205],{},[186,206,208],{"href":207},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fast#macro-options-and-callbacks","Macro callbacks",[79,210,211,212],{},"The walk context, with the node's ",[14,213,214],{},"parent",[79,216,217],{},"The visit context of the running hook",[10,219,220,221,223,224,33],{},"A plugin has one visit context per build. Its bus hooks and its visit hooks get the same object, so a value you set on it in ",[14,222,98],{}," is there in ",[14,225,226],{},"kubb:plugin:visit:schema",[228,229,230,249,276],"ul",{},[231,232,233,234,238,239,99,242,99,245,248],"li",{},"The ",[186,235,237],{"href":236},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fplugins#setup-context","setup context"," holds ",[14,240,241],{},"options",[14,243,244],{},"setOptions",[14,246,247],{},"injectFile"," and the members every context has.",[231,250,251,252,99,255,99,258,99,261,99,264,99,267,270,271,275],{},"The visit context holds ",[14,253,254],{},"config",[14,256,257],{},"plugin",[14,259,260],{},"memo",[14,262,263],{},"hooks",[14,265,266],{},"requirePlugin",[14,268,269],{},"upsertFile"," and the other ",[186,272,274],{"href":273},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fhooks#visit-hooks","visit hook"," members.",[231,277,233,278,282,283,99,286,289,290,65,292,33],{},[186,279,281],{"href":280},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Ftemplates#templatecontext","template context"," is the visit context plus ",[14,284,285],{},"resolver",[14,287,288],{},"getResolver(name)",", per-node ",[14,291,241],{},[14,293,294],{},"printer",[35,296,298],{"id":297},"composables","Composables",[10,300,301,302,304,305,307],{},"Use composables for plugin-level concepts in ",[14,303,32],{},", and read ",[14,306,20],{}," for the rest.",[49,309,310,320],{},[52,311,312],{},[55,313,314,317],{},[58,315,316],{},"Composable",[58,318,319],{},"Works in",[74,321,322,341,354,373,383,396,419],{},[55,323,324,331],{},[79,325,326,99,328],{},[14,327,24],{},[14,329,330],{},"tryUseKubb()",[79,332,333,334,336,337,340],{},"Any hook. ",[14,335,330],{}," returns ",[14,338,339],{},"undefined"," outside one instead of throwing.",[55,342,343,351],{},[79,344,345,99,348],{},[14,346,347],{},"useConfig()",[14,349,350],{},"requirePlugin(name)",[79,352,353],{},"Any hook.",[55,355,356,370],{},[79,357,358,99,361,99,364,99,367],{},[14,359,360],{},"memo(key, fn)",[14,362,363],{},"memo.get",[14,365,366],{},"memo.set",[14,368,369],{},"memo.has",[79,371,372],{},"Any hook. They read the build memo of the running hook.",[55,374,375,380],{},[79,376,377],{},[14,378,379],{},"memo(store, key, fn)",[79,381,382],{},"Anywhere. It caches in the store you pass and needs no hook.",[55,384,385,389],{},[79,386,387],{},[14,388,288],{},[79,390,391,392,395],{},"Any hook. Pass a memo as the second argument, ",[14,393,394],{},"getResolver(name, memo)",", outside a hook.",[55,397,398,409],{},[79,399,400,99,403,99,406],{},[14,401,402],{},"setOptions()",[14,404,405],{},"useOptions()",[14,407,408],{},"injectFile()",[79,410,411,65,413,415,416,33],{},[14,412,32],{},[14,414,85],{},". Elsewhere they throw ",[14,417,418],{},"KUBB_NO_CONTEXT",[55,420,421,441],{},[79,422,423,99,426,99,429,99,432,99,435,99,438],{},[14,424,425],{},"addTemplate()",[14,427,428],{},"setResolver()",[14,430,431],{},"addMacro()",[14,433,434],{},"setMacros()",[14,436,437],{},"setPrinter()",[14,439,440],{},"provide()",[79,442,443,65,445,447,448,451,452,33],{},[14,444,32],{},[14,446,85],{}," of a plugin defined with ",[14,449,450],{},"defineExtension"," from ",[14,453,454],{},"kubb\u002Fkit",[10,456,457,458,33],{},"Outside every hook, a composable that needs a context throws ",[186,459,461],{"href":460},"\u002Fdocs\u002F5.x\u002Freference\u002Fdiagnostics#kubb-no-context",[14,462,418],{},[464,465,470],"pre",{"language":466,"filename":467,"code":468,"class":469},"typescript","plugin.ts","import { defineExtension, getResolver, memo, useConfig } from 'kubb\u002Fkit'\n\nexport const pluginExample = defineExtension({\n kind: 'plugin',\n meta: { name: 'plugin-example' },\n hooks: {\n 'kubb:plugin:visit:schema'(node) {\n const names = memo('plugin-example:names', () => new Set\u003Cstring>())\n if (node.name) names.add(getResolver('plugin-ts').name(node.name))\n console.log(useConfig().root, this.plugin.name)\n },\n },\n})","highlights",[14,471,473,513,514,513,516,513,536,513,553,513,577,513,588,513,605,513,650,513,703,513,742,513,749,513,757],{"class":472},"language-typescript",[474,475,478,482,486,489,491,494,496,498,500,503,506,509],"span",{"class":476,"style":477},"line","display: inline",[474,479,481],{"style":480},"color:#d73a49;--hls-dark:#f97583","import ",[474,483,485],{"style":484},"color:#005cc5;--hls-dark:#79b8ff","{ ",[474,487,450],{"style":488},"color:#24292e;--hls-dark:#e1e4e8",[474,490,99],{"style":480},[474,492,493],{"style":488},"getResolver",[474,495,99],{"style":480},[474,497,260],{"style":488},[474,499,99],{"style":480},[474,501,502],{"style":488},"useConfig ",[474,504,505],{"style":484},"} ",[474,507,508],{"style":480},"from ",[474,510,512],{"style":511},"color:#032f62;--hls-dark:#9ecbff","'kubb\u002Fkit'","\n",[474,515],{"class":476,"style":477},[474,517,518,521,524,527,530,533],{"class":476,"style":477},[474,519,520],{"style":480},"export ",[474,522,523],{"style":480},"const ",[474,525,526],{"style":488},"pluginExample ",[474,528,529],{"style":480},"= ",[474,531,450],{"style":532},"color:#6f42c1;--hls-dark:#b392f0",[474,534,535],{"style":484},"({",[474,537,538,541,544,547,550],{"class":476,"style":477},[474,539,540],{"style":484}," ",[474,542,543],{"style":484},"kind",[474,545,546],{"style":480},": ",[474,548,549],{"style":511},"'plugin'",[474,551,552],{"style":480},",",[474,554,555,557,560,562,564,567,569,572,575],{"class":476,"style":477},[474,556,540],{"style":480},[474,558,559],{"style":484},"meta",[474,561,546],{"style":480},[474,563,485],{"style":484},[474,565,566],{"style":484},"name",[474,568,546],{"style":480},[474,570,571],{"style":511},"'plugin-example' ",[474,573,574],{"style":484},"}",[474,576,552],{"style":480},[474,578,579,581,583,585],{"class":476,"style":477},[474,580,540],{"style":480},[474,582,263],{"style":484},[474,584,546],{"style":480},[474,586,587],{"style":484},"{",[474,589,590,593,596,599,602],{"class":476,"style":477},[474,591,592],{"style":484}," ",[474,594,595],{"style":511},"'kubb:plugin:visit:schema'",[474,597,598],{"style":484},"(",[474,600,601],{"style":488},"node",[474,603,604],{"style":484},") {",[474,606,607,610,612,615,617,619,621,624,626,629,632,635,638,641,644,647],{"class":476,"style":477},[474,608,609],{"style":484}," ",[474,611,523],{"style":480},[474,613,614],{"style":488},"names ",[474,616,529],{"style":480},[474,618,260],{"style":532},[474,620,598],{"style":484},[474,622,623],{"style":511},"'plugin-example:names'",[474,625,99],{"style":480},[474,627,628],{"style":484},"() ",[474,630,631],{"style":480},"=> ",[474,633,634],{"style":480},"new ",[474,636,637],{"style":532},"Set",[474,639,640],{"style":480},"\u003C",[474,642,643],{"style":488},"string",[474,645,646],{"style":480},">",[474,648,649],{"style":484},"())",[474,651,652,654,657,659,661,663,665,668,671,673,676,678,680,682,685,688,690,692,694,696,698,700],{"class":476,"style":477},[474,653,609],{"style":484},[474,655,656],{"style":480},"if ",[474,658,598],{"style":484},[474,660,601],{"style":488},[474,662,33],{"style":480},[474,664,566],{"style":484},[474,666,667],{"style":484},") ",[474,669,670],{"style":488},"names",[474,672,33],{"style":480},[474,674,675],{"style":532},"add",[474,677,598],{"style":484},[474,679,493],{"style":532},[474,681,598],{"style":484},[474,683,684],{"style":511},"'plugin-ts'",[474,686,687],{"style":484},")",[474,689,33],{"style":480},[474,691,566],{"style":532},[474,693,598],{"style":484},[474,695,601],{"style":488},[474,697,33],{"style":480},[474,699,566],{"style":484},[474,701,702],{"style":484},"))",[474,704,705,707,710,712,715,717,720,723,725,728,730,732,734,736,738,740],{"class":476,"style":477},[474,706,609],{"style":484},[474,708,709],{"style":488},"console",[474,711,33],{"style":480},[474,713,714],{"style":532},"log",[474,716,598],{"style":484},[474,718,719],{"style":532},"useConfig",[474,721,722],{"style":484},"()",[474,724,33],{"style":480},[474,726,727],{"style":484},"root",[474,729,99],{"style":480},[474,731,16],{"style":488},[474,733,33],{"style":480},[474,735,257],{"style":484},[474,737,33],{"style":480},[474,739,566],{"style":484},[474,741,687],{"style":484},[474,743,744,747],{"class":476,"style":477},[474,745,746],{"style":484}," }",[474,748,552],{"style":480},[474,750,751,753,755],{"class":476,"style":477},[474,752,540],{"style":480},[474,754,574],{"style":484},[474,756,552],{"style":480},[474,758,759],{"class":476,"style":477},[474,760,761],{"style":484},"})",[763,764,766],"h3",{"id":765},"setup-methods","Setup methods",[10,768,769,770,451,772,774,775,33],{},"In a plugin defined with ",[14,771,450],{},[14,773,454],{},", the setup context also has the setup composables as methods. See ",[186,776,777],{"href":236},"Setup context",[35,779,781],{"id":780},"withcontext",[14,782,783],{},"withContext",[10,785,786,789,790,793,794,796,797,799,800,802,803,805,806,808,809,811],{},[14,787,788],{},"withContext(ctx, fn)"," runs ",[14,791,792],{},"fn"," with ",[14,795,20],{}," as its ",[14,798,16],{},", its argument and the value ",[14,801,24],{}," returns, also after an ",[14,804,28],{}," in ",[14,807,792],{},". It returns what ",[14,810,792],{}," returns. Kubb runs every hook through it.",[464,813,816],{"language":466,"filename":814,"code":815,"class":469},"Type definition","function withContext\u003CTContext extends object, TResult>(ctx: TContext, fn: (this: TContext, ctx: TContext) => TResult): TResult",[14,817,818],{"class":472},[474,819,820,823,825,827,830,833,836,838,841,843,845,847,849,852,854,856,858,860,862,864,866,868,870,872,874,876,878,880,882,884],{"class":476,"style":477},[474,821,822],{"style":480},"function ",[474,824,783],{"style":532},[474,826,640],{"style":480},[474,828,829],{"style":532},"TContext ",[474,831,832],{"style":480},"extends ",[474,834,835],{"style":532},"object",[474,837,99],{"style":480},[474,839,840],{"style":532},"TResult",[474,842,646],{"style":480},[474,844,598],{"style":484},[474,846,20],{"style":488},[474,848,546],{"style":488},[474,850,851],{"style":532},"TContext",[474,853,99],{"style":480},[474,855,792],{"style":488},[474,857,546],{"style":480},[474,859,598],{"style":484},[474,861,16],{"style":488},[474,863,546],{"style":488},[474,865,851],{"style":532},[474,867,99],{"style":480},[474,869,20],{"style":484},[474,871,546],{"style":488},[474,873,851],{"style":532},[474,875,667],{"style":484},[474,877,631],{"style":480},[474,879,840],{"style":532},[474,881,687],{"style":484},[474,883,546],{"style":488},[474,885,840],{"style":532},[10,887,888],{},"Extension authors use it to run their own callbacks with a context, so composables work there too:",[464,890,892],{"language":466,"code":891,"class":469},"import { useKubb, withContext } from 'kubb\u002Fkit'\n\nawait withContext(ctx, async function () {\n console.log(this === useKubb()) \u002F\u002F true\n})",[14,893,894,513,914,513,916,513,937,513,964],{"class":472},[474,895,896,898,900,903,905,908,910,912],{"class":476,"style":477},[474,897,481],{"style":480},[474,899,485],{"style":484},[474,901,902],{"style":488},"useKubb",[474,904,99],{"style":480},[474,906,907],{"style":488},"withContext ",[474,909,505],{"style":484},[474,911,508],{"style":480},[474,913,512],{"style":511},[474,915],{"class":476,"style":477},[474,917,918,921,923,925,927,929,932,934],{"class":476,"style":477},[474,919,920],{"style":480},"await ",[474,922,783],{"style":532},[474,924,598],{"style":484},[474,926,20],{"style":488},[474,928,99],{"style":480},[474,930,931],{"style":480},"async ",[474,933,822],{"style":480},[474,935,936],{"style":484},"() {",[474,938,939,941,943,945,947,949,952,955,957,960],{"class":476,"style":477},[474,940,540],{"style":484},[474,942,709],{"style":488},[474,944,33],{"style":480},[474,946,714],{"style":532},[474,948,598],{"style":484},[474,950,951],{"style":488},"this ",[474,953,954],{"style":480},"=== ",[474,956,902],{"style":532},[474,958,959],{"style":484},"()) ",[474,961,963],{"style":962},"color:#6a737d;--hls-dark:#6a737d","\u002F\u002F true",[474,965,966],{"class":476,"style":477},[474,967,761],{"style":484},[763,969,971],{"id":970},"in-tests","In tests",[10,973,974,975,977,978,981,982,984,985,987,988,990],{},"A plugin test can call a template or hook through ",[14,976,783],{}," instead of ",[14,979,980],{},"kubbContext.run",". It also sets ",[14,983,16],{},", so code that reads ",[14,986,16],{}," or ",[14,989,24],{}," sees your test context:",[464,992,995],{"language":466,"filename":993,"code":994,"class":469},"template.test.ts","import { withContext } from 'kubb\u002Fkit'\n\nconst files = await withContext(ctx, (ctx) => typeSchema.render.call(ctx, node, ctx))",[14,996,997,513,1011,513,1013],{"class":472},[474,998,999,1001,1003,1005,1007,1009],{"class":476,"style":477},[474,1000,481],{"style":480},[474,1002,485],{"style":484},[474,1004,907],{"style":488},[474,1006,505],{"style":484},[474,1008,508],{"style":480},[474,1010,512],{"style":511},[474,1012],{"class":476,"style":477},[474,1014,1015,1017,1020,1022,1024,1026,1028,1030,1032,1034,1036,1038,1040,1043,1045,1047,1049,1052,1054,1056,1058,1060,1062,1064],{"class":476,"style":477},[474,1016,523],{"style":480},[474,1018,1019],{"style":488},"files ",[474,1021,529],{"style":480},[474,1023,920],{"style":480},[474,1025,783],{"style":532},[474,1027,598],{"style":484},[474,1029,20],{"style":488},[474,1031,99],{"style":480},[474,1033,598],{"style":484},[474,1035,20],{"style":488},[474,1037,667],{"style":484},[474,1039,631],{"style":480},[474,1041,1042],{"style":488},"typeSchema",[474,1044,33],{"style":480},[474,1046,160],{"style":484},[474,1048,33],{"style":480},[474,1050,1051],{"style":532},"call",[474,1053,598],{"style":484},[474,1055,20],{"style":488},[474,1057,99],{"style":480},[474,1059,601],{"style":488},[474,1061,99],{"style":480},[474,1063,20],{"style":488},[474,1065,702],{"style":484},{"title":1067,"searchDepth":1068,"depth":1068,"links":1069},"",5,[1070,1072,1076],{"id":37,"depth":1071,"text":38},2,{"id":297,"depth":1071,"text":298,"children":1073},[1074],{"id":765,"depth":1075,"text":766},3,{"id":780,"depth":1071,"text":783,"children":1077},[1078],{"id":970,"depth":1075,"text":971},"Reference for the context Kubb passes to plugin code: what this, the ctx argument and useKubb() are in each hook, which composables work where, and withContext.","md",{"layout":1082,"title":5,"description":1079,"outline":1083,"order":1084,"navigation":1085},"doc",[1071,1075],12,{"title":1086,"icon":1087},"Context","i-iconoir-mirror",{"layout":1082,"outline":1089},[1071,1075],{"title":1086,"icon":1087},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fcontext",{"title":5,"description":1079},{"loc":1091},"kubb-labs\u002Fkubb\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fcontext.md","docs\u002F5.x\u002Freference\u002Fkit\u002Fcontext","buRWiw9LOHNFdu2hhfzBTUU-h8uUb80X7OXjtLvquj4",1795727677768,"1b64b1eb-29b7-4f90-ad41-6f24721e1669"]