htmlemmurt 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +40 -0
- package/browser.d.ts +2 -0
- package/browser.js +75 -0
- package/index.d.ts +64 -0
- package/index.js +25 -0
- package/lib/api.js +42 -0
- package/lib/runtime.js +40 -0
- package/package.json +67 -0
- package/vite.d.ts +9 -0
- package/vite.js +147 -0
- package/wasm/node/emmurt_wasm.js +266 -0
- package/wasm/node/emmurt_wasm_bg.wasm +0 -0
- package/wasm/node/package.json +1 -0
- package/wasm/web/emmurt_wasm.js +360 -0
- package/wasm/web/emmurt_wasm_bg.wasm +0 -0
- package/web.d.ts +12 -0
- package/web.js +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vito Esposito
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# htmlemmurt
|
|
2
|
+
|
|
3
|
+
Il compilatore di [htmlemmurt](https://github.com/bitoesposito/htmlemmurt) (HTML in napoletano) in WebAssembly, con un plugin Vite e un runtime per il browser.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm i -D htmlemmurt
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Da Node
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import { compile, compileFratm, formatError } from 'htmlemmurt';
|
|
13
|
+
|
|
14
|
+
const { html, dependencies, errors } = compile('<cascett classe="a">Uè</cascett>', {
|
|
15
|
+
filename: 'index.emmurt',
|
|
16
|
+
});
|
|
17
|
+
// html → '<div class="a">Uè</div>'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`compile` non lancia mai eccezioni: gli errori stanno in `errors`, con `message`, `file`, `line`, `column` e `suggestion`, in napoletano. Le opzioni sono `filename`, `root` (la cartella per i percorsi che cominciano con `/`), `read` (per leggere i file in un altro modo) e `inline` (incolla nella pagina i file di `<fratm da>`). `compileFratm` compila un modulo FratmScript.
|
|
21
|
+
|
|
22
|
+
## Con Vite
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
// vite.config.js
|
|
26
|
+
import htmlemmurt from 'htmlemmurt/vite';
|
|
27
|
+
|
|
28
|
+
export default { plugins: [htmlemmurt()] };
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Ogni `x.emmurt` che comincia con `<cristian>` diventa `x.html`, in sviluppo e in build. Anche i moduli `.fratm` passano dal plugin.
|
|
32
|
+
|
|
33
|
+
## Nel browser
|
|
34
|
+
|
|
35
|
+
```html
|
|
36
|
+
<script type="module" src="https://cdn.jsdelivr.net/npm/htmlemmurt@0.1/browser.js"></script>
|
|
37
|
+
<script type="text/htmlemmurt" src="index.emmurt"></script>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Da JavaScript: `import { init, compile } from 'htmlemmurt/web'`, poi `await init()`.
|
package/browser.d.ts
ADDED
package/browser.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Runs htmlemmurt in a web page, no build step:
|
|
2
|
+
//
|
|
3
|
+
// <script type="module" src="…/htmlemmurt/browser.js"></script>
|
|
4
|
+
// <script type="text/htmlemmurt" src="index.emmurt"></script>
|
|
5
|
+
//
|
|
6
|
+
// Each <script type="text/htmlemmurt"> (inline or with src) is compiled and
|
|
7
|
+
// takes the place of the tag; a whole page (<cristian>) replaces the document.
|
|
8
|
+
// Errors are shown in the page and in the console, in Neapolitan. `ready`
|
|
9
|
+
// resolves when every script has been shown.
|
|
10
|
+
import { init, compile, runtimeError, formatError } from './web.js';
|
|
11
|
+
import { SCRIPT_TYPE, compileWithFiles, scriptName } from './lib/runtime.js';
|
|
12
|
+
|
|
13
|
+
async function load(path) {
|
|
14
|
+
const response = await fetch(new URL(path, document.baseURI));
|
|
15
|
+
if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
|
|
16
|
+
return response.text();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function domReady() {
|
|
20
|
+
if (document.readyState !== 'loading') return Promise.resolve();
|
|
21
|
+
return new Promise((resolve) => document.addEventListener('DOMContentLoaded', resolve, { once: true }));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function report(script, message) {
|
|
25
|
+
console.error(`🤌 htmlemmurt ${message}`);
|
|
26
|
+
const box = document.createElement('pre');
|
|
27
|
+
box.style.cssText = 'color:#7a1f1f;background:#fff4f4;padding:1rem;border:1px solid #e3b4ae;border-radius:6px;white-space:pre-wrap';
|
|
28
|
+
box.textContent = `Uh, mannaggia! ${message}`;
|
|
29
|
+
script.replaceWith(box);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function show(html, script) {
|
|
33
|
+
if (html.startsWith('<!DOCTYPE html>')) {
|
|
34
|
+
document.open();
|
|
35
|
+
document.write(html);
|
|
36
|
+
document.close();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const template = document.createElement('template');
|
|
40
|
+
template.innerHTML = html;
|
|
41
|
+
const scripts = [...template.content.querySelectorAll('script')];
|
|
42
|
+
script.replaceWith(template.content);
|
|
43
|
+
// Scripts parsed by innerHTML never run: swap each for a fresh copy
|
|
44
|
+
for (const old of scripts) {
|
|
45
|
+
const fresh = document.createElement('script');
|
|
46
|
+
for (const { name, value } of old.attributes) fresh.setAttribute(name, value);
|
|
47
|
+
fresh.textContent = old.textContent;
|
|
48
|
+
old.replaceWith(fresh);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
window.addEventListener('error', (event) => {
|
|
53
|
+
if (event.error instanceof Error) console.error(`🤌 ${runtimeError(event.error)}`);
|
|
54
|
+
});
|
|
55
|
+
window.addEventListener('unhandledrejection', (event) => {
|
|
56
|
+
if (event.reason instanceof Error) console.error(`🤌 ${runtimeError(event.reason)}`);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export const ready = Promise.all([init(), domReady()]).then(async () => {
|
|
60
|
+
const scripts = [...document.querySelectorAll(`script[type="${SCRIPT_TYPE}"]`)];
|
|
61
|
+
for (const [index, script] of scripts.entries()) {
|
|
62
|
+
const src = script.getAttribute('src');
|
|
63
|
+
const filename = scriptName(src, index);
|
|
64
|
+
let source;
|
|
65
|
+
try {
|
|
66
|
+
source = src ? await load(src) : script.textContent;
|
|
67
|
+
} catch (error) {
|
|
68
|
+
report(script, `nun riesco a caricà ${filename} (${error.message})`);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const { html, errors } = await compileWithFiles(source, filename, load, compile);
|
|
72
|
+
if (errors.length > 0) report(script, formatError(errors[0]));
|
|
73
|
+
else show(html, script);
|
|
74
|
+
}
|
|
75
|
+
});
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export interface EmmurtError {
|
|
2
|
+
/** Error message, in Neapolitan */
|
|
3
|
+
message: string;
|
|
4
|
+
/** File the error is in: the page or a file it includes */
|
|
5
|
+
file: string;
|
|
6
|
+
/** 1-based line */
|
|
7
|
+
line: number;
|
|
8
|
+
/** 1-based column */
|
|
9
|
+
column: number;
|
|
10
|
+
/** A hint on how to fix it, when there is one */
|
|
11
|
+
suggestion: string | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface CompileOptions {
|
|
15
|
+
/** Name of the page, for errors and to resolve relative paths (default: "input.emmurt"). */
|
|
16
|
+
filename?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Reads a file named by `<chiamm da>` or an inlined `<fratm da>`: its text, or null when it
|
|
19
|
+
* does not exist. Default in Node: from disk, `/…` paths from `root`.
|
|
20
|
+
*/
|
|
21
|
+
read?: (path: string) => string | null;
|
|
22
|
+
/** Folder for paths that start with `/` (Node only, default: the working directory). */
|
|
23
|
+
root?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Write `<fratm da="file.fratm">` files into the page instead of leaving
|
|
26
|
+
* `<script type="module" src>` for a bundler (default: false).
|
|
27
|
+
*/
|
|
28
|
+
inline?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface CompileResult {
|
|
32
|
+
/** The HTML, or null when there are errors */
|
|
33
|
+
html: string | null;
|
|
34
|
+
/** Files read while compiling */
|
|
35
|
+
dependencies: string[];
|
|
36
|
+
errors: EmmurtError[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface FratmResult {
|
|
40
|
+
/** The JavaScript, or null when there are errors */
|
|
41
|
+
code: string | null;
|
|
42
|
+
/** Source map (v3) back to the .fratm file */
|
|
43
|
+
map: { version: number; file: string; sources: string[]; sourcesContent: string[]; names: string[]; mappings: string } | null;
|
|
44
|
+
errors: EmmurtError[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface Vocabulary {
|
|
48
|
+
tags: [string, string][];
|
|
49
|
+
attributes: { nap: string; html: string; only?: string[]; values?: [string, string][] }[];
|
|
50
|
+
events: [string, string][];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Compiles htmlemmurt to HTML. Never throws on invalid source: check `errors`. */
|
|
54
|
+
export function compile(source: string, options?: CompileOptions): CompileResult;
|
|
55
|
+
/** Compiles a FratmScript module to JavaScript. */
|
|
56
|
+
export function compileFratm(source: string, options?: { filename?: string }): FratmResult;
|
|
57
|
+
/** Every Neapolitan name and its HTML. */
|
|
58
|
+
export function vocabulary(): Vocabulary;
|
|
59
|
+
/** Version of the compiler. */
|
|
60
|
+
export function version(): string;
|
|
61
|
+
/** A JavaScript runtime error in Neapolitan, with the original in brackets. */
|
|
62
|
+
export function runtimeError(error: unknown): string;
|
|
63
|
+
/** `file:line:column: message`, then the hint. */
|
|
64
|
+
export function formatError(error: EmmurtError): string;
|
package/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// htmlemmurt compiler for Node.js (and bundlers that run in Node).
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { createApi } from './lib/api.js';
|
|
6
|
+
|
|
7
|
+
const wasm = createRequire(import.meta.url)('./wasm/node/emmurt_wasm.js');
|
|
8
|
+
const api = createApi(wasm);
|
|
9
|
+
|
|
10
|
+
export const { compileFratm, vocabulary, version, runtimeError } = api;
|
|
11
|
+
export { formatError } from './lib/api.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Compiles a page. Included files are read from disk: paths starting with
|
|
15
|
+
* `/` that are not absolute paths on disk are taken from `root`.
|
|
16
|
+
*/
|
|
17
|
+
export function compile(source, options = {}) {
|
|
18
|
+
const { root = process.cwd(), read = (path) => readFrom(root, path), ...rest } = options;
|
|
19
|
+
return api.compile(source, { ...rest, read });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function readFrom(root, path) {
|
|
23
|
+
const file = path.startsWith('/') && !existsSync(path) ? join(root, path) : path;
|
|
24
|
+
return existsSync(file) ? readFileSync(file, 'utf8') : null;
|
|
25
|
+
}
|
package/lib/api.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Turns the raw responses of the WASM bindings into the public API.
|
|
2
|
+
|
|
3
|
+
/** @param {any} wasm */
|
|
4
|
+
export function createApi(wasm) {
|
|
5
|
+
function compile(source, options = {}) {
|
|
6
|
+
const { filename = 'input.emmurt', read, inline = false } = options;
|
|
7
|
+
// The bindings take a missing file as anything that is not a string
|
|
8
|
+
const response = wasm.compile(String(source), filename, read ? (path) => read(path) : undefined, inline);
|
|
9
|
+
return { html: response.code, dependencies: response.dependencies, errors: response.errors };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function compileFratm(source, options = {}) {
|
|
13
|
+
const filename = options.filename ?? 'input.fratm';
|
|
14
|
+
const response = wasm.compileFratm(String(source), filename);
|
|
15
|
+
let map = null;
|
|
16
|
+
if (response.map) {
|
|
17
|
+
map = JSON.parse(response.map);
|
|
18
|
+
map.sources = [filename];
|
|
19
|
+
map.sourcesContent = [String(source)];
|
|
20
|
+
map.file = filename.replace(/\.fratm$/, '') + '.js';
|
|
21
|
+
}
|
|
22
|
+
return { code: response.code, map, errors: response.errors };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function vocabulary() {
|
|
26
|
+
return JSON.parse(wasm.vocabulary());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function runtimeError(error) {
|
|
30
|
+
const name = error && error.name ? String(error.name) : 'Error';
|
|
31
|
+
const message = error && error.message !== undefined ? String(error.message) : String(error);
|
|
32
|
+
return wasm.runtimeError(name, message);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return { compile, compileFratm, vocabulary, version: wasm.version, runtimeError };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `file:line:column: message`, then the hint on its own line. */
|
|
39
|
+
export function formatError(error) {
|
|
40
|
+
const hint = error.suggestion ? `\n ${error.suggestion}` : '';
|
|
41
|
+
return `${error.file}:${error.line}:${error.column}: ${error.message}${hint}`;
|
|
42
|
+
}
|
package/lib/runtime.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// The parts of the browser runtime that need no DOM, so tests can run them in Node.
|
|
2
|
+
|
|
3
|
+
export const SCRIPT_TYPE = 'text/htmlemmurt';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Compiles a page, fetching the files it includes. The compiler reads files
|
|
7
|
+
* synchronously, so each round compiles with the files fetched so far and
|
|
8
|
+
* fetches the one it was missing, until nothing is missing.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} source
|
|
11
|
+
* @param {string} filename paths in the page are relative to it
|
|
12
|
+
* @param {(path: string) => Promise<string>} load rejects when the file is missing
|
|
13
|
+
* @param {(source: string, options: object) => any} compile the htmlemmurt compile()
|
|
14
|
+
*/
|
|
15
|
+
export async function compileWithFiles(source, filename, load, compile) {
|
|
16
|
+
const files = new Map();
|
|
17
|
+
for (;;) {
|
|
18
|
+
const wanted = [];
|
|
19
|
+
const result = compile(source, {
|
|
20
|
+
filename,
|
|
21
|
+
inline: true,
|
|
22
|
+
read: (path) => {
|
|
23
|
+
if (files.has(path)) return files.get(path);
|
|
24
|
+
wanted.push(path);
|
|
25
|
+
return null;
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
if (wanted.length === 0) return result;
|
|
29
|
+
await Promise.all(
|
|
30
|
+
wanted.map(async (path) => {
|
|
31
|
+
files.set(path, await load(path).catch(() => null));
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Name of a page in errors: its `src`, or `pagina-N.emmurt` when inline. */
|
|
38
|
+
export function scriptName(src, index) {
|
|
39
|
+
return src ? src.split(/[?#]/)[0] : `pagina-${index + 1}.emmurt`;
|
|
40
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "htmlemmurt",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "htmlemmurt compiler (WebAssembly), Vite plugin and browser runtime: HTML, ma comme si deve 🤌",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Vito Esposito",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/bitoesposito/htmlemmurt.git",
|
|
11
|
+
"directory": "packages/htmlemmurt"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/bitoesposito/htmlemmurt#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/bitoesposito/htmlemmurt/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"htmlemmurt",
|
|
19
|
+
"fratmscript",
|
|
20
|
+
"napoletano",
|
|
21
|
+
"html",
|
|
22
|
+
"compiler",
|
|
23
|
+
"wasm",
|
|
24
|
+
"vite-plugin"
|
|
25
|
+
],
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./index.d.ts",
|
|
29
|
+
"default": "./index.js"
|
|
30
|
+
},
|
|
31
|
+
"./vite": {
|
|
32
|
+
"types": "./vite.d.ts",
|
|
33
|
+
"default": "./vite.js"
|
|
34
|
+
},
|
|
35
|
+
"./web": {
|
|
36
|
+
"types": "./web.d.ts",
|
|
37
|
+
"default": "./web.js"
|
|
38
|
+
},
|
|
39
|
+
"./browser": {
|
|
40
|
+
"types": "./browser.d.ts",
|
|
41
|
+
"default": "./browser.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"index.js",
|
|
46
|
+
"index.d.ts",
|
|
47
|
+
"vite.js",
|
|
48
|
+
"vite.d.ts",
|
|
49
|
+
"web.js",
|
|
50
|
+
"web.d.ts",
|
|
51
|
+
"browser.js",
|
|
52
|
+
"browser.d.ts",
|
|
53
|
+
"lib/",
|
|
54
|
+
"wasm/node/package.json",
|
|
55
|
+
"wasm/node/emmurt_wasm.js",
|
|
56
|
+
"wasm/node/emmurt_wasm_bg.wasm",
|
|
57
|
+
"wasm/web/emmurt_wasm.js",
|
|
58
|
+
"wasm/web/emmurt_wasm_bg.wasm"
|
|
59
|
+
],
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=18"
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "node scripts/build-wasm.js",
|
|
65
|
+
"test": "node --test test/*.test.js"
|
|
66
|
+
}
|
|
67
|
+
}
|
package/vite.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Serves and builds every `x.emmurt` page as `x.html` and compiles `.fratm`
|
|
5
|
+
* modules. Pages are the .emmurt files that start with `<cristian>`; set
|
|
6
|
+
* `build.rollupOptions.input` to choose them yourself.
|
|
7
|
+
*/
|
|
8
|
+
export default function htmlemmurt(): Plugin;
|
|
9
|
+
export { htmlemmurt };
|
package/vite.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// Vite plugin: .emmurt pages are the app's HTML entries, .fratm files its
|
|
2
|
+
// modules. `x.emmurt` is served and built as `x.html`.
|
|
3
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
4
|
+
import { join, relative, resolve, sep } from 'node:path';
|
|
5
|
+
import { compile, compileFratm, formatError } from './index.js';
|
|
6
|
+
|
|
7
|
+
const FRATM = /\.fratm$/;
|
|
8
|
+
// A request for a .fratm file that Vite would serve as it is
|
|
9
|
+
const RAW_FRATM_REQUEST = /^[^?#]*\.fratm(?:$|[?#])/;
|
|
10
|
+
// A page starts with <cristian>, maybe after comments
|
|
11
|
+
const PAGE = /^\s*(?:<!--[\s\S]*?-->\s*)*<cristian[\s>/]/i;
|
|
12
|
+
const SKIP_DIRS = new Set(['node_modules', 'dist', 'public']);
|
|
13
|
+
|
|
14
|
+
export default function htmlemmurt() {
|
|
15
|
+
let root = process.cwd();
|
|
16
|
+
|
|
17
|
+
/** The .emmurt source of an .html path that is not on disk. */
|
|
18
|
+
const sourceOf = (htmlPath) => {
|
|
19
|
+
if (!htmlPath.endsWith('.html') || existsSync(htmlPath)) return null;
|
|
20
|
+
const source = htmlPath.slice(0, -'.html'.length) + '.emmurt';
|
|
21
|
+
return existsSync(source) ? source : null;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const compilePage = (file) =>
|
|
25
|
+
compile(readFileSync(file, 'utf8'), { filename: file, root });
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
name: 'htmlemmurt',
|
|
29
|
+
enforce: 'pre',
|
|
30
|
+
|
|
31
|
+
config(config) {
|
|
32
|
+
root = resolve(config.root ?? process.cwd());
|
|
33
|
+
if (config.build?.rollupOptions?.input) return;
|
|
34
|
+
const pages = findPages(root);
|
|
35
|
+
if (pages.length === 0) return;
|
|
36
|
+
const input = Object.fromEntries(
|
|
37
|
+
pages.map((page) => [
|
|
38
|
+
relative(root, page).slice(0, -'.emmurt'.length).split(sep).join('/'),
|
|
39
|
+
page.slice(0, -'.emmurt'.length) + '.html',
|
|
40
|
+
]),
|
|
41
|
+
);
|
|
42
|
+
return { build: { rollupOptions: { input } } };
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
configResolved(config) {
|
|
46
|
+
root = config.root;
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
resolveId(id) {
|
|
50
|
+
return sourceOf(id) ? id : null;
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
load(id) {
|
|
54
|
+
const file = sourceOf(id);
|
|
55
|
+
if (!file) return null;
|
|
56
|
+
const { html, dependencies, errors } = compilePage(file);
|
|
57
|
+
if (errors.length > 0) this.error(errorFor(errors[0]));
|
|
58
|
+
for (const dependency of dependencies) this.addWatchFile(resolve(root, dependency));
|
|
59
|
+
return html;
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
transform(source, id) {
|
|
63
|
+
const file = id.split('?', 1)[0];
|
|
64
|
+
if (!FRATM.test(file)) return null;
|
|
65
|
+
const { code, map, errors } = compileFratm(source, { filename: file });
|
|
66
|
+
if (errors.length > 0) this.error(errorFor(errors[0]));
|
|
67
|
+
return { code, map };
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
configureServer(server) {
|
|
71
|
+
const reload = (file) => {
|
|
72
|
+
if (file.endsWith('.emmurt')) server.ws.send({ type: 'full-reload' });
|
|
73
|
+
};
|
|
74
|
+
server.watcher.on('change', reload);
|
|
75
|
+
server.watcher.on('add', reload);
|
|
76
|
+
server.watcher.on('unlink', reload);
|
|
77
|
+
|
|
78
|
+
server.middlewares.use(async (req, res, next) => {
|
|
79
|
+
if (!req.url) return next();
|
|
80
|
+
// The dev server only compiles requests it knows are modules. Imports
|
|
81
|
+
// between modules get `?import`, the <script src> of a page does not
|
|
82
|
+
if (RAW_FRATM_REQUEST.test(req.url) && !/[?&]import(?:[=&#]|$)/.test(req.url)) {
|
|
83
|
+
req.url += req.url.includes('?') ? '&import' : '?import';
|
|
84
|
+
return next();
|
|
85
|
+
}
|
|
86
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') return next();
|
|
87
|
+
let path = decodeURIComponent(req.url.split(/[?#]/, 1)[0]);
|
|
88
|
+
if (path.endsWith('/')) path += 'index.html';
|
|
89
|
+
else if (!/\.[^/]*$/.test(path)) path += '.html';
|
|
90
|
+
const file = sourceOf(join(root, path));
|
|
91
|
+
if (!file) return next();
|
|
92
|
+
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
93
|
+
const { html, errors } = compilePage(file);
|
|
94
|
+
if (errors.length > 0) {
|
|
95
|
+
const message = formatError({ ...errors[0], file: relative(root, errors[0].file) });
|
|
96
|
+
server.config.logger.error(`🤌 ${message}`);
|
|
97
|
+
res.statusCode = 500;
|
|
98
|
+
res.end(errorPage(message));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
res.end(await server.transformIndexHtml(path, html, req.originalUrl));
|
|
103
|
+
} catch (error) {
|
|
104
|
+
next(error);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Every .emmurt file under `dir` that is a page, not a piece for <chiamm>. */
|
|
112
|
+
function findPages(dir) {
|
|
113
|
+
const pages = [];
|
|
114
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
115
|
+
const path = join(dir, entry.name);
|
|
116
|
+
if (entry.isDirectory()) {
|
|
117
|
+
if (!entry.name.startsWith('.') && !SKIP_DIRS.has(entry.name)) pages.push(...findPages(path));
|
|
118
|
+
} else if (entry.name.endsWith('.emmurt') && PAGE.test(readFileSync(path, 'utf8'))) {
|
|
119
|
+
pages.push(path);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return pages.sort();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function errorFor(error) {
|
|
126
|
+
return {
|
|
127
|
+
message: error.suggestion ? `${error.message}\n${error.suggestion}` : error.message,
|
|
128
|
+
id: error.file,
|
|
129
|
+
loc: { file: error.file, line: error.line, column: error.column - 1 },
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// The Vite client reloads the page when the file is saved again
|
|
134
|
+
function errorPage(message) {
|
|
135
|
+
const escaped = message.replace(/&/g, '&').replace(/</g, '<');
|
|
136
|
+
return `<!DOCTYPE html>
|
|
137
|
+
<meta charset="utf-8">
|
|
138
|
+
<title>Uh, mannaggia</title>
|
|
139
|
+
<script type="module" src="/@vite/client"></script>
|
|
140
|
+
<style>body{font:16px/1.5 system-ui;margin:2rem;color:#7a1f1f}pre{background:#fff4f4;padding:1rem;border:1px solid #e3b4ae;border-radius:6px;white-space:pre-wrap}</style>
|
|
141
|
+
<h1>Uh, mannaggia! 🤌</h1>
|
|
142
|
+
<pre>${escaped}</pre>
|
|
143
|
+
<p>Aggiusta 'o file e salva: 'a paggena se ricarica sola.</p>
|
|
144
|
+
`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export { htmlemmurt };
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/* @ts-self-types="./emmurt_wasm.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Compiles a page. `read(path)` returns the text of an included file, or
|
|
5
|
+
* null when it does not exist. `inline` writes `<fratm da>` files into the
|
|
6
|
+
* page instead of leaving them to a bundler.
|
|
7
|
+
* @param {string} source
|
|
8
|
+
* @param {string} filename
|
|
9
|
+
* @param {Function | null | undefined} read
|
|
10
|
+
* @param {boolean} inline
|
|
11
|
+
* @returns {any}
|
|
12
|
+
*/
|
|
13
|
+
function compile(source, filename, read, inline) {
|
|
14
|
+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
15
|
+
const len0 = WASM_VECTOR_LEN;
|
|
16
|
+
const ptr1 = passStringToWasm0(filename, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
17
|
+
const len1 = WASM_VECTOR_LEN;
|
|
18
|
+
const ret = wasm.compile(ptr0, len0, ptr1, len1, isLikeNone(read) ? 0 : addToExternrefTable0(read), inline);
|
|
19
|
+
return ret;
|
|
20
|
+
}
|
|
21
|
+
exports.compile = compile;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Compiles a `.fratm` module to JavaScript.
|
|
25
|
+
* @param {string} source
|
|
26
|
+
* @param {string} filename
|
|
27
|
+
* @returns {any}
|
|
28
|
+
*/
|
|
29
|
+
function compileFratm(source, filename) {
|
|
30
|
+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
31
|
+
const len0 = WASM_VECTOR_LEN;
|
|
32
|
+
const ptr1 = passStringToWasm0(filename, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
33
|
+
const len1 = WASM_VECTOR_LEN;
|
|
34
|
+
const ret = wasm.compileFratm(ptr0, len0, ptr1, len1);
|
|
35
|
+
return ret;
|
|
36
|
+
}
|
|
37
|
+
exports.compileFratm = compileFratm;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A JavaScript runtime error in Neapolitan, as FratmScript says it.
|
|
41
|
+
* @param {string} name
|
|
42
|
+
* @param {string} message
|
|
43
|
+
* @returns {string}
|
|
44
|
+
*/
|
|
45
|
+
function runtimeError(name, message) {
|
|
46
|
+
let deferred3_0;
|
|
47
|
+
let deferred3_1;
|
|
48
|
+
try {
|
|
49
|
+
const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
50
|
+
const len0 = WASM_VECTOR_LEN;
|
|
51
|
+
const ptr1 = passStringToWasm0(message, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
52
|
+
const len1 = WASM_VECTOR_LEN;
|
|
53
|
+
const ret = wasm.runtimeError(ptr0, len0, ptr1, len1);
|
|
54
|
+
deferred3_0 = ret[0];
|
|
55
|
+
deferred3_1 = ret[1];
|
|
56
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
57
|
+
} finally {
|
|
58
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.runtimeError = runtimeError;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @returns {string}
|
|
65
|
+
*/
|
|
66
|
+
function version() {
|
|
67
|
+
let deferred1_0;
|
|
68
|
+
let deferred1_1;
|
|
69
|
+
try {
|
|
70
|
+
const ret = wasm.version();
|
|
71
|
+
deferred1_0 = ret[0];
|
|
72
|
+
deferred1_1 = ret[1];
|
|
73
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
74
|
+
} finally {
|
|
75
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.version = version;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Tags, attributes and events, as JSON.
|
|
82
|
+
* @returns {string}
|
|
83
|
+
*/
|
|
84
|
+
function vocabulary() {
|
|
85
|
+
let deferred1_0;
|
|
86
|
+
let deferred1_1;
|
|
87
|
+
try {
|
|
88
|
+
const ret = wasm.vocabulary();
|
|
89
|
+
deferred1_0 = ret[0];
|
|
90
|
+
deferred1_1 = ret[1];
|
|
91
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
92
|
+
} finally {
|
|
93
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.vocabulary = vocabulary;
|
|
97
|
+
function __wbg_get_imports() {
|
|
98
|
+
const import0 = {
|
|
99
|
+
__proto__: null,
|
|
100
|
+
__wbg_Error_30c8987f7c2ed4e2: function(arg0, arg1) {
|
|
101
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
102
|
+
return ret;
|
|
103
|
+
},
|
|
104
|
+
__wbg___wbindgen_string_get_0380ccaa2f57f0d9: function(arg0, arg1) {
|
|
105
|
+
const obj = arg1;
|
|
106
|
+
const ret = typeof(obj) === 'string' ? obj : undefined;
|
|
107
|
+
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
108
|
+
var len1 = WASM_VECTOR_LEN;
|
|
109
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
110
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
111
|
+
},
|
|
112
|
+
__wbg___wbindgen_throw_41e9ee4f547fc59a: function(arg0, arg1) {
|
|
113
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
114
|
+
},
|
|
115
|
+
__wbg_call_187d372bd5fdd4aa: function() { return handleError(function (arg0, arg1, arg2) {
|
|
116
|
+
const ret = arg0.call(arg1, arg2);
|
|
117
|
+
return ret;
|
|
118
|
+
}, arguments); },
|
|
119
|
+
__wbg_new_617a8cdb8bb1130e: function() {
|
|
120
|
+
const ret = new Object();
|
|
121
|
+
return ret;
|
|
122
|
+
},
|
|
123
|
+
__wbg_new_ee2291f50781bf1d: function() {
|
|
124
|
+
const ret = new Array();
|
|
125
|
+
return ret;
|
|
126
|
+
},
|
|
127
|
+
__wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
|
|
128
|
+
arg0[arg1] = arg2;
|
|
129
|
+
},
|
|
130
|
+
__wbg_set_bea140a88be9b277: function(arg0, arg1, arg2) {
|
|
131
|
+
arg0[arg1 >>> 0] = arg2;
|
|
132
|
+
},
|
|
133
|
+
__wbindgen_generic_0000000000000001: function(arg0) {
|
|
134
|
+
// Cast intrinsic for `F64 -> Externref`.
|
|
135
|
+
const ret = arg0;
|
|
136
|
+
return ret;
|
|
137
|
+
},
|
|
138
|
+
__wbindgen_generic_0000000000000002: function(arg0, arg1) {
|
|
139
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
140
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
141
|
+
return ret;
|
|
142
|
+
},
|
|
143
|
+
__wbindgen_generic_0000000000000003: function(arg0) {
|
|
144
|
+
// Cast intrinsic for `U64 -> Externref`.
|
|
145
|
+
const ret = BigInt.asUintN(64, arg0);
|
|
146
|
+
return ret;
|
|
147
|
+
},
|
|
148
|
+
__wbindgen_init_externref_table: function() {
|
|
149
|
+
const table = wasm.__wbindgen_externrefs;
|
|
150
|
+
const offset = table.grow(4);
|
|
151
|
+
table.set(0, undefined);
|
|
152
|
+
table.set(offset + 0, undefined);
|
|
153
|
+
table.set(offset + 1, null);
|
|
154
|
+
table.set(offset + 2, true);
|
|
155
|
+
table.set(offset + 3, false);
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
return {
|
|
159
|
+
__proto__: null,
|
|
160
|
+
"./emmurt_wasm_bg.js": import0,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function addToExternrefTable0(obj) {
|
|
165
|
+
const idx = wasm.__externref_table_alloc();
|
|
166
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
167
|
+
return idx;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
let cachedDataViewMemory0 = null;
|
|
171
|
+
function getDataViewMemory0() {
|
|
172
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
173
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
174
|
+
}
|
|
175
|
+
return cachedDataViewMemory0;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function getStringFromWasm0(ptr, len) {
|
|
179
|
+
return decodeText(ptr >>> 0, len);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let cachedUint8ArrayMemory0 = null;
|
|
183
|
+
function getUint8ArrayMemory0() {
|
|
184
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
185
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
186
|
+
}
|
|
187
|
+
return cachedUint8ArrayMemory0;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function handleError(f, args) {
|
|
191
|
+
try {
|
|
192
|
+
return f.apply(this, args);
|
|
193
|
+
} catch (e) {
|
|
194
|
+
const idx = addToExternrefTable0(e);
|
|
195
|
+
wasm.__wbindgen_exn_store(idx);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function isLikeNone(x) {
|
|
200
|
+
return x === undefined || x === null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
204
|
+
if (realloc === undefined) {
|
|
205
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
206
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
207
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
208
|
+
WASM_VECTOR_LEN = buf.length;
|
|
209
|
+
return ptr;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
let len = arg.length;
|
|
213
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
214
|
+
|
|
215
|
+
const mem = getUint8ArrayMemory0();
|
|
216
|
+
|
|
217
|
+
let offset = 0;
|
|
218
|
+
|
|
219
|
+
for (; offset < len; offset++) {
|
|
220
|
+
const code = arg.charCodeAt(offset);
|
|
221
|
+
if (code > 0x7F) break;
|
|
222
|
+
mem[ptr + offset] = code;
|
|
223
|
+
}
|
|
224
|
+
if (offset !== len) {
|
|
225
|
+
if (offset !== 0) {
|
|
226
|
+
arg = arg.slice(offset);
|
|
227
|
+
}
|
|
228
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
229
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
230
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
231
|
+
|
|
232
|
+
offset += ret.written;
|
|
233
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
WASM_VECTOR_LEN = offset;
|
|
237
|
+
return ptr;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
241
|
+
cachedTextDecoder.decode();
|
|
242
|
+
function decodeText(ptr, len) {
|
|
243
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const cachedTextEncoder = new TextEncoder();
|
|
247
|
+
|
|
248
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
249
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
250
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
251
|
+
view.set(buf);
|
|
252
|
+
return {
|
|
253
|
+
read: arg.length,
|
|
254
|
+
written: buf.length
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let WASM_VECTOR_LEN = 0;
|
|
260
|
+
|
|
261
|
+
const wasmPath = `${__dirname}/emmurt_wasm_bg.wasm`;
|
|
262
|
+
const wasmBytes = require('fs').readFileSync(wasmPath);
|
|
263
|
+
const wasmModule = new WebAssembly.Module(wasmBytes);
|
|
264
|
+
let wasmInstance = new WebAssembly.Instance(wasmModule, __wbg_get_imports());
|
|
265
|
+
let wasm = wasmInstance.exports;
|
|
266
|
+
wasm.__wbindgen_start();
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
/* @ts-self-types="./emmurt_wasm.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Compiles a page. `read(path)` returns the text of an included file, or
|
|
5
|
+
* null when it does not exist. `inline` writes `<fratm da>` files into the
|
|
6
|
+
* page instead of leaving them to a bundler.
|
|
7
|
+
* @param {string} source
|
|
8
|
+
* @param {string} filename
|
|
9
|
+
* @param {Function | null | undefined} read
|
|
10
|
+
* @param {boolean} inline
|
|
11
|
+
* @returns {any}
|
|
12
|
+
*/
|
|
13
|
+
export function compile(source, filename, read, inline) {
|
|
14
|
+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
15
|
+
const len0 = WASM_VECTOR_LEN;
|
|
16
|
+
const ptr1 = passStringToWasm0(filename, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
17
|
+
const len1 = WASM_VECTOR_LEN;
|
|
18
|
+
const ret = wasm.compile(ptr0, len0, ptr1, len1, isLikeNone(read) ? 0 : addToExternrefTable0(read), inline);
|
|
19
|
+
return ret;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Compiles a `.fratm` module to JavaScript.
|
|
24
|
+
* @param {string} source
|
|
25
|
+
* @param {string} filename
|
|
26
|
+
* @returns {any}
|
|
27
|
+
*/
|
|
28
|
+
export function compileFratm(source, filename) {
|
|
29
|
+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
30
|
+
const len0 = WASM_VECTOR_LEN;
|
|
31
|
+
const ptr1 = passStringToWasm0(filename, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
32
|
+
const len1 = WASM_VECTOR_LEN;
|
|
33
|
+
const ret = wasm.compileFratm(ptr0, len0, ptr1, len1);
|
|
34
|
+
return ret;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A JavaScript runtime error in Neapolitan, as FratmScript says it.
|
|
39
|
+
* @param {string} name
|
|
40
|
+
* @param {string} message
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/
|
|
43
|
+
export function runtimeError(name, message) {
|
|
44
|
+
let deferred3_0;
|
|
45
|
+
let deferred3_1;
|
|
46
|
+
try {
|
|
47
|
+
const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
48
|
+
const len0 = WASM_VECTOR_LEN;
|
|
49
|
+
const ptr1 = passStringToWasm0(message, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
50
|
+
const len1 = WASM_VECTOR_LEN;
|
|
51
|
+
const ret = wasm.runtimeError(ptr0, len0, ptr1, len1);
|
|
52
|
+
deferred3_0 = ret[0];
|
|
53
|
+
deferred3_1 = ret[1];
|
|
54
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
55
|
+
} finally {
|
|
56
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @returns {string}
|
|
62
|
+
*/
|
|
63
|
+
export function version() {
|
|
64
|
+
let deferred1_0;
|
|
65
|
+
let deferred1_1;
|
|
66
|
+
try {
|
|
67
|
+
const ret = wasm.version();
|
|
68
|
+
deferred1_0 = ret[0];
|
|
69
|
+
deferred1_1 = ret[1];
|
|
70
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
71
|
+
} finally {
|
|
72
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Tags, attributes and events, as JSON.
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
export function vocabulary() {
|
|
81
|
+
let deferred1_0;
|
|
82
|
+
let deferred1_1;
|
|
83
|
+
try {
|
|
84
|
+
const ret = wasm.vocabulary();
|
|
85
|
+
deferred1_0 = ret[0];
|
|
86
|
+
deferred1_1 = ret[1];
|
|
87
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
88
|
+
} finally {
|
|
89
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function __wbg_get_imports() {
|
|
93
|
+
const import0 = {
|
|
94
|
+
__proto__: null,
|
|
95
|
+
__wbg_Error_30c8987f7c2ed4e2: function(arg0, arg1) {
|
|
96
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
97
|
+
return ret;
|
|
98
|
+
},
|
|
99
|
+
__wbg___wbindgen_string_get_0380ccaa2f57f0d9: function(arg0, arg1) {
|
|
100
|
+
const obj = arg1;
|
|
101
|
+
const ret = typeof(obj) === 'string' ? obj : undefined;
|
|
102
|
+
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
103
|
+
var len1 = WASM_VECTOR_LEN;
|
|
104
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
105
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
106
|
+
},
|
|
107
|
+
__wbg___wbindgen_throw_41e9ee4f547fc59a: function(arg0, arg1) {
|
|
108
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
109
|
+
},
|
|
110
|
+
__wbg_call_187d372bd5fdd4aa: function() { return handleError(function (arg0, arg1, arg2) {
|
|
111
|
+
const ret = arg0.call(arg1, arg2);
|
|
112
|
+
return ret;
|
|
113
|
+
}, arguments); },
|
|
114
|
+
__wbg_new_617a8cdb8bb1130e: function() {
|
|
115
|
+
const ret = new Object();
|
|
116
|
+
return ret;
|
|
117
|
+
},
|
|
118
|
+
__wbg_new_ee2291f50781bf1d: function() {
|
|
119
|
+
const ret = new Array();
|
|
120
|
+
return ret;
|
|
121
|
+
},
|
|
122
|
+
__wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
|
|
123
|
+
arg0[arg1] = arg2;
|
|
124
|
+
},
|
|
125
|
+
__wbg_set_bea140a88be9b277: function(arg0, arg1, arg2) {
|
|
126
|
+
arg0[arg1 >>> 0] = arg2;
|
|
127
|
+
},
|
|
128
|
+
__wbindgen_generic_0000000000000001: function(arg0) {
|
|
129
|
+
// Cast intrinsic for `F64 -> Externref`.
|
|
130
|
+
const ret = arg0;
|
|
131
|
+
return ret;
|
|
132
|
+
},
|
|
133
|
+
__wbindgen_generic_0000000000000002: function(arg0, arg1) {
|
|
134
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
135
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
136
|
+
return ret;
|
|
137
|
+
},
|
|
138
|
+
__wbindgen_generic_0000000000000003: function(arg0) {
|
|
139
|
+
// Cast intrinsic for `U64 -> Externref`.
|
|
140
|
+
const ret = BigInt.asUintN(64, arg0);
|
|
141
|
+
return ret;
|
|
142
|
+
},
|
|
143
|
+
__wbindgen_init_externref_table: function() {
|
|
144
|
+
const table = wasm.__wbindgen_externrefs;
|
|
145
|
+
const offset = table.grow(4);
|
|
146
|
+
table.set(0, undefined);
|
|
147
|
+
table.set(offset + 0, undefined);
|
|
148
|
+
table.set(offset + 1, null);
|
|
149
|
+
table.set(offset + 2, true);
|
|
150
|
+
table.set(offset + 3, false);
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
return {
|
|
154
|
+
__proto__: null,
|
|
155
|
+
"./emmurt_wasm_bg.js": import0,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function addToExternrefTable0(obj) {
|
|
160
|
+
const idx = wasm.__externref_table_alloc();
|
|
161
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
162
|
+
return idx;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let cachedDataViewMemory0 = null;
|
|
166
|
+
function getDataViewMemory0() {
|
|
167
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
168
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
169
|
+
}
|
|
170
|
+
return cachedDataViewMemory0;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function getStringFromWasm0(ptr, len) {
|
|
174
|
+
return decodeText(ptr >>> 0, len);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let cachedUint8ArrayMemory0 = null;
|
|
178
|
+
function getUint8ArrayMemory0() {
|
|
179
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
180
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
181
|
+
}
|
|
182
|
+
return cachedUint8ArrayMemory0;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function handleError(f, args) {
|
|
186
|
+
try {
|
|
187
|
+
return f.apply(this, args);
|
|
188
|
+
} catch (e) {
|
|
189
|
+
const idx = addToExternrefTable0(e);
|
|
190
|
+
wasm.__wbindgen_exn_store(idx);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function isLikeNone(x) {
|
|
195
|
+
return x === undefined || x === null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
199
|
+
if (realloc === undefined) {
|
|
200
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
201
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
202
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
203
|
+
WASM_VECTOR_LEN = buf.length;
|
|
204
|
+
return ptr;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let len = arg.length;
|
|
208
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
209
|
+
|
|
210
|
+
const mem = getUint8ArrayMemory0();
|
|
211
|
+
|
|
212
|
+
let offset = 0;
|
|
213
|
+
|
|
214
|
+
for (; offset < len; offset++) {
|
|
215
|
+
const code = arg.charCodeAt(offset);
|
|
216
|
+
if (code > 0x7F) break;
|
|
217
|
+
mem[ptr + offset] = code;
|
|
218
|
+
}
|
|
219
|
+
if (offset !== len) {
|
|
220
|
+
if (offset !== 0) {
|
|
221
|
+
arg = arg.slice(offset);
|
|
222
|
+
}
|
|
223
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
224
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
225
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
226
|
+
|
|
227
|
+
offset += ret.written;
|
|
228
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
WASM_VECTOR_LEN = offset;
|
|
232
|
+
return ptr;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
236
|
+
cachedTextDecoder.decode();
|
|
237
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
238
|
+
let numBytesDecoded = 0;
|
|
239
|
+
function decodeText(ptr, len) {
|
|
240
|
+
numBytesDecoded += len;
|
|
241
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
242
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
243
|
+
cachedTextDecoder.decode();
|
|
244
|
+
numBytesDecoded = len;
|
|
245
|
+
}
|
|
246
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const cachedTextEncoder = new TextEncoder();
|
|
250
|
+
|
|
251
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
252
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
253
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
254
|
+
view.set(buf);
|
|
255
|
+
return {
|
|
256
|
+
read: arg.length,
|
|
257
|
+
written: buf.length
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
let WASM_VECTOR_LEN = 0;
|
|
263
|
+
|
|
264
|
+
let wasmModule, wasmInstance, wasm;
|
|
265
|
+
function __wbg_finalize_init(instance, module) {
|
|
266
|
+
wasmInstance = instance;
|
|
267
|
+
wasm = instance.exports;
|
|
268
|
+
wasmModule = module;
|
|
269
|
+
cachedDataViewMemory0 = null;
|
|
270
|
+
cachedUint8ArrayMemory0 = null;
|
|
271
|
+
wasm.__wbindgen_start();
|
|
272
|
+
return wasm;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function __wbg_load(module, imports) {
|
|
276
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
277
|
+
if (!module.ok) {
|
|
278
|
+
throw new Error(`failed to fetch Wasm: ${module.status} ${module.statusText} fetching '${module.url}'`);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
282
|
+
try {
|
|
283
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
284
|
+
} catch (e) {
|
|
285
|
+
const validResponse = expectedResponseType(module.type);
|
|
286
|
+
|
|
287
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
288
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
289
|
+
|
|
290
|
+
} else { throw e; }
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const bytes = await module.arrayBuffer();
|
|
295
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
296
|
+
} else {
|
|
297
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
298
|
+
|
|
299
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
300
|
+
return { instance, module };
|
|
301
|
+
} else {
|
|
302
|
+
return instance;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function expectedResponseType(type) {
|
|
307
|
+
switch (type) {
|
|
308
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function initSync(module) {
|
|
315
|
+
if (wasm !== undefined) return wasm;
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
if (module !== undefined) {
|
|
319
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
320
|
+
({module} = module)
|
|
321
|
+
} else {
|
|
322
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const imports = __wbg_get_imports();
|
|
327
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
328
|
+
module = new WebAssembly.Module(module);
|
|
329
|
+
}
|
|
330
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
331
|
+
return __wbg_finalize_init(instance, module);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
async function __wbg_init(module_or_path) {
|
|
335
|
+
if (wasm !== undefined) return wasm;
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
if (module_or_path !== undefined) {
|
|
339
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
340
|
+
({module_or_path} = module_or_path)
|
|
341
|
+
} else {
|
|
342
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (module_or_path === undefined) {
|
|
347
|
+
module_or_path = new URL('emmurt_wasm_bg.wasm', import.meta.url);
|
|
348
|
+
}
|
|
349
|
+
const imports = __wbg_get_imports();
|
|
350
|
+
|
|
351
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
352
|
+
module_or_path = fetch(module_or_path);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
356
|
+
|
|
357
|
+
return __wbg_finalize_init(instance, module);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
package/web.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type { EmmurtError, CompileResult, FratmResult, Vocabulary } from './index.js';
|
|
2
|
+
import type { CompileOptions, CompileResult, FratmResult, Vocabulary, EmmurtError } from './index.js';
|
|
3
|
+
|
|
4
|
+
/** Loads the WebAssembly module (by default the .wasm file next to this one). Call once, await it. */
|
|
5
|
+
export function init(input?: string | URL | Request | Response | BufferSource | WebAssembly.Module): Promise<void>;
|
|
6
|
+
/** Compiles htmlemmurt to HTML. Without `read`, includes cannot be resolved. */
|
|
7
|
+
export function compile(source: string, options?: Omit<CompileOptions, 'root'>): CompileResult;
|
|
8
|
+
export function compileFratm(source: string, options?: { filename?: string }): FratmResult;
|
|
9
|
+
export function vocabulary(): Vocabulary;
|
|
10
|
+
export function version(): string;
|
|
11
|
+
export function runtimeError(error: unknown): string;
|
|
12
|
+
export function formatError(error: EmmurtError): string;
|
package/web.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// htmlemmurt compiler for browsers. Call `init()` once before the rest.
|
|
2
|
+
import initWasm, * as wasm from './wasm/web/emmurt_wasm.js';
|
|
3
|
+
import { createApi } from './lib/api.js';
|
|
4
|
+
|
|
5
|
+
const api = createApi(wasm);
|
|
6
|
+
let ready = null;
|
|
7
|
+
let loaded = false;
|
|
8
|
+
|
|
9
|
+
export function init(input) {
|
|
10
|
+
ready ??= initWasm(input === undefined ? undefined : { module_or_path: input }).then(() => {
|
|
11
|
+
loaded = true;
|
|
12
|
+
});
|
|
13
|
+
return ready;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function loadedApi() {
|
|
17
|
+
if (!loaded) throw new Error('htmlemmurt/web: await init() before using the compiler');
|
|
18
|
+
return api;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const compile = (source, options) => loadedApi().compile(source, options);
|
|
22
|
+
export const compileFratm = (source, options) => loadedApi().compileFratm(source, options);
|
|
23
|
+
export const vocabulary = () => loadedApi().vocabulary();
|
|
24
|
+
export const version = () => loadedApi().version();
|
|
25
|
+
export const runtimeError = (error) => loadedApi().runtimeError(error);
|
|
26
|
+
export { formatError } from './lib/api.js';
|