@@ -6,12 +6,21 @@ import { debugSourceMap } from './test-utils'
66describe ( transformHoistInlineDirective , ( ) => {
77 async function testTransform (
88 input : string ,
9- options ?: { encode ?: boolean ; noExport ?: boolean ; directive ?: string } ,
9+ options ?: {
10+ encode ?: boolean
11+ noExport ?: boolean
12+ directive ?: string | RegExp
13+ } ,
1014 ) {
1115 const ast = await parseAstAsync ( input )
1216 const { output } = transformHoistInlineDirective ( input , ast , {
13- runtime : ( value , name ) =>
14- `$$register(${ value } , "<id>", ${ JSON . stringify ( name ) } )` ,
17+ runtime : ( value , name , meta ) =>
18+ `$$register(${ value } , "<id>", ${ JSON . stringify ( name ) } ` +
19+ `${
20+ options ?. directive instanceof RegExp
21+ ? `, ${ JSON . stringify ( meta ) } `
22+ : ''
23+ } )`,
1524 directive : options ?. directive ?? 'use server' ,
1625 encode : options ?. encode ? ( v ) => `__enc(${ v } )` : undefined ,
1726 decode : options ?. encode ? ( v ) => `__dec(${ v } )` : undefined ,
@@ -369,4 +378,55 @@ export async function test() {
369378 "
370379 ` )
371380 } )
381+
382+ it ( 'directive pattern' , async ( ) => {
383+ const input = `
384+ export async function none() {
385+ "use cache";
386+ return "test";
387+ }
388+
389+ export async function fs() {
390+ "use cache: fs";
391+ return "test";
392+ }
393+
394+ export async function kv() {
395+ "use cache: kv";
396+ return "test";
397+ }
398+ `
399+ expect (
400+ await testTransform ( input , {
401+ directive : / ^ u s e c a c h e ( : .+ ) ? $ / ,
402+ noExport : true ,
403+ } ) ,
404+ ) . toMatchInlineSnapshot ( `
405+ "
406+ export const none = /* #__PURE__ */ $$register($$hoist_0_none, "<id>", "$$hoist_0_none", {"directiveMatch":["use cache",null]});
407+
408+ export const fs = /* #__PURE__ */ $$register($$hoist_1_fs, "<id>", "$$hoist_1_fs", {"directiveMatch":["use cache: fs",": fs"]});
409+
410+ export const kv = /* #__PURE__ */ $$register($$hoist_2_kv, "<id>", "$$hoist_2_kv", {"directiveMatch":["use cache: kv",": kv"]});
411+
412+ ;async function $$hoist_0_none() {
413+ "use cache";
414+ return "test";
415+ };
416+ /* #__PURE__ */ Object.defineProperty($$hoist_0_none, "name", { value: "none" });
417+
418+ ;async function $$hoist_1_fs() {
419+ "use cache: fs";
420+ return "test";
421+ };
422+ /* #__PURE__ */ Object.defineProperty($$hoist_1_fs, "name", { value: "fs" });
423+
424+ ;async function $$hoist_2_kv() {
425+ "use cache: kv";
426+ return "test";
427+ };
428+ /* #__PURE__ */ Object.defineProperty($$hoist_2_kv, "name", { value: "kv" });
429+ "
430+ ` )
431+ } )
372432} )
0 commit comments