@slim-lang/core 1.2.9 → 1.2.11

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 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slim-lang/core",
3
- "version": "1.2.9",
3
+ "version": "1.2.11",
4
4
  "description": "Slim extends JavaScript with runtime types, structs, operators, and components, compiling to plain Javascript",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/run-dev-slim.js CHANGED
@@ -12,7 +12,8 @@ const config = JSON.parse(
12
12
  fs.readFileSync("slimconfig.json", "utf8")
13
13
  );
14
14
 
15
- const distDir = typeof config.dist === "string" && config.dist.trim() ? config.dist : "slim-dist";
15
+ const distName = (config.names && config.names.dist) ?? config.dist;
16
+ const distDir = typeof distName === "string" && distName.trim() ? distName : "slim-dist";
16
17
  const entry = `${distDir}/${config.main}.js`;
17
18
  const hot = process.argv.includes("--hot");
18
19
 
package/run-slim.js CHANGED
@@ -11,7 +11,8 @@ const root = process.cwd();
11
11
 
12
12
  const config = JSON.parse(readFileSync(path.join(root, 'slimconfig.json'), 'utf8'));
13
13
  const mainFile = config.main;
14
- const distDir = typeof config.dist === 'string' && config.dist.trim() ? config.dist : 'slim-dist';
14
+ const distName = (config.names && config.names.dist) ?? config.dist;
15
+ const distDir = typeof distName === 'string' && distName.trim() ? distName : 'slim-dist';
15
16
 
16
17
  const dist = path.join(root, distDir, `${mainFile}.js`);
17
18
  const args = ['--enable-source-maps', '--no-warnings', dist];
package/src/bin/cli.js CHANGED
@@ -351,7 +351,7 @@ program
351
351
  packageCheck()
352
352
 
353
353
  if (!fs.existsSync(slimConfigPath)) {
354
- fs.writeFileSync(slimConfigPath, JSON.stringify({ main: "index", usePackages: true }, null, 4), "utf8")
354
+ fs.writeFileSync(slimConfigPath, JSON.stringify({ main: "index", uses: { style: "import", enabled: true } }, null, 4), "utf8")
355
355
  log("Created slimconfig.json")
356
356
  } else {
357
357
  log("slimconfig.json already exists, leaving it untouched")
@@ -1,7 +1,11 @@
1
- {
2
- "main": "path/to/your/files",
3
- "usePackages": true,
4
- "uses": "import",
5
- "packages": "packages",
6
- "dist": "slim-dist"
7
- }
1
+ {
2
+ "main": "path/to/your/files",
3
+ "uses": {
4
+ "style": "import",
5
+ "enabled": true
6
+ },
7
+ "names": {
8
+ "packages": "packages",
9
+ "dist": "slim-dist"
10
+ }
11
+ }
package/src/compile.js CHANGED
@@ -90,7 +90,7 @@ function compileFile(slimFile, isEntry = false, mainEntry = null) {
90
90
 
91
91
  if (!usePackages && uses.length > 0) {
92
92
  const err = new UseError(
93
- `The "use" feature is disabled. Set "usePackages": true in slimconfig.json to enable imports (in ${abs}, found: use ${uses[0]})`
93
+ `The "use" feature is disabled. Set "uses": { "enabled": true } in slimconfig.json to enable imports (in ${abs}, found: use ${uses[0]})`
94
94
  )
95
95
  console.error(err)
96
96
  process.exit(1)
@@ -209,11 +209,14 @@ async function main() {
209
209
  process.exit(1)
210
210
  }
211
211
 
212
- usePackages = data.usePackages !== false
212
+ const uses = data.uses && typeof data.uses === "object" ? data.uses : null
213
+ usePackages = uses ? uses.enabled !== false : data.usePackages !== false
214
+ useStyle = uses
215
+ ? (typeof uses.style === "string" ? uses.style : "import")
216
+ : (typeof data.uses === "string" ? data.uses : "import")
213
217
  jsdoc = data.jsdoc === true
214
218
  declarations = data.declarations === true
215
219
  check = data.check !== false && process.env.SLIM_NO_CHECK !== "1"
216
- useStyle = typeof data.uses === "string" ? data.uses : "import"
217
220
 
218
221
  if (!("main" in data)) return
219
222
 
@@ -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
  })
@@ -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__ = htmlToVdom(__html__\`${html}\`).toElement();
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__ = htmlToVdom(__html__\`${html}\`).toElement();
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__ = htmlToVdom(__html__\`${html}\`).toElement(); for (const fn of __mounts__) fn(__el__); __lifecycle__(__el__, __connects__, __unmounts__); return __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
 
@@ -15,12 +15,18 @@ function readConfig() {
15
15
  }
16
16
  }
17
17
 
18
+ function configName(key, fallback) {
19
+ const config = readConfig()
20
+ const names = config.names && typeof config.names === "object" ? config.names : {}
21
+ const value = names[key] ?? config[key]
22
+ return typeof value === "string" && value.trim() ? value : fallback
23
+ }
24
+
18
25
  let projectPackagesCache = null
19
26
  export function projectPackagesDir() {
20
27
  if (projectPackagesCache) return projectPackagesCache
21
28
 
22
- const dir = readConfig().packages
23
- projectPackagesCache = path.resolve(typeof dir === "string" && dir.trim() ? dir : "packages")
29
+ projectPackagesCache = path.resolve(configName("packages", "packages"))
24
30
  return projectPackagesCache
25
31
  }
26
32
 
@@ -28,8 +34,7 @@ let distDirCache = null
28
34
  export function distDirName() {
29
35
  if (distDirCache) return distDirCache
30
36
 
31
- const dir = readConfig().dist
32
- distDirCache = typeof dir === "string" && dir.trim() ? dir : "slim-dist"
37
+ distDirCache = configName("dist", "slim-dist")
33
38
  return distDirCache
34
39
  }
35
40