@astrojs/compiler 0.12.0 → 0.13.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 +25 -0
- package/astro.wasm +0 -0
- package/browser/utils.d.ts +6 -2
- package/browser/utils.js +69 -0
- package/node/utils.d.ts +6 -2
- package/node/utils.js +69 -0
- package/package.json +1 -1
- package/shared/ast.d.ts +1 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# @astrojs/compiler
|
|
2
2
|
|
|
3
|
+
## 0.13.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 2f8334c: Update `parse` and `serialize` functions to combine `attributes` and `directives`, fix issue with `serialize` not respecting `attributes`.
|
|
8
|
+
- b308955: Add self-close option to serialize util
|
|
9
|
+
|
|
10
|
+
## 0.13.0
|
|
11
|
+
|
|
12
|
+
### Minor Changes
|
|
13
|
+
|
|
14
|
+
- ce3f1a5: Update CSS parser to use `esbuild`, adding support for CSS nesting, `@container`, `@layer`, and other modern syntax features
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- 24a1185: Parser: Always output the `children` property in an element node, even if it has no children
|
|
19
|
+
|
|
20
|
+
## 0.12.1
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- 097ac47: Parser: Always output the `attribute` property in an element node, even if empty
|
|
25
|
+
- ad62437: Add `serialize` util
|
|
26
|
+
- eb7eb95: Parse: fix escaping of `&` characters in AST output
|
|
27
|
+
|
|
3
28
|
## 0.12.0
|
|
4
29
|
|
|
5
30
|
### Minor Changes
|
package/astro.wasm
CHANGED
|
Binary file
|
package/browser/utils.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Node, ParentNode, RootNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, LiteralNode, ExpressionNode, TextNode, CommentNode, DoctypeNode, FrontmatterNode } from '../shared/ast';
|
|
1
|
+
import { Node, ParentNode, RootNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, LiteralNode, TagLikeNode, ExpressionNode, TextNode, CommentNode, DoctypeNode, FrontmatterNode } from '../shared/ast';
|
|
2
2
|
export interface Visitor {
|
|
3
3
|
(node: Node, parent?: ParentNode, index?: number): void | Promise<void>;
|
|
4
4
|
}
|
|
5
5
|
export declare const is: {
|
|
6
6
|
parent(node: Node): node is ParentNode;
|
|
7
7
|
literal(node: Node): node is LiteralNode;
|
|
8
|
-
tag(node: Node): node is
|
|
8
|
+
tag(node: Node): node is TagLikeNode;
|
|
9
9
|
whitespace(node: Node): node is TextNode;
|
|
10
10
|
root: (node: Node) => node is RootNode;
|
|
11
11
|
element: (node: Node) => node is ElementNode;
|
|
@@ -19,3 +19,7 @@ export declare const is: {
|
|
|
19
19
|
frontmatter: (node: Node) => node is FrontmatterNode;
|
|
20
20
|
};
|
|
21
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
CHANGED
|
@@ -45,3 +45,72 @@ export function walk(node, callback) {
|
|
|
45
45
|
const walker = new Walker(callback);
|
|
46
46
|
walker.visit(node);
|
|
47
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
|
+
}
|
package/node/utils.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Node, ParentNode, RootNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, LiteralNode, ExpressionNode, TextNode, CommentNode, DoctypeNode, FrontmatterNode } from '../shared/ast';
|
|
1
|
+
import { Node, ParentNode, RootNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, LiteralNode, TagLikeNode, ExpressionNode, TextNode, CommentNode, DoctypeNode, FrontmatterNode } from '../shared/ast';
|
|
2
2
|
export interface Visitor {
|
|
3
3
|
(node: Node, parent?: ParentNode, index?: number): void | Promise<void>;
|
|
4
4
|
}
|
|
5
5
|
export declare const is: {
|
|
6
6
|
parent(node: Node): node is ParentNode;
|
|
7
7
|
literal(node: Node): node is LiteralNode;
|
|
8
|
-
tag(node: Node): node is
|
|
8
|
+
tag(node: Node): node is TagLikeNode;
|
|
9
9
|
whitespace(node: Node): node is TextNode;
|
|
10
10
|
root: (node: Node) => node is RootNode;
|
|
11
11
|
element: (node: Node) => node is ElementNode;
|
|
@@ -19,3 +19,7 @@ export declare const is: {
|
|
|
19
19
|
frontmatter: (node: Node) => node is FrontmatterNode;
|
|
20
20
|
};
|
|
21
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/node/utils.js
CHANGED
|
@@ -45,3 +45,72 @@ export function walk(node, callback) {
|
|
|
45
45
|
const walker = new Walker(callback);
|
|
46
46
|
walker.visit(node);
|
|
47
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
|
+
}
|
package/package.json
CHANGED
package/shared/ast.d.ts
CHANGED
|
@@ -33,9 +33,6 @@ export interface AttributeNode extends BaseNode {
|
|
|
33
33
|
name: string;
|
|
34
34
|
value: string;
|
|
35
35
|
}
|
|
36
|
-
export interface DirectiveNode extends Omit<AttributeNode, 'type'> {
|
|
37
|
-
type: 'directive';
|
|
38
|
-
}
|
|
39
36
|
export interface TextNode extends LiteralNode {
|
|
40
37
|
type: 'text';
|
|
41
38
|
}
|
|
@@ -43,26 +40,23 @@ export interface ElementNode extends ParentLikeNode {
|
|
|
43
40
|
type: 'element';
|
|
44
41
|
name: string;
|
|
45
42
|
attributes: AttributeNode[];
|
|
46
|
-
directives: DirectiveNode[];
|
|
47
43
|
}
|
|
48
44
|
export interface FragmentNode extends ParentLikeNode {
|
|
49
45
|
type: 'fragment';
|
|
50
46
|
name: string;
|
|
51
47
|
attributes: AttributeNode[];
|
|
52
|
-
directives: DirectiveNode[];
|
|
53
48
|
}
|
|
54
49
|
export interface ComponentNode extends ParentLikeNode {
|
|
55
50
|
type: 'component';
|
|
56
51
|
name: string;
|
|
57
52
|
attributes: AttributeNode[];
|
|
58
|
-
directives: DirectiveNode[];
|
|
59
53
|
}
|
|
60
54
|
export interface CustomElementNode extends ParentLikeNode {
|
|
61
55
|
type: 'custom-element';
|
|
62
56
|
name: string;
|
|
63
57
|
attributes: AttributeNode[];
|
|
64
|
-
directives: DirectiveNode[];
|
|
65
58
|
}
|
|
59
|
+
export declare type TagLikeNode = ElementNode | FragmentNode | ComponentNode | CustomElementNode;
|
|
66
60
|
export interface DoctypeNode extends LiteralNode {
|
|
67
61
|
type: 'doctype';
|
|
68
62
|
}
|