@slim-lang/core 1.2.9 → 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 +31 -3
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,6 +1,34 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
2
|
+
import path from "node:path"
|
|
1
3
|
import { tokenize } from "../../lexer.js"
|
|
2
4
|
import { parseTypedArgs, buildTypedArgsResult } from "../parserHandler.js"
|
|
3
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
|
+
}
|
|
31
|
+
|
|
4
32
|
const OPENERS = new Set(["(", "[", "{", "${"])
|
|
5
33
|
const CLOSERS = new Set([")", "]", "}"])
|
|
6
34
|
|
|
@@ -187,7 +215,7 @@ function buildElementComponent(name, binding, before, html, explicitTag, checks
|
|
|
187
215
|
const onConnect = (fn) => __connects__.push(fn);
|
|
188
216
|
const onUnmount = (fn) => __unmounts__.push(fn);
|
|
189
217
|
${checkPrelude}${before};
|
|
190
|
-
const __el__ =
|
|
218
|
+
const __el__ = ${elementExpr(html, name)};
|
|
191
219
|
|
|
192
220
|
if (!__host__) {
|
|
193
221
|
for (const fn of __mounts__) fn(__el__);
|
|
@@ -238,7 +266,7 @@ function buildComponent(name, args, body, modifier = null, tag = null) {
|
|
|
238
266
|
const onConnect = (fn) => __connects__.push(fn);
|
|
239
267
|
const onUnmount = (fn) => __unmounts__.push(fn);
|
|
240
268
|
${checkPrelude}${before};
|
|
241
|
-
const __el__ =
|
|
269
|
+
const __el__ = ${elementExpr(html, name)};
|
|
242
270
|
for (const fn of __mounts__) fn(__el__);
|
|
243
271
|
__lifecycle__(__el__, __connects__, __unmounts__);
|
|
244
272
|
return __el__;
|
|
@@ -247,7 +275,7 @@ function buildComponent(name, args, body, modifier = null, tag = null) {
|
|
|
247
275
|
`;
|
|
248
276
|
}
|
|
249
277
|
else {
|
|
250
|
-
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__;`;
|
|
251
279
|
|
|
252
280
|
const guard = checks ? `const ${binding} = __props__;\n${checks};\n` : "";
|
|
253
281
|
|