diff options
author | 2021-03-16 10:23:29 -0400 | |
---|---|---|
committer | 2021-03-16 10:23:29 -0400 | |
commit | e5ebebbcd74df8027651f3cf717fb16ffe712367 (patch) | |
tree | beed51f761bf12acf039dd3ee6eb395cb198d5ee /src/transform2.ts | |
parent | 01c34ac5d4a9da9e1a15222653b53620f0e48df6 (diff) | |
download | astro-e5ebebbcd74df8027651f3cf717fb16ffe712367.tar.gz astro-e5ebebbcd74df8027651f3cf717fb16ffe712367.tar.zst astro-e5ebebbcd74df8027651f3cf717fb16ffe712367.zip |
Progress on preparsing
Diffstat (limited to '')
-rw-r--r-- | src/transform2.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/transform2.ts b/src/transform2.ts index 51399a48a..485c575c6 100644 --- a/src/transform2.ts +++ b/src/transform2.ts @@ -11,6 +11,7 @@ import matter from "gray-matter"; import gfmHtml from "micromark-extension-gfm/html.js"; import { walk, parse } from "./compiler.js"; import markdownEncode from './markdown-encode.js'; +import { preparse } from './parser.js'; const { transformSync } = esbuild; @@ -161,11 +162,55 @@ function getComponentWrapper( throw new Error("Unknown Component Type: " + name); } +function runPreparser(template: string): string { + const doc = preparse(template, tag => { + if(tag.tagName === 'script') { + let isSetup = false; + for(let attr of tag.attributes) { + if(attr.name === 'hmx' && attr.value === 'setup') { + isSetup = true; + break; + } + } + if(isSetup && typeof tag.children[0] === 'string') { + debugger; + + const content = tag.children[0]; + let { code } = transformSync(content, { + loader: "tsx", + jsxFactory: "h", + jsxFragment: "Fragment", + charset: "utf8", + }); + return { + ...tag, + children: [{ + type: 0, + data: code, + start: 0, + end: 0 + }] + }; + } + } + + return tag; + }); + + // TODO codegen + + return template; +} + async function convertHmxToJsx(template: string, compileOptions: CompileOptions) { await eslexer.init; + + //template = runPreparser(template); + const ast = parse(template, {}); // Todo: Validate that `h` and `Fragment` aren't defined in the script const script = ast.instance ? astring.generate(ast.instance.content) : ""; + const [scriptImports] = eslexer.parse(script, "optional-sourcename"); const components = Object.fromEntries( scriptImports.map((imp) => { |