@matthesketh/utopia-compiler 0.0.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.
@@ -0,0 +1,155 @@
1
+ /** A single top-level block extracted from the SFC. */
2
+ interface SFCBlock {
3
+ /** The raw content between the opening and closing tags. */
4
+ content: string;
5
+ /** Attributes on the opening tag. Boolean attrs have value `true`. */
6
+ attrs: Record<string, string | true>;
7
+ /** Byte offset of the opening tag's `<` in the original source. */
8
+ start: number;
9
+ /** Byte offset one past the closing tag's `>` in the original source. */
10
+ end: number;
11
+ }
12
+ /** The result of parsing a `.utopia` single-file component. */
13
+ interface SFCDescriptor {
14
+ template: SFCBlock | null;
15
+ script: SFCBlock | null;
16
+ style: SFCBlock | null;
17
+ filename: string;
18
+ }
19
+ /**
20
+ * Parse a `.utopia` SFC source string into its constituent blocks.
21
+ *
22
+ * The parser only looks for *top-level* `<template>`, `<script>`, and
23
+ * `<style>` tags. Anything else at the top level is silently ignored.
24
+ */
25
+ declare function parse(source: string, filename?: string): SFCDescriptor;
26
+ interface SourcePosition {
27
+ line: number;
28
+ column: number;
29
+ }
30
+ declare class SFCParseError extends Error {
31
+ filename: string;
32
+ position: SourcePosition;
33
+ constructor(message: string, filename: string, position: SourcePosition);
34
+ }
35
+
36
+ interface TemplateCompileOptions {
37
+ /** When set, every created element will receive this data attribute. */
38
+ scopeId?: string;
39
+ }
40
+ interface TemplateCompileResult {
41
+ /** The full ES-module source for the render function. */
42
+ code: string;
43
+ /** The set of @matthesketh/utopia-runtime helpers actually referenced. */
44
+ helpers: Set<string>;
45
+ }
46
+ /**
47
+ * Compile a raw HTML template string into a JavaScript render function module.
48
+ */
49
+ declare function compileTemplate(template: string, options?: TemplateCompileOptions): TemplateCompileResult;
50
+ declare const enum NodeType {
51
+ Element = 1,
52
+ Text = 2,
53
+ Interpolation = 3,
54
+ Comment = 4
55
+ }
56
+ interface ElementNode {
57
+ type: NodeType.Element;
58
+ tag: string;
59
+ attrs: Attribute[];
60
+ directives: Directive[];
61
+ children: TemplateNode[];
62
+ selfClosing: boolean;
63
+ }
64
+ interface TextNode {
65
+ type: NodeType.Text;
66
+ content: string;
67
+ }
68
+ interface InterpolationNode {
69
+ type: NodeType.Interpolation;
70
+ expression: string;
71
+ }
72
+ interface CommentNode {
73
+ type: NodeType.Comment;
74
+ content: string;
75
+ }
76
+ type TemplateNode = ElementNode | TextNode | InterpolationNode | CommentNode;
77
+ interface Attribute {
78
+ name: string;
79
+ value: string | null;
80
+ }
81
+ interface Directive {
82
+ kind: DirectiveKind;
83
+ arg: string | null;
84
+ expression: string;
85
+ modifiers: string[];
86
+ }
87
+ type DirectiveKind = 'on' | 'bind' | 'if' | 'for' | 'model';
88
+ /** Exported for testing — parse a template string into an AST. */
89
+ declare function parseTemplate(source: string): TemplateNode[];
90
+
91
+ interface StyleCompileOptions {
92
+ /** The raw CSS source. */
93
+ source: string;
94
+ /** The filename for the component (used to generate a stable hash). */
95
+ filename: string;
96
+ /** Whether the `<style>` block had the `scoped` attribute. */
97
+ scoped: boolean;
98
+ /** Override the scope ID (useful for testing). */
99
+ scopeId?: string;
100
+ }
101
+ interface StyleCompileResult {
102
+ /** The (possibly transformed) CSS. */
103
+ css: string;
104
+ /**
105
+ * The data attribute that must be added to every element in the component
106
+ * template so the scoped selectors match. `null` when not scoped.
107
+ */
108
+ scopeId: string | null;
109
+ }
110
+ /**
111
+ * Compile a `<style>` block, applying scoped transformations when required.
112
+ */
113
+ declare function compileStyle(options: StyleCompileOptions): StyleCompileResult;
114
+ /**
115
+ * Generate a deterministic scope ID from a filename.
116
+ *
117
+ * We use a simple DJB2-style hash (fast, no crypto dependency) and format it
118
+ * as `data-u-XXXXXXXX`.
119
+ */
120
+ declare function generateScopeId(filename: string): string;
121
+
122
+ interface CompileOptions {
123
+ /** Filename for error messages and scope-id generation. */
124
+ filename?: string;
125
+ /** Override the scope ID for testing. */
126
+ scopeId?: string;
127
+ }
128
+ interface CompileResult {
129
+ /** The compiled JavaScript module source. */
130
+ code: string;
131
+ /** Extracted CSS (with scoping applied if the style block is `scoped`). */
132
+ css: string;
133
+ /** Source map (reserved for future use). */
134
+ map?: unknown;
135
+ }
136
+ /**
137
+ * Compile a `.utopia` single-file component source string.
138
+ *
139
+ * Returns the generated JavaScript module code and CSS string.
140
+ *
141
+ * The generated JS module has the shape:
142
+ * ```js
143
+ * import { ... } from '@matthesketh/utopia-runtime'
144
+ *
145
+ * // <script> block contents (user code) inlined here
146
+ *
147
+ * export default function render(_ctx) { ... }
148
+ * ```
149
+ *
150
+ * The caller (e.g. the Vite plugin) is responsible for injecting the CSS
151
+ * into the page (via a `<style>` tag, or a CSS module import, etc.).
152
+ */
153
+ declare function compile(source: string, options?: CompileOptions): CompileResult;
154
+
155
+ export { type CompileOptions, type CompileResult, type SFCBlock, type SFCDescriptor, SFCParseError, type StyleCompileOptions, type StyleCompileResult, type TemplateCompileOptions, type TemplateCompileResult, compile, compileStyle, compileTemplate, generateScopeId, parse, parseTemplate };
@@ -0,0 +1,155 @@
1
+ /** A single top-level block extracted from the SFC. */
2
+ interface SFCBlock {
3
+ /** The raw content between the opening and closing tags. */
4
+ content: string;
5
+ /** Attributes on the opening tag. Boolean attrs have value `true`. */
6
+ attrs: Record<string, string | true>;
7
+ /** Byte offset of the opening tag's `<` in the original source. */
8
+ start: number;
9
+ /** Byte offset one past the closing tag's `>` in the original source. */
10
+ end: number;
11
+ }
12
+ /** The result of parsing a `.utopia` single-file component. */
13
+ interface SFCDescriptor {
14
+ template: SFCBlock | null;
15
+ script: SFCBlock | null;
16
+ style: SFCBlock | null;
17
+ filename: string;
18
+ }
19
+ /**
20
+ * Parse a `.utopia` SFC source string into its constituent blocks.
21
+ *
22
+ * The parser only looks for *top-level* `<template>`, `<script>`, and
23
+ * `<style>` tags. Anything else at the top level is silently ignored.
24
+ */
25
+ declare function parse(source: string, filename?: string): SFCDescriptor;
26
+ interface SourcePosition {
27
+ line: number;
28
+ column: number;
29
+ }
30
+ declare class SFCParseError extends Error {
31
+ filename: string;
32
+ position: SourcePosition;
33
+ constructor(message: string, filename: string, position: SourcePosition);
34
+ }
35
+
36
+ interface TemplateCompileOptions {
37
+ /** When set, every created element will receive this data attribute. */
38
+ scopeId?: string;
39
+ }
40
+ interface TemplateCompileResult {
41
+ /** The full ES-module source for the render function. */
42
+ code: string;
43
+ /** The set of @matthesketh/utopia-runtime helpers actually referenced. */
44
+ helpers: Set<string>;
45
+ }
46
+ /**
47
+ * Compile a raw HTML template string into a JavaScript render function module.
48
+ */
49
+ declare function compileTemplate(template: string, options?: TemplateCompileOptions): TemplateCompileResult;
50
+ declare const enum NodeType {
51
+ Element = 1,
52
+ Text = 2,
53
+ Interpolation = 3,
54
+ Comment = 4
55
+ }
56
+ interface ElementNode {
57
+ type: NodeType.Element;
58
+ tag: string;
59
+ attrs: Attribute[];
60
+ directives: Directive[];
61
+ children: TemplateNode[];
62
+ selfClosing: boolean;
63
+ }
64
+ interface TextNode {
65
+ type: NodeType.Text;
66
+ content: string;
67
+ }
68
+ interface InterpolationNode {
69
+ type: NodeType.Interpolation;
70
+ expression: string;
71
+ }
72
+ interface CommentNode {
73
+ type: NodeType.Comment;
74
+ content: string;
75
+ }
76
+ type TemplateNode = ElementNode | TextNode | InterpolationNode | CommentNode;
77
+ interface Attribute {
78
+ name: string;
79
+ value: string | null;
80
+ }
81
+ interface Directive {
82
+ kind: DirectiveKind;
83
+ arg: string | null;
84
+ expression: string;
85
+ modifiers: string[];
86
+ }
87
+ type DirectiveKind = 'on' | 'bind' | 'if' | 'for' | 'model';
88
+ /** Exported for testing — parse a template string into an AST. */
89
+ declare function parseTemplate(source: string): TemplateNode[];
90
+
91
+ interface StyleCompileOptions {
92
+ /** The raw CSS source. */
93
+ source: string;
94
+ /** The filename for the component (used to generate a stable hash). */
95
+ filename: string;
96
+ /** Whether the `<style>` block had the `scoped` attribute. */
97
+ scoped: boolean;
98
+ /** Override the scope ID (useful for testing). */
99
+ scopeId?: string;
100
+ }
101
+ interface StyleCompileResult {
102
+ /** The (possibly transformed) CSS. */
103
+ css: string;
104
+ /**
105
+ * The data attribute that must be added to every element in the component
106
+ * template so the scoped selectors match. `null` when not scoped.
107
+ */
108
+ scopeId: string | null;
109
+ }
110
+ /**
111
+ * Compile a `<style>` block, applying scoped transformations when required.
112
+ */
113
+ declare function compileStyle(options: StyleCompileOptions): StyleCompileResult;
114
+ /**
115
+ * Generate a deterministic scope ID from a filename.
116
+ *
117
+ * We use a simple DJB2-style hash (fast, no crypto dependency) and format it
118
+ * as `data-u-XXXXXXXX`.
119
+ */
120
+ declare function generateScopeId(filename: string): string;
121
+
122
+ interface CompileOptions {
123
+ /** Filename for error messages and scope-id generation. */
124
+ filename?: string;
125
+ /** Override the scope ID for testing. */
126
+ scopeId?: string;
127
+ }
128
+ interface CompileResult {
129
+ /** The compiled JavaScript module source. */
130
+ code: string;
131
+ /** Extracted CSS (with scoping applied if the style block is `scoped`). */
132
+ css: string;
133
+ /** Source map (reserved for future use). */
134
+ map?: unknown;
135
+ }
136
+ /**
137
+ * Compile a `.utopia` single-file component source string.
138
+ *
139
+ * Returns the generated JavaScript module code and CSS string.
140
+ *
141
+ * The generated JS module has the shape:
142
+ * ```js
143
+ * import { ... } from '@matthesketh/utopia-runtime'
144
+ *
145
+ * // <script> block contents (user code) inlined here
146
+ *
147
+ * export default function render(_ctx) { ... }
148
+ * ```
149
+ *
150
+ * The caller (e.g. the Vite plugin) is responsible for injecting the CSS
151
+ * into the page (via a `<style>` tag, or a CSS module import, etc.).
152
+ */
153
+ declare function compile(source: string, options?: CompileOptions): CompileResult;
154
+
155
+ export { type CompileOptions, type CompileResult, type SFCBlock, type SFCDescriptor, SFCParseError, type StyleCompileOptions, type StyleCompileResult, type TemplateCompileOptions, type TemplateCompileResult, compile, compileStyle, compileTemplate, generateScopeId, parse, parseTemplate };