@slim-lang/core 1.2.8 → 1.2.10
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/README.md +19 -1
- package/package.json +1 -1
- package/src/external/defaults.js +3 -5
- package/src/external/helpers.js +23 -0
- package/src/handlers/parser/components.js +64 -19
package/README.md
CHANGED
|
@@ -40,7 +40,25 @@ Requirements:
|
|
|
40
40
|
- Node.js 22+
|
|
41
41
|
- Git
|
|
42
42
|
|
|
43
|
-
## Installation
|
|
43
|
+
## NPM Installation
|
|
44
|
+
|
|
45
|
+
Create your project, then run:
|
|
46
|
+
```console
|
|
47
|
+
npm i @slim-lang/core
|
|
48
|
+
```
|
|
49
|
+
```console
|
|
50
|
+
npm link
|
|
51
|
+
```
|
|
52
|
+
Make sure the installation went smoothly:
|
|
53
|
+
```console
|
|
54
|
+
npm view @slim-lang/core version
|
|
55
|
+
```
|
|
56
|
+
```console
|
|
57
|
+
slmc --version
|
|
58
|
+
spm --version
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Source Installation
|
|
44
62
|
Create a project folder, then run:
|
|
45
63
|
|
|
46
64
|
- Clone project
|
package/package.json
CHANGED
package/src/external/defaults.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// Full runtime for components and Node error frames; pure code uses core.js.
|
|
2
|
-
|
|
3
1
|
import fs from "fs"
|
|
4
2
|
import { setSourceReader } from "./classErrors.js"
|
|
5
3
|
import {
|
|
@@ -11,15 +9,15 @@ import {
|
|
|
11
9
|
__lifecycle__,
|
|
12
10
|
__create_host__,
|
|
13
11
|
__adopt_into__,
|
|
14
|
-
__define_element__
|
|
12
|
+
__define_element__,
|
|
13
|
+
__resolve_bem__
|
|
15
14
|
} from "./helpers.js"
|
|
16
15
|
|
|
17
16
|
export * from "./core.js"
|
|
18
17
|
|
|
19
|
-
// The bare fs import lets client bundles remove source-frame support.
|
|
20
18
|
setSourceReader(file => fs.readFileSync(file, "utf8"))
|
|
21
19
|
|
|
22
20
|
Object.assign(globalThis, {
|
|
23
21
|
htmlToVdom, HTMLElement, __html__, __flush_events__, __bind_events__, __lifecycle__,
|
|
24
|
-
__create_host__, __adopt_into__, __define_element__
|
|
22
|
+
__create_host__, __adopt_into__, __define_element__, __resolve_bem__
|
|
25
23
|
})
|
package/src/external/helpers.js
CHANGED
|
@@ -522,6 +522,29 @@ export function htmlToElement(input, doc = slimDocument) {
|
|
|
522
522
|
return vnodeToElement(htmlToVdom(input), doc);
|
|
523
523
|
}
|
|
524
524
|
|
|
525
|
+
export function __resolve_bem__(vnode, scope = "") {
|
|
526
|
+
if (!vnode || typeof vnode !== "object") return vnode;
|
|
527
|
+
|
|
528
|
+
if (vnode.type === "fragment") {
|
|
529
|
+
for (const child of vnode.children || []) __resolve_bem__(child, scope);
|
|
530
|
+
return vnode;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if (vnode.type === "element") {
|
|
534
|
+
let childScope = scope;
|
|
535
|
+
const cls = vnode.attrs && vnode.attrs.class;
|
|
536
|
+
|
|
537
|
+
if (typeof cls === "string" && cls.length) {
|
|
538
|
+
if (cls.includes("&")) vnode.attrs.class = cls.replaceAll("&", scope);
|
|
539
|
+
childScope = vnode.attrs.class.trim().split(/\s+/)[0] || scope;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
for (const child of vnode.children || []) __resolve_bem__(child, childScope);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return vnode;
|
|
546
|
+
}
|
|
547
|
+
|
|
525
548
|
export function htmlToVdom(input) {
|
|
526
549
|
if (input instanceof VNode) return input;
|
|
527
550
|
|
|
@@ -1,9 +1,37 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
2
|
+
import path from "node:path"
|
|
1
3
|
import { tokenize } from "../../lexer.js"
|
|
4
|
+
import { parseTypedArgs, buildTypedArgsResult } from "../parserHandler.js"
|
|
5
|
+
|
|
6
|
+
let bemCache = null
|
|
7
|
+
function useBEMClasses() {
|
|
8
|
+
if (bemCache !== null) return bemCache
|
|
9
|
+
try {
|
|
10
|
+
const cfg = JSON.parse(fs.readFileSync(path.join(process.cwd(), "slimconfig.json"), "utf8"))
|
|
11
|
+
bemCache = !!(cfg.components && cfg.components.useBEMClasses === true)
|
|
12
|
+
} catch {
|
|
13
|
+
bemCache = false
|
|
14
|
+
}
|
|
15
|
+
return bemCache
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function kebabCase(name) {
|
|
19
|
+
return name
|
|
20
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
21
|
+
.replace(/[_\s]+/g, "-")
|
|
22
|
+
.toLowerCase()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The expression that turns the template into a DOM element, resolving `&`
|
|
26
|
+
// parent references first when BEM classes are enabled.
|
|
27
|
+
function elementExpr(html, name) {
|
|
28
|
+
if (!useBEMClasses()) return `htmlToVdom(__html__\`${html}\`).toElement()`
|
|
29
|
+
return `(() => { const __v__ = htmlToVdom(__html__\`${html}\`); __resolve_bem__(__v__, ${JSON.stringify(kebabCase(name))}); return __v__.toElement(); })()`
|
|
30
|
+
}
|
|
2
31
|
|
|
3
32
|
const OPENERS = new Set(["(", "[", "{", "${"])
|
|
4
33
|
const CLOSERS = new Set([")", "]", "}"])
|
|
5
34
|
|
|
6
|
-
// Find the top-level template return, skipping setup callbacks.
|
|
7
35
|
function templateReturnStart(body) {
|
|
8
36
|
let depth = 0
|
|
9
37
|
for (const t of tokenize(body)) {
|
|
@@ -91,19 +119,33 @@ function parseComponents(code) {
|
|
|
91
119
|
return out + code.slice(cursor);
|
|
92
120
|
}
|
|
93
121
|
|
|
94
|
-
function
|
|
122
|
+
function parseComponentArgs(name, args) {
|
|
95
123
|
const trimmed = args.trim();
|
|
96
124
|
|
|
97
|
-
if (!trimmed) return "";
|
|
98
|
-
|
|
99
|
-
|
|
125
|
+
if (!trimmed) return { binding: "", checks: "" };
|
|
126
|
+
|
|
127
|
+
let parsed, binding;
|
|
100
128
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
129
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
|
|
130
|
+
parsed = parseTypedArgs(trimmed.slice(1, -1));
|
|
131
|
+
const fields = parsed.map(a => a.default !== null ? `${a.name} = ${a.default}` : a.name);
|
|
132
|
+
binding = `{ ${fields.join(", ")} }`;
|
|
133
|
+
} else {
|
|
134
|
+
parsed = parseTypedArgs(trimmed);
|
|
135
|
+
|
|
136
|
+
if (parsed.length !== 1 || !/^[A-Za-z_$][\w$]*$/.test(parsed[0].name)) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Component "${name}" arguments must be a single object name (e.g. "props" or "props: SomeStruct") or a destructuring pattern (e.g. "{ id: int }"), got: "${trimmed}"`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
binding = parsed[0].name;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const { checks } = buildTypedArgsResult(parsed, name);
|
|
146
|
+
return { binding, checks };
|
|
104
147
|
}
|
|
105
148
|
|
|
106
|
-
// Escape template text without changing nested interpolations.
|
|
107
149
|
function escapeTemplateBackticks(s) {
|
|
108
150
|
const n = s.length;
|
|
109
151
|
let out = "";
|
|
@@ -154,10 +196,10 @@ function elementTag(name, explicit) {
|
|
|
154
196
|
return `slim-${kebab}`;
|
|
155
197
|
}
|
|
156
198
|
|
|
157
|
-
|
|
158
|
-
function buildElementComponent(name, binding, before, html, explicitTag) {
|
|
199
|
+
function buildElementComponent(name, binding, before, html, explicitTag, checks = "") {
|
|
159
200
|
const tag = elementTag(name, explicitTag);
|
|
160
201
|
const param = binding ? `${binding} = {}` : "__unused__ = {}";
|
|
202
|
+
const checkPrelude = checks ? `${checks};\n` : "";
|
|
161
203
|
const quoted = JSON.stringify(tag);
|
|
162
204
|
|
|
163
205
|
return `
|
|
@@ -172,8 +214,8 @@ function buildElementComponent(name, binding, before, html, explicitTag) {
|
|
|
172
214
|
const onMount = (fn) => __mounts__.push(fn);
|
|
173
215
|
const onConnect = (fn) => __connects__.push(fn);
|
|
174
216
|
const onUnmount = (fn) => __unmounts__.push(fn);
|
|
175
|
-
${before};
|
|
176
|
-
const __el__ =
|
|
217
|
+
${checkPrelude}${before};
|
|
218
|
+
const __el__ = ${elementExpr(html, name)};
|
|
177
219
|
|
|
178
220
|
if (!__host__) {
|
|
179
221
|
for (const fn of __mounts__) fn(__el__);
|
|
@@ -203,11 +245,12 @@ function buildComponent(name, args, body, modifier = null, tag = null) {
|
|
|
203
245
|
template = template.slice(1, -1).trim();
|
|
204
246
|
}
|
|
205
247
|
const html = escapeTemplateBackticks(template);
|
|
206
|
-
const binding =
|
|
248
|
+
const { binding, checks } = parseComponentArgs(name, args);
|
|
207
249
|
const param = binding ? `${binding} = {}` : "";
|
|
250
|
+
const checkPrelude = checks ? `${checks};\n` : "";
|
|
208
251
|
|
|
209
252
|
if (modifier === "element") {
|
|
210
|
-
return buildElementComponent(name, binding, before, html, tag);
|
|
253
|
+
return buildElementComponent(name, binding, before, html, tag, checks);
|
|
211
254
|
}
|
|
212
255
|
|
|
213
256
|
const isolated = modifier === "isolated";
|
|
@@ -222,8 +265,8 @@ function buildComponent(name, args, body, modifier = null, tag = null) {
|
|
|
222
265
|
const onMount = (fn) => __mounts__.push(fn);
|
|
223
266
|
const onConnect = (fn) => __connects__.push(fn);
|
|
224
267
|
const onUnmount = (fn) => __unmounts__.push(fn);
|
|
225
|
-
${before};
|
|
226
|
-
const __el__ =
|
|
268
|
+
${checkPrelude}${before};
|
|
269
|
+
const __el__ = ${elementExpr(html, name)};
|
|
227
270
|
for (const fn of __mounts__) fn(__el__);
|
|
228
271
|
__lifecycle__(__el__, __connects__, __unmounts__);
|
|
229
272
|
return __el__;
|
|
@@ -232,11 +275,13 @@ function buildComponent(name, args, body, modifier = null, tag = null) {
|
|
|
232
275
|
`;
|
|
233
276
|
}
|
|
234
277
|
else {
|
|
235
|
-
const fnBody = `const __mounts__=[];const __connects__=[];const __unmounts__=[];const onMount=(fn)=>__mounts__.push(fn);const onConnect=(fn)=>__connects__.push(fn);const onUnmount=(fn)=>__unmounts__.push(fn);${before}; const __el__ =
|
|
278
|
+
const fnBody = `const __mounts__=[];const __connects__=[];const __unmounts__=[];const onMount=(fn)=>__mounts__.push(fn);const onConnect=(fn)=>__connects__.push(fn);const onUnmount=(fn)=>__unmounts__.push(fn);${before}; const __el__ = ${elementExpr(html, name)}; for (const fn of __mounts__) fn(__el__); __lifecycle__(__el__, __connects__, __unmounts__); return __el__;`;
|
|
279
|
+
|
|
280
|
+
const guard = checks ? `const ${binding} = __props__;\n${checks};\n` : "";
|
|
236
281
|
|
|
237
282
|
return `
|
|
238
283
|
const ${name} = (__props__ = {}) => {
|
|
239
|
-
return new Function(${JSON.stringify(binding)}, ${JSON.stringify(fnBody)})(__props__);
|
|
284
|
+
${guard}return new Function(${JSON.stringify(binding)}, ${JSON.stringify(fnBody)})(__props__);
|
|
240
285
|
};
|
|
241
286
|
${name}.__component__ = true
|
|
242
287
|
`;
|