@ktjs/vite-plugin-ktjsx 0.4.4 → 0.4.6
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 +59 -20
- package/dist/index.mjs +13 -41
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,32 +1,71 @@
|
|
|
1
|
-
#
|
|
1
|
+
# KT.js
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/vite-plugin-ktjsx)
|
|
6
|
-
[](https://www.npmjs.com/package/vite-plugin-ktjsx)
|
|
3
|
+
[](https://www.npmjs.com/package/kt.js)
|
|
4
|
+
[](https://www.npmjs.com/package/kt.js)
|
|
7
5
|
[](https://opensource.org/licenses/MIT)
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://baendlorel.github.io/kt.js/">
|
|
9
|
+
<img src="https://raw.githubusercontent.com/baendlorel/kt.js/refs/heads/main/assets/ktjs-0.0.1.svg" width="240px" alt="KT.js logo" />
|
|
10
|
+
</a>
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<p align="center"><strong>Visit KT.js: <a href="https://baendlorel.github.io/kt.js/">https://baendlorel.github.io/kt.js/</a></strong></p>
|
|
14
|
+
|
|
15
|
+
> kt.js is still under development, so there might be some breaking changes. Note the Update Log below
|
|
16
|
+
|
|
17
|
+
## Recent Updates
|
|
18
|
+
|
|
19
|
+
1. `ref.value` remains the standard read API, and it can also replace the whole outer value with `ref.value = nextValue`.
|
|
20
|
+
2. `ref.draft` is the deep-mutation entry for nested objects, arrays, `Map` / `Set`, and custom mutable objects.
|
|
21
|
+
3. `ref.draft` itself is not assignable; mutate nested fields or call mutating methods on the returned object instead.
|
|
22
|
+
4. `addOnChange((newValue, oldValue) => ...)` keeps `oldValue` as the previous reference, not a deep snapshot.
|
|
23
|
+
|
|
24
|
+
## Community
|
|
25
|
+
|
|
26
|
+
- QQ Group: `1070434849`
|
|
27
|
+
- Telegram: https://t.me/kt_js
|
|
10
28
|
|
|
11
|
-
|
|
29
|
+
## Introduction
|
|
12
30
|
|
|
13
|
-
|
|
14
|
-
- compile adjacent `k-if` + `k-else` siblings into `KTConditional(...)`
|
|
15
|
-
- warn and leave `k-else-if` unchanged (currently unsupported)
|
|
31
|
+
kt.js is a simple framework with a tiny runtime that renders real DOM directly (no virtual DOM), uses explicit reactivity variables and gives you manual control over refs, bindings, and redraw timing.
|
|
16
32
|
|
|
17
|
-
|
|
33
|
+
KT.js focuses on one principle: keep direct control of the DOM and avoid unnecessary repainting.
|
|
34
|
+
|
|
35
|
+
## Reactive Contract
|
|
18
36
|
|
|
19
37
|
```ts
|
|
20
|
-
|
|
21
|
-
|
|
38
|
+
const user = ref({ profile: { name: 'John' }, tags: ['new'] });
|
|
39
|
+
|
|
40
|
+
console.log(user.value.profile.name); // read
|
|
22
41
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
42
|
+
user.value = {
|
|
43
|
+
...user.value,
|
|
44
|
+
profile: { ...user.value.profile, name: 'Jane' },
|
|
45
|
+
tags: [...user.value.tags],
|
|
46
|
+
}; // replace the whole outer value
|
|
47
|
+
|
|
48
|
+
user.draft.profile.name = 'Jane'; // deep write
|
|
49
|
+
user.draft.tags.push('active'); // array / map / set / custom-object style mutation
|
|
26
50
|
```
|
|
27
51
|
|
|
28
|
-
|
|
52
|
+
Rules:
|
|
53
|
+
|
|
54
|
+
- Read with `.value`.
|
|
55
|
+
- Replace the whole outer value with `.value = nextValue`.
|
|
56
|
+
- Use `.draft` for deep mutations on nested objects, arrays, `Map` / `Set`, or other mutable instances.
|
|
57
|
+
- Do not assign to `.draft` itself; mutate inside it.
|
|
58
|
+
- `computed` stays read-only and is consumed through `.value`.
|
|
59
|
+
- `oldValue` in change listeners is the previous reference only, not a deep-cloned snapshot.
|
|
60
|
+
- Correctness is expected to come from the transformer and TypeScript checks; runtime hot paths stay minimal on purpose.
|
|
29
61
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
62
|
+
This is an explicit contract, closer to a Rust-style model than permissive runtime magic: unclear code should fail early.
|
|
63
|
+
|
|
64
|
+
## Quick Start
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pnpm create kt.js my-app
|
|
68
|
+
cd my-app
|
|
69
|
+
pnpm install
|
|
70
|
+
pnpm dev
|
|
71
|
+
```
|
package/dist/index.mjs
CHANGED
|
@@ -1,53 +1,25 @@
|
|
|
1
|
-
import { transformWithKTjsx } from
|
|
1
|
+
import { transformWithKTjsx } from "@ktjs/transformer";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_INCLUDE_RE = /\.[cm]?[jt]sx$/, NODE_MODULES_RE = /\/node_modules\//, QUERY_RE = /\?.*$/, matchFilter = (filter, id) => !!filter && ("function" == typeof filter ? filter(id) : (filter.lastIndex = 0,
|
|
4
|
+
filter.test(id)));
|
|
2
5
|
|
|
3
|
-
const DEFAULT_INCLUDE_RE = /\.[cm]?[jt]sx$/;
|
|
4
|
-
const NODE_MODULES_RE = /\/node_modules\//;
|
|
5
|
-
const QUERY_RE = /\?.*$/;
|
|
6
|
-
const stripQuery = (id) => id.replace(QUERY_RE, '');
|
|
7
|
-
const matchFilter = (filter, id) => {
|
|
8
|
-
if (!filter) {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
if (typeof filter === 'function') {
|
|
12
|
-
return filter(id);
|
|
13
|
-
}
|
|
14
|
-
filter.lastIndex = 0;
|
|
15
|
-
return filter.test(id);
|
|
16
|
-
};
|
|
17
|
-
const shouldTransform = (id, include, exclude) => {
|
|
18
|
-
if (id.startsWith('\0') || NODE_MODULES_RE.test(id)) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
if (exclude && matchFilter(exclude, id)) {
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
if (include) {
|
|
25
|
-
return matchFilter(include, id);
|
|
26
|
-
}
|
|
27
|
-
return DEFAULT_INCLUDE_RE.test(id);
|
|
28
|
-
};
|
|
29
6
|
function viteKTjsx(options = {}) {
|
|
30
7
|
return {
|
|
31
|
-
name:
|
|
32
|
-
enforce:
|
|
8
|
+
name: "ktjs:vite-plugin-ktjsx",
|
|
9
|
+
enforce: "pre",
|
|
33
10
|
async transform(code, id) {
|
|
34
|
-
const cleanId =
|
|
35
|
-
if (!
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
11
|
+
const cleanId = (id => id.replace(QUERY_RE, ""))(id);
|
|
12
|
+
if (!((id, include, exclude) => !id.startsWith("\0") && !NODE_MODULES_RE.test(id) && (!exclude || !matchFilter(exclude, id)) && (include ? matchFilter(include, id) : DEFAULT_INCLUDE_RE.test(id)))(cleanId, options.include, options.exclude)) return null;
|
|
38
13
|
const result = await transformWithKTjsx(code, {
|
|
39
14
|
filename: cleanId,
|
|
40
15
|
sourceFileName: cleanId,
|
|
41
|
-
...options.babelConfig
|
|
16
|
+
...options.babelConfig
|
|
42
17
|
});
|
|
43
|
-
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
return {
|
|
18
|
+
return result?.code ? {
|
|
47
19
|
code: result.code,
|
|
48
|
-
map: result.map ?? null
|
|
49
|
-
};
|
|
50
|
-
}
|
|
20
|
+
map: result.map ?? null
|
|
21
|
+
} : null;
|
|
22
|
+
}
|
|
51
23
|
};
|
|
52
24
|
}
|
|
53
25
|
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../../src/index.ts"],"sourcesContent":["import type { Plugin } from 'vite';\nimport { transformWithKTjsx, type KTjsxTransformOptions } from '@ktjs/transformer';\n\ntype Filter = RegExp | ((id: string) => boolean);\n\nexport interface ViteKTjsxOptions {\n include?: Filter;\n exclude?: Filter;\n babelConfig?: Record<string, unknown>;\n}\n\nconst DEFAULT_INCLUDE_RE = /\\.[cm]?[jt]sx$/;\nconst NODE_MODULES_RE = /\\/node_modules\\//;\nconst QUERY_RE = /\\?.*$/;\ntype InternalTransformOptions = Omit<KTjsxTransformOptions, 'filename' | 'sourceFileName'>;\n\nconst stripQuery = (id: string) => id.replace(QUERY_RE, '');\n\nconst matchFilter = (filter: Filter | undefined, id: string): boolean => {\n if (!filter) {\n return false;\n }\n\n if (typeof filter === 'function') {\n return filter(id);\n }\n\n filter.lastIndex = 0;\n return filter.test(id);\n};\n\nconst shouldTransform = (id: string, include?: Filter, exclude?: Filter): boolean => {\n if (id.startsWith('\\0') || NODE_MODULES_RE.test(id)) {\n return false;\n }\n\n if (exclude && matchFilter(exclude, id)) {\n return false;\n }\n\n if (include) {\n return matchFilter(include, id);\n }\n\n return DEFAULT_INCLUDE_RE.test(id);\n};\n\nexport function viteKTjsx(options: ViteKTjsxOptions = {}): Plugin {\n return {\n name: 'ktjs:vite-plugin-ktjsx',\n enforce: 'pre',\n async transform(code, id) {\n const cleanId = stripQuery(id);\n if (!shouldTransform(cleanId, options.include, options.exclude)) {\n return null;\n }\n\n const result = await transformWithKTjsx(code, {\n filename: cleanId,\n sourceFileName: cleanId,\n ...(options.babelConfig as InternalTransformOptions),\n });\n\n if (!result?.code) {\n return null;\n }\n\n return {\n code: result.code,\n map: result.map ?? null,\n };\n },\n };\n}\n\nexport default viteKTjsx;\n"],"names":[
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../src/index.ts"],"sourcesContent":["import type { Plugin } from 'vite';\nimport { transformWithKTjsx, type KTjsxTransformOptions } from '@ktjs/transformer';\n\ntype Filter = RegExp | ((id: string) => boolean);\n\nexport interface ViteKTjsxOptions {\n include?: Filter;\n exclude?: Filter;\n babelConfig?: Record<string, unknown>;\n}\n\nconst DEFAULT_INCLUDE_RE = /\\.[cm]?[jt]sx$/;\nconst NODE_MODULES_RE = /\\/node_modules\\//;\nconst QUERY_RE = /\\?.*$/;\ntype InternalTransformOptions = Omit<KTjsxTransformOptions, 'filename' | 'sourceFileName'>;\n\nconst stripQuery = (id: string) => id.replace(QUERY_RE, '');\n\nconst matchFilter = (filter: Filter | undefined, id: string): boolean => {\n if (!filter) {\n return false;\n }\n\n if (typeof filter === 'function') {\n return filter(id);\n }\n\n filter.lastIndex = 0;\n return filter.test(id);\n};\n\nconst shouldTransform = (id: string, include?: Filter, exclude?: Filter): boolean => {\n if (id.startsWith('\\0') || NODE_MODULES_RE.test(id)) {\n return false;\n }\n\n if (exclude && matchFilter(exclude, id)) {\n return false;\n }\n\n if (include) {\n return matchFilter(include, id);\n }\n\n return DEFAULT_INCLUDE_RE.test(id);\n};\n\nexport function viteKTjsx(options: ViteKTjsxOptions = {}): Plugin {\n return {\n name: 'ktjs:vite-plugin-ktjsx',\n enforce: 'pre',\n async transform(code, id) {\n const cleanId = stripQuery(id);\n if (!shouldTransform(cleanId, options.include, options.exclude)) {\n return null;\n }\n\n const result = await transformWithKTjsx(code, {\n filename: cleanId,\n sourceFileName: cleanId,\n ...(options.babelConfig as InternalTransformOptions),\n });\n\n if (!result?.code) {\n return null;\n }\n\n return {\n code: result.code,\n map: result.map ?? null,\n };\n },\n };\n}\n\nexport default viteKTjsx;\n"],"names":["DEFAULT_INCLUDE_RE","NODE_MODULES_RE","QUERY_RE","matchFilter","filter","id","lastIndex","test","viteKTjsx","options","name","enforce","transform","code","cleanId","replace","stripQuery","include","exclude","startsWith","shouldTransform","result","transformWithKTjsx","filename","sourceFileName","babelConfig","map"],"mappings":";;AAWA,MAAMA,qBAAqB,kBACrBC,kBAAkB,oBAClBC,WAAW,SAKXC,cAAc,CAACC,QAA4BC,SAC1CD,WAIiB,qBAAXA,SACFA,OAAOC,OAGhBD,OAAOE,YAAY;AACZF,OAAOG,KAAKF;;AAmBf,SAAUG,UAAUC,UAA4B;IACpD,OAAO;QACLC,MAAM;QACNC,SAAS;QACT,eAAMC,CAAUC,MAAMR;YACpB,MAAMS,UApCO,CAACT,MAAeA,GAAGU,QAAQb,UAAU,IAoClCc,CAAWX;YAC3B,KAtBkB,EAACA,IAAYY,SAAkBC,aACjDb,GAAGc,WAAW,UAASlB,gBAAgBM,KAAKF,SAI5Ca,YAAWf,YAAYe,SAASb,SAIhCY,UACKd,YAAYc,SAASZ,MAGvBL,mBAAmBO,KAAKF,KAStBe,CAAgBN,SAASL,QAAQQ,SAASR,QAAQS,UACrD,OAAO;YAGT,MAAMG,eAAeC,mBAAmBT,MAAM;gBAC5CU,UAAUT;gBACVU,gBAAgBV;mBACZL,QAAQgB;;YAGd,OAAKJ,QAAQR,OAIN;gBACLA,MAAMQ,OAAOR;gBACba,KAAKL,OAAOK,OAAO;gBALZ;AAOX;;AAEJ;;"}
|