-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom codegen for directives passing
- Loading branch information
Showing
5 changed files
with
260 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { readdirSync } from "node:fs"; | ||
|
||
const fileRegex = /\.tsx$/; | ||
const codegenRegex = /\/\/ @codegen (.*)/g; | ||
|
||
const DIRECTIVES = readdirSync("./components/ui/directives") | ||
.filter((x) => x !== "index.ts") | ||
.map((x) => x.substring(0, x.length - 3)); | ||
|
||
const directiveRegex = new RegExp("use:(" + DIRECTIVES.join("|") + ")"); | ||
|
||
export default function codegenPlugin() { | ||
return { | ||
name: "codegen", | ||
enforce: "pre", | ||
transform(src: string, id: string) { | ||
if (fileRegex.test(id)) { | ||
src = src.replace(codegenRegex, (substring, group1) => { | ||
const rawArgs: string[] = group1.split(" "); | ||
const type = rawArgs.shift(); | ||
|
||
const args = rawArgs.reduce( | ||
(d, arg) => { | ||
const [key, value] = arg.split("="); | ||
return { | ||
...d, | ||
[key]: value, | ||
}; | ||
}, | ||
{ type } | ||
) as { | ||
type: "directives"; | ||
props?: string; | ||
include?: string; | ||
}; | ||
|
||
switch (args.type) { | ||
case "directives": | ||
// Generate directives forwarding | ||
const source = args.props ?? "props"; | ||
const permitted: string[] = | ||
args.include?.split(",") ?? DIRECTIVES; | ||
return DIRECTIVES.filter((d) => permitted.includes(d)) | ||
.map((d) => `use:${d}={${source}["use:${d}"]}`) | ||
.join("\n"); | ||
default: | ||
return substring; | ||
} | ||
}); | ||
|
||
if (directiveRegex.test(src)) { | ||
if (!id.endsWith("client/components/ui/index.tsx")) | ||
src = | ||
`import { ${DIRECTIVES.join( | ||
", " | ||
)} } from "@revolt/ui/directives";\n` + src; | ||
} | ||
|
||
return src; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.