@astrojs/compiler 0.16.0 → 0.17.0-pre.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/CHANGELOG.md +6 -0
- package/astro.wasm +0 -0
- package/browser/index.d.ts +4 -0
- package/browser/index.js +55 -0
- package/browser/utils.d.ts +25 -0
- package/browser/utils.js +116 -0
- package/browser/wasm_exec.d.ts +35 -0
- package/browser/wasm_exec.js +559 -0
- package/node/index.d.ts +6 -0
- package/node/index.js +57 -0
- package/node/utils.d.ts +25 -0
- package/node/utils.js +116 -0
- package/node/wasm_exec.d.ts +35 -0
- package/node/wasm_exec.js +451 -0
- package/package.json +5 -5
- package/shared/ast.d.ts +71 -0
- package/shared/ast.js +1 -0
- package/shared/types.d.ts +61 -0
- package/shared/types.js +1 -0
- package/LICENSE +0 -53
package/CHANGELOG.md
CHANGED
package/astro.wasm
CHANGED
|
Binary file
|
package/browser/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import Go from './wasm_exec.js';
|
|
2
|
+
export const transform = (input, options) => {
|
|
3
|
+
return ensureServiceIsRunning().transform(input, options);
|
|
4
|
+
};
|
|
5
|
+
export const parse = (input, options) => {
|
|
6
|
+
return ensureServiceIsRunning().parse(input, options);
|
|
7
|
+
};
|
|
8
|
+
let initializePromise;
|
|
9
|
+
let longLivedService;
|
|
10
|
+
export const initialize = async (options) => {
|
|
11
|
+
let wasmURL = options.wasmURL;
|
|
12
|
+
if (!wasmURL)
|
|
13
|
+
throw new Error('Must provide the "wasmURL" option');
|
|
14
|
+
wasmURL += '';
|
|
15
|
+
if (!initializePromise) {
|
|
16
|
+
initializePromise = startRunningService(wasmURL).catch((err) => {
|
|
17
|
+
// Let the caller try again if this fails.
|
|
18
|
+
initializePromise = void 0;
|
|
19
|
+
// But still, throw the error back up the caller.
|
|
20
|
+
throw err;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
longLivedService = longLivedService || (await initializePromise);
|
|
24
|
+
};
|
|
25
|
+
let ensureServiceIsRunning = () => {
|
|
26
|
+
if (!initializePromise)
|
|
27
|
+
throw new Error('You need to call "initialize" before calling this');
|
|
28
|
+
if (!longLivedService)
|
|
29
|
+
throw new Error('You need to wait for the promise returned from "initialize" to be resolved before calling this');
|
|
30
|
+
return longLivedService;
|
|
31
|
+
};
|
|
32
|
+
const instantiateWASM = async (wasmURL, importObject) => {
|
|
33
|
+
let response = undefined;
|
|
34
|
+
if (WebAssembly.instantiateStreaming) {
|
|
35
|
+
response = await WebAssembly.instantiateStreaming(fetch(wasmURL), importObject);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const fetchAndInstantiateTask = async () => {
|
|
39
|
+
const wasmArrayBuffer = await fetch(wasmURL).then((res) => res.arrayBuffer());
|
|
40
|
+
return WebAssembly.instantiate(wasmArrayBuffer, importObject);
|
|
41
|
+
};
|
|
42
|
+
response = await fetchAndInstantiateTask();
|
|
43
|
+
}
|
|
44
|
+
return response;
|
|
45
|
+
};
|
|
46
|
+
const startRunningService = async (wasmURL) => {
|
|
47
|
+
const go = new Go();
|
|
48
|
+
const wasm = await instantiateWASM(wasmURL, go.importObject);
|
|
49
|
+
go.run(wasm.instance);
|
|
50
|
+
const service = globalThis['@astrojs/compiler'];
|
|
51
|
+
return {
|
|
52
|
+
transform: (input, options) => new Promise((resolve) => resolve(service.transform(input, options || {}))),
|
|
53
|
+
parse: (input, options) => new Promise((resolve) => resolve(service.parse(input, options || {}))).then((result) => ({ ...result, ast: JSON.parse(result.ast) })),
|
|
54
|
+
};
|
|
55
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Node, ParentNode, RootNode, LiteralNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, TagLikeNode, ExpressionNode, TextNode, CommentNode, DoctypeNode, FrontmatterNode } from '../shared/ast';
|
|
2
|
+
export interface Visitor {
|
|
3
|
+
(node: Node, parent?: ParentNode, index?: number): void | Promise<void>;
|
|
4
|
+
}
|
|
5
|
+
export declare const is: {
|
|
6
|
+
parent(node: Node): node is ParentNode;
|
|
7
|
+
literal(node: Node): node is LiteralNode;
|
|
8
|
+
tag(node: Node): node is TagLikeNode;
|
|
9
|
+
whitespace(node: Node): node is TextNode;
|
|
10
|
+
root: (node: Node) => node is RootNode;
|
|
11
|
+
element: (node: Node) => node is ElementNode;
|
|
12
|
+
customElement: (node: Node) => node is CustomElementNode;
|
|
13
|
+
component: (node: Node) => node is ComponentNode;
|
|
14
|
+
fragment: (node: Node) => node is FragmentNode;
|
|
15
|
+
expression: (node: Node) => node is ExpressionNode;
|
|
16
|
+
text: (node: Node) => node is TextNode;
|
|
17
|
+
doctype: (node: Node) => node is DoctypeNode;
|
|
18
|
+
comment: (node: Node) => node is CommentNode;
|
|
19
|
+
frontmatter: (node: Node) => node is FrontmatterNode;
|
|
20
|
+
};
|
|
21
|
+
export declare function walk(node: ParentNode, callback: Visitor): void;
|
|
22
|
+
export interface SerializeOtions {
|
|
23
|
+
selfClose: boolean;
|
|
24
|
+
}
|
|
25
|
+
export declare function serialize(root: Node, opts?: SerializeOtions): string;
|
package/browser/utils.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
function guard(type) {
|
|
2
|
+
return (node) => node.type === type;
|
|
3
|
+
}
|
|
4
|
+
export const is = {
|
|
5
|
+
parent(node) {
|
|
6
|
+
return Array.isArray(node.children);
|
|
7
|
+
},
|
|
8
|
+
literal(node) {
|
|
9
|
+
return typeof node.value === 'string';
|
|
10
|
+
},
|
|
11
|
+
tag(node) {
|
|
12
|
+
return node.type === 'element' || node.type === 'custom-element' || node.type === 'component' || node.type === 'fragment';
|
|
13
|
+
},
|
|
14
|
+
whitespace(node) {
|
|
15
|
+
return node.type === 'text' && node.value.trim().length === 0;
|
|
16
|
+
},
|
|
17
|
+
root: guard('root'),
|
|
18
|
+
element: guard('element'),
|
|
19
|
+
customElement: guard('custom-element'),
|
|
20
|
+
component: guard('component'),
|
|
21
|
+
fragment: guard('fragment'),
|
|
22
|
+
expression: guard('expression'),
|
|
23
|
+
text: guard('text'),
|
|
24
|
+
doctype: guard('doctype'),
|
|
25
|
+
comment: guard('comment'),
|
|
26
|
+
frontmatter: guard('frontmatter'),
|
|
27
|
+
};
|
|
28
|
+
class Walker {
|
|
29
|
+
constructor(callback) {
|
|
30
|
+
this.callback = callback;
|
|
31
|
+
}
|
|
32
|
+
async visit(node, parent, index) {
|
|
33
|
+
await this.callback(node, parent, index);
|
|
34
|
+
if (is.parent(node)) {
|
|
35
|
+
let promises = [];
|
|
36
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
37
|
+
const child = node.children[i];
|
|
38
|
+
promises.push(this.callback(child, node, i));
|
|
39
|
+
}
|
|
40
|
+
await Promise.all(promises);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function walk(node, callback) {
|
|
45
|
+
const walker = new Walker(callback);
|
|
46
|
+
walker.visit(node);
|
|
47
|
+
}
|
|
48
|
+
function serializeAttributes(node) {
|
|
49
|
+
let output = '';
|
|
50
|
+
for (const attr of node.attributes) {
|
|
51
|
+
output += ' ';
|
|
52
|
+
switch (attr.kind) {
|
|
53
|
+
case 'empty': {
|
|
54
|
+
output += `${attr.name}`;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
case 'expression': {
|
|
58
|
+
output += `${attr.name}={${attr.value}}`;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
case 'quoted': {
|
|
62
|
+
output += `${attr.name}="${attr.value}"`;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
case 'template-literal': {
|
|
66
|
+
output += `${attr.name}=\`${attr.value}\``;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
case 'shorthand': {
|
|
70
|
+
output += `{${attr.name}}`;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case 'spread': {
|
|
74
|
+
output += `{...${attr.value}}`;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return output;
|
|
80
|
+
}
|
|
81
|
+
export function serialize(root, opts = { selfClose: true }) {
|
|
82
|
+
let output = '';
|
|
83
|
+
function visitor(node) {
|
|
84
|
+
if (is.root(node)) {
|
|
85
|
+
node.children.forEach((child) => visitor(child));
|
|
86
|
+
}
|
|
87
|
+
else if (is.frontmatter(node)) {
|
|
88
|
+
output += `---${node.value}---\n\n`;
|
|
89
|
+
}
|
|
90
|
+
else if (is.comment(node)) {
|
|
91
|
+
output += `<!--${node.value}-->`;
|
|
92
|
+
}
|
|
93
|
+
else if (is.expression(node)) {
|
|
94
|
+
output += `{`;
|
|
95
|
+
node.children.forEach((child) => visitor(child));
|
|
96
|
+
output += `}`;
|
|
97
|
+
}
|
|
98
|
+
else if (is.literal(node)) {
|
|
99
|
+
output += node.value;
|
|
100
|
+
}
|
|
101
|
+
else if (is.tag(node)) {
|
|
102
|
+
output += `<${node.name}`;
|
|
103
|
+
output += serializeAttributes(node);
|
|
104
|
+
if (node.children.length == 0 && opts.selfClose) {
|
|
105
|
+
output += ` />`;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
output += '>';
|
|
109
|
+
node.children.forEach((child) => visitor(child));
|
|
110
|
+
output += `</${node.name}>`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
visitor(root);
|
|
115
|
+
return output;
|
|
116
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export default class Go {
|
|
2
|
+
importObject: {
|
|
3
|
+
go: {
|
|
4
|
+
'runtime.wasmExit': (sp: any) => void;
|
|
5
|
+
'runtime.wasmWrite': (sp: any) => void;
|
|
6
|
+
'runtime.resetMemoryDataView': (sp: any) => void;
|
|
7
|
+
'runtime.nanotime1': (sp: any) => void;
|
|
8
|
+
'runtime.walltime': (sp: any) => void;
|
|
9
|
+
'runtime.scheduleTimeoutEvent': (sp: any) => void;
|
|
10
|
+
'runtime.clearTimeoutEvent': (sp: any) => void;
|
|
11
|
+
'runtime.getRandomData': (sp: any) => void;
|
|
12
|
+
'syscall/js.finalizeRef': (sp: any) => void;
|
|
13
|
+
'syscall/js.stringVal': (sp: any) => void;
|
|
14
|
+
'syscall/js.valueGet': (sp: any) => void;
|
|
15
|
+
'syscall/js.valueSet': (sp: any) => void;
|
|
16
|
+
'syscall/js.valueDelete': (sp: any) => void;
|
|
17
|
+
'syscall/js.valueIndex': (sp: any) => void;
|
|
18
|
+
'syscall/js.valueSetIndex': (sp: any) => void;
|
|
19
|
+
'syscall/js.valueCall': (sp: any) => void;
|
|
20
|
+
'syscall/js.valueInvoke': (sp: any) => void;
|
|
21
|
+
'syscall/js.valueNew': (sp: any) => void;
|
|
22
|
+
'syscall/js.valueLength': (sp: any) => void;
|
|
23
|
+
'syscall/js.valuePrepareString': (sp: any) => void;
|
|
24
|
+
'syscall/js.valueLoadString': (sp: any) => void;
|
|
25
|
+
'syscall/js.valueInstanceOf': (sp: any) => void;
|
|
26
|
+
'syscall/js.copyBytesToGo': (sp: any) => void;
|
|
27
|
+
'syscall/js.copyBytesToJS': (sp: any) => void;
|
|
28
|
+
debug: (value: any) => void;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
constructor();
|
|
32
|
+
run(instance: any): Promise<void>;
|
|
33
|
+
private _resume;
|
|
34
|
+
private _makeFuncWrapper;
|
|
35
|
+
}
|