-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e5860e
commit ee37d14
Showing
12 changed files
with
133 additions
and
70 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
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,10 @@ | ||
import type { Options } from "./types.ts"; | ||
|
||
export const defaultFormats: Options['formats']; | ||
export const defaults: Options; | ||
|
||
export class Compiler { | ||
constructor(options?: Options); | ||
|
||
compile(format: string, text: string): Promise<HTMLElement>; | ||
} |
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,61 @@ | ||
/** | ||
* @typedef {import("./types.ts").Options} Options | ||
*/ | ||
import { assert } from './utils.js'; | ||
|
||
export const defaultFormats = { | ||
mermaid: { | ||
compiler: async () => { | ||
return { | ||
compile: async (text) => {}, | ||
}; | ||
}, | ||
}, | ||
}; | ||
|
||
export const defaults = { | ||
formats: defaultFormats, | ||
}; | ||
|
||
export class Compiler { | ||
/** @type {Options} */ | ||
#options; | ||
|
||
/** | ||
* Options may be passed to the compiler to add to its behavior. | ||
*/ | ||
constructor(options = defaults) { | ||
this.#options = options; | ||
} | ||
|
||
/** | ||
* @param {string} format | ||
* @param {string} text | ||
*/ | ||
async compile(format, text) { | ||
const config = this.#options.formats[format]; | ||
|
||
assert( | ||
`${format} is not a configured format. ` + | ||
`The currently configured formats are ${Object.keys(this.#options.formats).join(', ')}`, | ||
config | ||
); | ||
|
||
const compiler = await config.compiler(); | ||
// TODO: pass this through es-module-shims | ||
// for getting the actual module back | ||
const compiledText = await compiler.compile(text); | ||
|
||
const div = this.#createDiv(); | ||
|
||
compiler.render(div, compiledText); | ||
|
||
return div; | ||
} | ||
|
||
#createDiv() { | ||
let div = document.createElement('div'); | ||
div.setAttribute('data-repl-output', ''); | ||
return div; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @param {string} message | ||
* @param {unknown} test | ||
* @asserts test | ||
*/ | ||
export function assert(message, test) { | ||
if (!test) { | ||
throw new Error(message); | ||
} | ||
} |
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
Binary file added
BIN
+2 KB
...t/tests/__screenshots__/custom-compiler.test.tsx/Custom-compiler-it-works-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { defineConfig } from 'vite'; | ||
import wasm from 'vite-plugin-wasm'; | ||
|
||
const wasmMiddleware = () => { | ||
return { | ||
name: 'wasm-middleware', | ||
configureServer(server) { | ||
server.middlewares.use((req, res, next) => { | ||
if (req.url.endsWith('.wasm')) { | ||
const wasmPath = path.join(__dirname, 'public', req.url); | ||
const wasmFile = fs.readFileSync(wasmPath); | ||
res.setHeader('Content-Type', 'application/wasm'); | ||
res.end(wasmFile); | ||
return; | ||
} | ||
next(); | ||
}); | ||
}, | ||
}; | ||
}; | ||
|
||
export default defineConfig({ | ||
esbuild: { | ||
supported: { | ||
'top-level-await': true, | ||
}, | ||
}, | ||
plugins: [ | ||
wasmMiddleware, | ||
// wasm(), | ||
], | ||
}); |
File renamed without changes.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.