kreftjs 0.0.0-stage → 0.0.2-beta
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 +103 -2
- package/bin/add.js +1074 -0
- package/bin/artisan-more.js +994 -0
- package/bin/artisan.js +1877 -0
- package/bin/backend/app.js +161 -0
- package/bin/backend/cache.js +196 -0
- package/bin/backend/collection.js +117 -0
- package/bin/backend/command.js +45 -0
- package/bin/backend/config.js +234 -0
- package/bin/backend/error-page.js +773 -0
- package/bin/backend/events.js +49 -0
- package/bin/backend/factory.js +223 -0
- package/bin/backend/fake.js +180 -0
- package/bin/backend/form.js +102 -0
- package/bin/backend/gate.js +79 -0
- package/bin/backend/gc.js +69 -0
- package/bin/backend/html.js +322 -0
- package/bin/backend/http.js +652 -0
- package/bin/backend/index.js +452 -0
- package/bin/backend/mail.js +77 -0
- package/bin/backend/mcp.js +433 -0
- package/bin/backend/migrator.js +370 -0
- package/bin/backend/model.js +781 -0
- package/bin/backend/namespace.js +213 -0
- package/bin/backend/notifications.js +68 -0
- package/bin/backend/qr.js +164 -0
- package/bin/backend/query.js +387 -0
- package/bin/backend/queue.js +412 -0
- package/bin/backend/redis.js +479 -0
- package/bin/backend/schedule.js +112 -0
- package/bin/backend/schema.js +321 -0
- package/bin/backend/seo-pages.js +100 -0
- package/bin/backend/storage.js +153 -0
- package/bin/backend/support.js +567 -0
- package/bin/backend/typescript.js +166 -0
- package/bin/build.js +540 -0
- package/bin/db-api.js +249 -0
- package/bin/db.js +275 -0
- package/bin/deps.js +138 -0
- package/bin/electron.js +509 -0
- package/bin/env.js +97 -0
- package/bin/guard.js +80 -0
- package/bin/kreftjs-config.js +40 -0
- package/bin/kreftjs.js +1373 -0
- package/bin/lang.js +241 -0
- package/bin/layout.js +74 -0
- package/bin/no-auth.js +162 -0
- package/bin/prerender.js +258 -0
- package/bin/safe-json.js +24 -0
- package/bin/server-routes.js +154 -0
- package/bin/ssr.js +133 -0
- package/bin/terminal.js +195 -0
- package/bin/to-ts.js +36 -0
- package/bin/update.js +237 -0
- package/bin/urls-file.js +27 -0
- package/bin/vite.js +228 -0
- package/docs/Docs.kreftjs +557 -0
- package/docs/README.md +2302 -0
- package/docs/markdown.js +186 -0
- package/editors/vscode/README.md +13 -0
- package/editors/vscode/extension.js +183 -0
- package/editors/vscode/language-configuration.json +17 -0
- package/editors/vscode/package.json +57 -0
- package/editors/vscode/snippets/kreftjs.json +21 -0
- package/editors/vscode/syntaxes/kreftjs.tmLanguage.json +207 -0
- package/kreftjs.d.ts +579 -0
- package/kreftjs.js +5108 -0
- package/package.json +97 -4
- package/server.d.ts +1028 -0
- package/server.js +8 -0
- package/templates/app/README.md +127 -0
- package/templates/app/_env.example +64 -0
- package/templates/app/_gitignore +5 -0
- package/templates/app/_vscode/settings.json +9 -0
- package/templates/app/backend/app/Auth/Session.js +177 -0
- package/templates/app/backend/app/Auth/Throttle.js +29 -0
- package/templates/app/backend/app/Console/Commands/.gitkeep +0 -0
- package/templates/app/backend/app/Events/.gitkeep +0 -0
- package/templates/app/backend/app/Http/Controllers/Admin/DashboardController.js +34 -0
- package/templates/app/backend/app/Http/Controllers/Admin/UserController.js +53 -0
- package/templates/app/backend/app/Http/Controllers/Auth/ApiTokenController.js +47 -0
- package/templates/app/backend/app/Http/Controllers/Auth/AuthenticatedSessionController.js +41 -0
- package/templates/app/backend/app/Http/Controllers/Auth/ConfirmablePasswordController.js +14 -0
- package/templates/app/backend/app/Http/Controllers/Auth/EmailVerificationController.js +23 -0
- package/templates/app/backend/app/Http/Controllers/Auth/NewPasswordController.js +29 -0
- package/templates/app/backend/app/Http/Controllers/Auth/PasswordResetLinkController.js +20 -0
- package/templates/app/backend/app/Http/Controllers/Auth/RegisteredUserController.js +19 -0
- package/templates/app/backend/app/Http/Controllers/Auth/SessionController.js +36 -0
- package/templates/app/backend/app/Http/Controllers/Auth/TwoFactorChallengeController.js +41 -0
- package/templates/app/backend/app/Http/Controllers/Auth/TwoFactorController.js +63 -0
- package/templates/app/backend/app/Http/Controllers/ProfileController.js +66 -0
- package/templates/app/backend/app/Http/Kernel.js +8 -0
- package/templates/app/backend/app/Http/Middleware/Authenticate.js +84 -0
- package/templates/app/backend/app/Http/Middleware/CheckAbilities.js +17 -0
- package/templates/app/backend/app/Http/Middleware/CheckForAnyAbility.js +12 -0
- package/templates/app/backend/app/Http/Middleware/EnsureEmailIsVerified.js +11 -0
- package/templates/app/backend/app/Http/Middleware/EnsureHasRole.js +11 -0
- package/templates/app/backend/app/Http/Middleware/EnsureIsAdmin.js +11 -0
- package/templates/app/backend/app/Http/Middleware/RequirePassword.js +15 -0
- package/templates/app/backend/app/Http/Requests/Auth/LoginRequest.js +45 -0
- package/templates/app/backend/app/Http/Resources/.gitkeep +1 -0
- package/templates/app/backend/app/Jobs/.gitkeep +0 -0
- package/templates/app/backend/app/Listeners/.gitkeep +0 -0
- package/templates/app/backend/app/Mail/.gitkeep +0 -0
- package/templates/app/backend/app/Models/PersonalAccessToken.js +21 -0
- package/templates/app/backend/app/Models/User.js +64 -0
- package/templates/app/backend/app/Notifications/.gitkeep +0 -0
- package/templates/app/backend/app/Observers/.gitkeep +0 -0
- package/templates/app/backend/app/Policies/.gitkeep +0 -0
- package/templates/app/backend/app/Providers/AppServiceProvider.js +12 -0
- package/templates/app/backend/app/Rules/.gitkeep +1 -0
- package/templates/app/backend/config/app.js +16 -0
- package/templates/app/backend/config/auth.js +34 -0
- package/templates/app/backend/config/database.js +38 -0
- package/templates/app/backend/config/redis.js +28 -0
- package/templates/app/backend/config/session.js +36 -0
- package/templates/app/backend/database/migrations/0001_01_01_000000_create_users_table.js +49 -0
- package/templates/app/backend/database/migrations/0001_01_01_000001_create_cache_table.js +13 -0
- package/templates/app/backend/database/migrations/0001_01_01_000002_create_jobs_table.js +26 -0
- package/templates/app/backend/database/migrations/0001_01_01_000003_create_notifications_table.js +18 -0
- package/templates/app/backend/database/seeders/DatabaseSeeder.js +15 -0
- package/templates/app/backend/lang/ar/messages.json +10 -0
- package/templates/app/backend/lang/ar/validation.json +21 -0
- package/templates/app/backend/lang/en/messages.json +10 -0
- package/templates/app/backend/lang/en/validation.json +19 -0
- package/templates/app/backend/routes/api.js +25 -0
- package/templates/app/backend/routes/auth.js +52 -0
- package/templates/app/backend/routes/console.js +15 -0
- package/templates/app/frontend/App.kreftjs +4 -0
- package/templates/app/frontend/components/Field.kreftjs +19 -0
- package/templates/app/frontend/components/StatCard.kreftjs +47 -0
- package/templates/app/frontend/components/profile/ApiTokens.kreftjs +70 -0
- package/templates/app/frontend/components/profile/Sessions.kreftjs +42 -0
- package/templates/app/frontend/components/profile/TwoFactor.kreftjs +90 -0
- package/templates/app/frontend/errors/ErrorMonitor.kreftjs +264 -0
- package/templates/app/frontend/index.html +79 -0
- package/templates/app/frontend/kreftjs.d.ts +579 -0
- package/templates/app/frontend/kreftjs.js +5108 -0
- package/templates/app/frontend/layouts/app.kreftjs +718 -0
- package/templates/app/frontend/layouts/docs.kreftjs +37 -0
- package/templates/app/frontend/layouts/guest.kreftjs +18 -0
- package/templates/app/frontend/pages/Dashboard.kreftjs +273 -0
- package/templates/app/frontend/pages/ErrorMonitor.kreftjs +96 -0
- package/templates/app/frontend/pages/Home.kreftjs +617 -0
- package/templates/app/frontend/pages/Profile.kreftjs +104 -0
- package/templates/app/frontend/pages/Users.kreftjs +240 -0
- package/templates/app/frontend/pages/auth/ConfirmPassword.kreftjs +33 -0
- package/templates/app/frontend/pages/auth/ForgotPassword.kreftjs +36 -0
- package/templates/app/frontend/pages/auth/Login.kreftjs +89 -0
- package/templates/app/frontend/pages/auth/Register.kreftjs +48 -0
- package/templates/app/frontend/pages/auth/ResetPassword.kreftjs +45 -0
- package/templates/app/frontend/pages/auth/VerifyEmail.kreftjs +59 -0
- package/templates/app/frontend/routes/web.kreftjs +34 -0
- package/templates/app/frontend/tests/components/Counter.kreftjs +15 -0
- package/templates/app/frontend/tests/pages/About.kreftjs +12 -0
- package/templates/app/frontend/tests/pages/Test.kreftjs +31 -0
- package/templates/app/frontend/ui.css +517 -0
- package/templates/app/lang/ar/auth.json +6 -0
- package/templates/app/lang/ar/messages.json +6 -0
- package/templates/app/lang/ar/pagination.json +5 -0
- package/templates/app/lang/ar/passwords.json +8 -0
- package/templates/app/lang/ar/validation.json +22 -0
- package/templates/app/lang/ar.json +10 -0
- package/templates/app/lang/en/auth.json +6 -0
- package/templates/app/lang/en/messages.json +6 -0
- package/templates/app/lang/en/pagination.json +5 -0
- package/templates/app/lang/en/passwords.json +8 -0
- package/templates/app/lang/en/validation.json +22 -0
- package/templates/app/lang/en.json +10 -0
- package/templates/errors/401.kreftjs +11 -0
- package/templates/errors/402.kreftjs +11 -0
- package/templates/errors/403.kreftjs +11 -0
- package/templates/errors/404.kreftjs +11 -0
- package/templates/errors/419.kreftjs +11 -0
- package/templates/errors/429.kreftjs +11 -0
- package/templates/errors/500.kreftjs +11 -0
- package/templates/errors/503.kreftjs +11 -0
- package/templates/errors/ErrorMonitor.kreftjs +502 -0
- package/templates/errors/error.kreftjs +11 -0
- package/templates/errors/minimal.kreftjs +25 -0
- package/tools/dev-client.js +125 -0
- package/tools/devtools.js +172 -0
- package/tools/vite-plugin-kreftjs.d.ts +52 -0
- package/tools/vite-plugin-kreftjs.js +466 -0
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Kreft Vite Plugin
|
|
3
|
+
*
|
|
4
|
+
* Full-featured Vite.js support for Kreft:
|
|
5
|
+
* • Compiles .kreftjs components into native ES modules
|
|
6
|
+
* • Scoped CSS (<style scoped>) extracted into Vite virtual CSS modules with instant HMR
|
|
7
|
+
* • Route files (routes/*.kreftjs) automatically transformed and registered
|
|
8
|
+
* • Component HMR preserves live state and reactivity
|
|
9
|
+
* • Precompiles view expressions ahead of time (zero eval / CSP-safe)
|
|
10
|
+
* • Automatically resolves and aliases 'kreftjs' (and 'kreft' and 'som', the earlier names)
|
|
11
|
+
*
|
|
12
|
+
* Usage (vite.config.js):
|
|
13
|
+
* import { defineConfig } from 'vite';
|
|
14
|
+
* import kreftjs from 'kreftjs/vite';
|
|
15
|
+
*
|
|
16
|
+
* export default defineConfig({
|
|
17
|
+
* plugins: [kreftjs()],
|
|
18
|
+
* });
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const fs = require('fs');
|
|
22
|
+
const path = require('path');
|
|
23
|
+
const Kreftjs = require('../kreftjs.js');
|
|
24
|
+
|
|
25
|
+
const {
|
|
26
|
+
expandBlocks, prepareTemplate, splitKreft, splitInterp, exactBrace, stripBrace,
|
|
27
|
+
interpToExpr, quoteBraceAttrs, expandSelfClosing, scopeCss, hashString,
|
|
28
|
+
simplePathRE, fnExpRE, forAliasRE
|
|
29
|
+
} = Kreftjs.__internal;
|
|
30
|
+
|
|
31
|
+
/* ------------------------------------------------------------------ helpers */
|
|
32
|
+
function extractImportedNames(what) {
|
|
33
|
+
if (!what) return [];
|
|
34
|
+
const names = [];
|
|
35
|
+
const parts = what.split(',').map((p) => p.trim()).filter(Boolean);
|
|
36
|
+
for (const part of parts) {
|
|
37
|
+
if (part.startsWith('{') || part.endsWith('}')) {
|
|
38
|
+
const inside = part.replace(/^\{|\}$/g, '').trim();
|
|
39
|
+
inside.split(',').map((s) => s.trim()).filter(Boolean).forEach((s) => {
|
|
40
|
+
const asMatch = s.match(/\bas\s+([A-Za-z_$][\w$]*)$/);
|
|
41
|
+
if (asMatch) names.push(asMatch[1]);
|
|
42
|
+
else if (/^[A-Za-z_$][\w$]*$/.test(s)) names.push(s);
|
|
43
|
+
});
|
|
44
|
+
} else {
|
|
45
|
+
const starMatch = part.match(/^\*\s+as\s+([A-Za-z_$][\w$]*)$/);
|
|
46
|
+
if (starMatch) names.push(starMatch[1]);
|
|
47
|
+
else if (/^[A-Za-z_$][\w$]*$/.test(part)) names.push(part);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return names;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* ------------------------------------------------------------------ expressions → functions */
|
|
54
|
+
const EVAL = (expr) => ['$s', `with($s){return(${expr})}`];
|
|
55
|
+
const STMT = (expr) => ['$s,$event,$args', `with($s){${expr}}`];
|
|
56
|
+
const ASSIGN = (expr) => ['$s,$v', `with($s){${expr}=$v}`];
|
|
57
|
+
|
|
58
|
+
function addFn(fns, [params, code]) {
|
|
59
|
+
const key = params + '\u0000' + code;
|
|
60
|
+
if (fns.has(key)) return;
|
|
61
|
+
const src = `function (${params.split(',').join(', ')}) { ${code} }`;
|
|
62
|
+
try { new Function('return ' + src); } catch (_) { return; }
|
|
63
|
+
fns.set(key, src);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function addHandler(fns, expr) {
|
|
67
|
+
if (!expr || !expr.trim()) return;
|
|
68
|
+
if (simplePathRE.test(expr.trim()) || fnExpRE.test(expr)) addFn(fns, EVAL(expr));
|
|
69
|
+
else addFn(fns, STMT(expr));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function addEval(fns, expr) {
|
|
73
|
+
if (expr != null && expr !== '') addFn(fns, EVAL(expr));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const STRUCTURAL = new Set(['if', 'elif', 'else', 'each', 'key', 'ref', 'skip', 'cloak', 'slot', 'empty', 'once', 'animate', 'waiting', 'failed']);
|
|
77
|
+
|
|
78
|
+
function collectExpressions(template, fns) {
|
|
79
|
+
const html = prepareTemplate(template);
|
|
80
|
+
scanHtml(html, {
|
|
81
|
+
attr(name, value) {
|
|
82
|
+
if (name[0] === ':' || name === 'k-bind' || name.startsWith('k-bind:')) return addEval(fns, value);
|
|
83
|
+
if (name[0] === '@' || name.startsWith('k-on:')) return addHandler(fns, stripBrace(value));
|
|
84
|
+
if (name.startsWith('k-')) {
|
|
85
|
+
const v = stripBrace(value);
|
|
86
|
+
const dir = name.slice(2).split('.')[0].split(':')[0];
|
|
87
|
+
if (dir === 'if' || dir === 'elif' || dir === 'key') return addEval(fns, v);
|
|
88
|
+
if (dir === 'each') {
|
|
89
|
+
const m = v.match(forAliasRE);
|
|
90
|
+
if (m) addEval(fns, m[3].trim());
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (dir === 'model') { addEval(fns, v); addFn(fns, ASSIGN(v)); return; }
|
|
94
|
+
if (!STRUCTURAL.has(dir) && v) addEval(fns, v);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (value.includes('{')) {
|
|
98
|
+
const expr = exactBrace(value) ?? interpToExpr(value);
|
|
99
|
+
if (expr != null) addEval(fns, expr);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
text(t) {
|
|
103
|
+
if (!t.includes('{')) return;
|
|
104
|
+
const parts = splitInterp(t);
|
|
105
|
+
if (!parts.some((p) => p.expr != null)) return;
|
|
106
|
+
const code = `with($s){return ${parts.map((p) => (p.expr != null ? `$d(${p.expr})` : JSON.stringify(p.text))).join('+')}}`;
|
|
107
|
+
addFn(fns, ['$s,$d', code]);
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const ENTITIES = {
|
|
113
|
+
amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ', copy: '©', reg: '®', times: '×',
|
|
114
|
+
middot: '·', hellip: '…', mdash: '—', ndash: '–', larr: '←', rarr: '→', uarr: '↑', darr: '↓', laquo: '«', raquo: '»', bull: '•'
|
|
115
|
+
};
|
|
116
|
+
const decode = (s) => s.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (m, e) => {
|
|
117
|
+
if (e[0] === '#') {
|
|
118
|
+
const cp = e[1] === 'x' || e[1] === 'X' ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
|
|
119
|
+
return Number.isNaN(cp) || cp < 0 || cp > 0x10ffff ? m : String.fromCodePoint(cp);
|
|
120
|
+
}
|
|
121
|
+
return ENTITIES[e.toLowerCase()] ?? m;
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
function scanHtml(html, on) {
|
|
125
|
+
let i = 0, text = '', dropNewline = false;
|
|
126
|
+
const flush = () => {
|
|
127
|
+
if (text) {
|
|
128
|
+
let t = decode(text);
|
|
129
|
+
if (dropNewline && t[0] === '\n') t = t.slice(1);
|
|
130
|
+
on.text(t);
|
|
131
|
+
}
|
|
132
|
+
text = '';
|
|
133
|
+
dropNewline = false;
|
|
134
|
+
};
|
|
135
|
+
while (i < html.length) {
|
|
136
|
+
if (html.startsWith('<!--', i)) {
|
|
137
|
+
flush();
|
|
138
|
+
const e = html.indexOf('-->', i + 4);
|
|
139
|
+
i = e < 0 ? html.length : e + 3;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (html[i] === '<' && html[i + 1] === '/') {
|
|
143
|
+
flush();
|
|
144
|
+
const e = html.indexOf('>', i);
|
|
145
|
+
i = e < 0 ? html.length : e + 1;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (html[i] === '<' && /[A-Za-z]/.test(html[i + 1] || '')) {
|
|
149
|
+
flush();
|
|
150
|
+
let j = i + 1;
|
|
151
|
+
while (j < html.length && !/[\s/>]/.test(html[j])) j++;
|
|
152
|
+
while (j < html.length && html[j] !== '>') {
|
|
153
|
+
if (/[\s/]/.test(html[j])) { j++; continue; }
|
|
154
|
+
let k = j;
|
|
155
|
+
while (k < html.length && !/[\s"'>/=]/.test(html[k])) k++;
|
|
156
|
+
const name = html.slice(j, k).toLowerCase();
|
|
157
|
+
let value = '';
|
|
158
|
+
let q = k;
|
|
159
|
+
while (/\s/.test(html[q] || '')) q++;
|
|
160
|
+
if (html[q] === '=') {
|
|
161
|
+
q++;
|
|
162
|
+
while (/\s/.test(html[q] || '')) q++;
|
|
163
|
+
if (html[q] === '"' || html[q] === "'") {
|
|
164
|
+
const end = html.indexOf(html[q], q + 1);
|
|
165
|
+
value = html.slice(q + 1, end < 0 ? html.length : end);
|
|
166
|
+
k = end < 0 ? html.length : end + 1;
|
|
167
|
+
} else {
|
|
168
|
+
let e = q;
|
|
169
|
+
while (e < html.length && !/[\s>]/.test(html[e])) e++;
|
|
170
|
+
value = html.slice(q, e);
|
|
171
|
+
k = e;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
on.attr(name, value);
|
|
175
|
+
j = k;
|
|
176
|
+
}
|
|
177
|
+
i = j < html.length && html[j] === '>' ? j + 1 : j;
|
|
178
|
+
dropNewline = true;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
text += html[i];
|
|
182
|
+
i++;
|
|
183
|
+
}
|
|
184
|
+
flush();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/* ------------------------------------------------------------------ the plugin */
|
|
188
|
+
function kreftPlugin(options = {}) {
|
|
189
|
+
const include = options.include || /\.(?:kreftjs|kreft|som)$/; // .kreft and .som: files of an older project
|
|
190
|
+
const exclude = options.exclude || /node_modules/;
|
|
191
|
+
const precompile = options.precompile !== false;
|
|
192
|
+
const isComponentFile = (f) => /\.(?:kreftjs|kreft|som)$/.test(f);
|
|
193
|
+
const kreftRuntimePath = path.resolve(__dirname, '..', 'kreftjs.js');
|
|
194
|
+
|
|
195
|
+
const filter = (id) => {
|
|
196
|
+
if (include instanceof RegExp && !include.test(id)) return false;
|
|
197
|
+
if (typeof include === 'string' && !id.includes(include)) return false;
|
|
198
|
+
if (Array.isArray(include) && !include.some((p) => (p instanceof RegExp ? p.test(id) : id.includes(p)))) return false;
|
|
199
|
+
if (exclude instanceof RegExp && exclude.test(id)) return false;
|
|
200
|
+
if (typeof exclude === 'string' && id.includes(exclude)) return false;
|
|
201
|
+
if (Array.isArray(exclude) && exclude.some((p) => (p instanceof RegExp ? p.test(id) : id.includes(p)))) return false;
|
|
202
|
+
return true;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// Cache parsed styles for handleHotUpdate comparison
|
|
206
|
+
const fileCache = new Map();
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
name: 'kreft:vite',
|
|
210
|
+
enforce: 'pre',
|
|
211
|
+
|
|
212
|
+
config(userConfig) {
|
|
213
|
+
return {
|
|
214
|
+
resolve: {
|
|
215
|
+
alias: [
|
|
216
|
+
{ find: /^(?:kreftjs|kreft|som)$/, replacement: kreftRuntimePath }, // kreft and som: the earlier names
|
|
217
|
+
],
|
|
218
|
+
},
|
|
219
|
+
optimizeDeps: {
|
|
220
|
+
exclude: ['kreftjs', 'kreft', 'som'],
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
},
|
|
224
|
+
|
|
225
|
+
configureServer(server) {
|
|
226
|
+
// Serve runtime if requested directly as script
|
|
227
|
+
server.middlewares.use((req, res, next) => {
|
|
228
|
+
const url = req.url ? req.url.split('?')[0] : '';
|
|
229
|
+
if (url === '/kreftjs.js' || url === '/kreftjs.min.js') {
|
|
230
|
+
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
|
|
231
|
+
res.end(fs.readFileSync(kreftRuntimePath, 'utf8'));
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
next();
|
|
235
|
+
});
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
resolveId(id) {
|
|
239
|
+
if (id.includes('?kreft&type=style')) {
|
|
240
|
+
return id;
|
|
241
|
+
}
|
|
242
|
+
return null;
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
load(id) {
|
|
246
|
+
if (id.includes('?kreft&type=style')) {
|
|
247
|
+
const [filePath, query] = id.split('?');
|
|
248
|
+
if (!isComponentFile(filePath) || !filter(filePath)) return null;
|
|
249
|
+
const params = new URLSearchParams(query);
|
|
250
|
+
const index = parseInt(params.get('index') || '0', 10);
|
|
251
|
+
const isScoped = params.get('scoped') === 'true';
|
|
252
|
+
|
|
253
|
+
let content = '';
|
|
254
|
+
try {
|
|
255
|
+
content = fs.readFileSync(filePath, 'utf8');
|
|
256
|
+
} catch (_) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const sp = splitKreft(content, filePath);
|
|
261
|
+
const style = sp.styles[index];
|
|
262
|
+
if (!style) return '';
|
|
263
|
+
|
|
264
|
+
let css = style.css;
|
|
265
|
+
if (isScoped || style.scoped) {
|
|
266
|
+
const scopeId = 'data-s-' + hashString(filePath);
|
|
267
|
+
css = scopeCss(css, scopeId);
|
|
268
|
+
}
|
|
269
|
+
return { code: css, map: null };
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
},
|
|
273
|
+
|
|
274
|
+
transform(code, id) {
|
|
275
|
+
const [filePath, query] = id.split('?');
|
|
276
|
+
if (!isComponentFile(filePath)) return null;
|
|
277
|
+
if (query && query.includes('type=style')) return null;
|
|
278
|
+
if (!filter(filePath)) return null;
|
|
279
|
+
|
|
280
|
+
// 1. Route files: routes/web.kreftjs or any .kreftjs file without <view>
|
|
281
|
+
if (!/<view(\s[^>]*)?>([\s\S]*)<\/view>/i.test(code)) {
|
|
282
|
+
const baseName = path.basename(filePath);
|
|
283
|
+
return {
|
|
284
|
+
code: `import Kreftjs from 'kreftjs';
|
|
285
|
+
|
|
286
|
+
export function routes(Route) {
|
|
287
|
+
${code}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (typeof window !== 'undefined' && window.Kreftjs && window.Kreftjs.__routes) {
|
|
291
|
+
window.Kreftjs.__routes(${JSON.stringify(baseName)}, routes);
|
|
292
|
+
window.Kreftjs.__routes(${JSON.stringify(filePath)}, routes);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (import.meta.hot) {
|
|
296
|
+
import.meta.hot.accept((newMod) => {
|
|
297
|
+
if (newMod && newMod.default && typeof window !== 'undefined' && window.Kreftjs && window.Kreftjs.__routes) {
|
|
298
|
+
window.Kreftjs.__routes(${JSON.stringify(baseName)}, newMod.default);
|
|
299
|
+
window.Kreftjs.__routes(${JSON.stringify(filePath)}, newMod.default);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export default routes;
|
|
305
|
+
`,
|
|
306
|
+
map: null,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// 2. Component file
|
|
311
|
+
const sp = splitKreft(code, filePath);
|
|
312
|
+
fileCache.set(filePath, { code, sp });
|
|
313
|
+
|
|
314
|
+
const hasScoped = sp.styles.some((s) => s.scoped);
|
|
315
|
+
const scopeId = hasScoped ? 'data-s-' + hashString(filePath) : null;
|
|
316
|
+
|
|
317
|
+
// Style imports (Vite will process each as virtual CSS module)
|
|
318
|
+
const styleImports = sp.styles.map((s, idx) => {
|
|
319
|
+
const queryStr = `?kreft&type=style&index=${idx}${s.scoped ? '&scoped=true' : ''}.css`;
|
|
320
|
+
return `import ${JSON.stringify(filePath + queryStr)};`;
|
|
321
|
+
}).join('\n');
|
|
322
|
+
|
|
323
|
+
// Component script imports
|
|
324
|
+
const topImports = [];
|
|
325
|
+
const components = [];
|
|
326
|
+
const values = [];
|
|
327
|
+
|
|
328
|
+
for (const stmt of sp.imports) {
|
|
329
|
+
const m = stmt.match(/^import\s+(?:([\s\S]+?)\s+from\s+)?(['"])(.+?)\2/);
|
|
330
|
+
if (!m) continue;
|
|
331
|
+
const [, what, , spec] = m;
|
|
332
|
+
if (/^(?:kreftjs|kreft|som)(\.js)?$/.test(spec)) continue;
|
|
333
|
+
if (/\.css(?:[?#].*)?$/i.test(spec)) {
|
|
334
|
+
topImports.push(`import ${JSON.stringify(spec)};`);
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (/\.(?:kreftjs|kreft|som)$/i.test(spec)) {
|
|
338
|
+
topImports.push(`import ${what} from ${JSON.stringify(spec)};`);
|
|
339
|
+
const names = extractImportedNames(what);
|
|
340
|
+
names.forEach((name) => {
|
|
341
|
+
components.push(`${JSON.stringify(name)}: ${name}`);
|
|
342
|
+
});
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
// .js / .ts / npm module
|
|
346
|
+
topImports.push(stmt.endsWith(';') ? stmt : stmt + ';');
|
|
347
|
+
const names = extractImportedNames(what);
|
|
348
|
+
names.forEach((name) => {
|
|
349
|
+
values.push(`${JSON.stringify(name)}: ${name}`);
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Props mapping
|
|
354
|
+
const props = Object.entries(sp.props).map(([p, expr]) => {
|
|
355
|
+
if (expr == null) return `${JSON.stringify(p)}: {}`;
|
|
356
|
+
return `${JSON.stringify(p)}: { default: function () { return (${expr}); } }`;
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
// Precompiled expressions (for fast runtime & CSP compliance)
|
|
360
|
+
const fns = new Map();
|
|
361
|
+
if (precompile) {
|
|
362
|
+
collectExpressions(sp.template, fns);
|
|
363
|
+
}
|
|
364
|
+
const precompiledSrc = [...fns]
|
|
365
|
+
.map(([key, fnSrc]) => `F[${JSON.stringify(key)}] = ${fnSrc};`)
|
|
366
|
+
.join('\n');
|
|
367
|
+
|
|
368
|
+
const runCode = 'with ($scope) {\n' + sp.code + '\n}';
|
|
369
|
+
const baseName = path.basename(filePath);
|
|
370
|
+
|
|
371
|
+
const output = `import Kreftjs from 'kreftjs';
|
|
372
|
+
${topImports.join('\n')}
|
|
373
|
+
${styleImports}
|
|
374
|
+
|
|
375
|
+
var F = (typeof window !== 'undefined' && window.Kreftjs && window.Kreftjs.__precompiled) || {};
|
|
376
|
+
${precompiledSrc}
|
|
377
|
+
|
|
378
|
+
var __run = function ($scope, $h) {
|
|
379
|
+
${runCode}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
var __def = Kreftjs.__makeDef({
|
|
383
|
+
name: ${JSON.stringify(sp.name)},
|
|
384
|
+
template: ${JSON.stringify(sp.template)},
|
|
385
|
+
props: { ${props.join(', ')} },
|
|
386
|
+
events: ${JSON.stringify(sp.events)},
|
|
387
|
+
layout: ${sp.layout !== undefined ? JSON.stringify(sp.layout) : 'undefined'},
|
|
388
|
+
scopeId: ${scopeId ? JSON.stringify(scopeId) : 'null'},
|
|
389
|
+
components: { ${components.join(', ')} },
|
|
390
|
+
run: __run
|
|
391
|
+
}, { ${values.join(', ')} });
|
|
392
|
+
|
|
393
|
+
if (typeof window !== 'undefined' && window.Kreftjs && window.Kreftjs.__registerDef) {
|
|
394
|
+
window.Kreftjs.__registerDef(${JSON.stringify(filePath)}, __def);
|
|
395
|
+
window.Kreftjs.__registerDef(${JSON.stringify(baseName)}, __def);
|
|
396
|
+
window.Kreftjs.__registerDef(${JSON.stringify(sp.name + '.kreftjs')}, __def);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (import.meta.hot) {
|
|
400
|
+
import.meta.hot.accept((newMod) => {
|
|
401
|
+
if (newMod && newMod.default && typeof window !== 'undefined' && window.Kreftjs && window.Kreftjs.__hotDef) {
|
|
402
|
+
window.Kreftjs.__hotDef(${JSON.stringify(filePath)}, newMod.default);
|
|
403
|
+
window.Kreftjs.__hotDef(${JSON.stringify(baseName)}, newMod.default);
|
|
404
|
+
window.Kreftjs.__hotDef(${JSON.stringify(sp.name + '.kreftjs')}, newMod.default);
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export default __def;
|
|
410
|
+
`;
|
|
411
|
+
|
|
412
|
+
return {
|
|
413
|
+
code: output,
|
|
414
|
+
map: null,
|
|
415
|
+
};
|
|
416
|
+
},
|
|
417
|
+
|
|
418
|
+
handleHotUpdate(ctx) {
|
|
419
|
+
if (!isComponentFile(ctx.file)) return;
|
|
420
|
+
|
|
421
|
+
const cached = fileCache.get(ctx.file);
|
|
422
|
+
if (!cached) return;
|
|
423
|
+
|
|
424
|
+
try {
|
|
425
|
+
const freshContent = fs.readFileSync(ctx.file, 'utf8');
|
|
426
|
+
const freshSp = splitKreft(freshContent, ctx.file);
|
|
427
|
+
|
|
428
|
+
// Check if only styles changed
|
|
429
|
+
const onlyStyleChanged =
|
|
430
|
+
cached.sp.template === freshSp.template &&
|
|
431
|
+
cached.sp.code === freshSp.code &&
|
|
432
|
+
JSON.stringify(cached.sp.props) === JSON.stringify(freshSp.props) &&
|
|
433
|
+
JSON.stringify(cached.sp.imports) === JSON.stringify(freshSp.imports);
|
|
434
|
+
|
|
435
|
+
fileCache.set(ctx.file, { code: freshContent, sp: freshSp });
|
|
436
|
+
|
|
437
|
+
if (onlyStyleChanged) {
|
|
438
|
+
// Invalidate and reload only the style modules
|
|
439
|
+
const mods = [];
|
|
440
|
+
for (const mod of ctx.modules) {
|
|
441
|
+
if (mod.id && mod.id.includes('?kreft&type=style')) {
|
|
442
|
+
ctx.server.moduleGraph.invalidateModule(mod);
|
|
443
|
+
mods.push(mod);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (mods.length) return mods;
|
|
447
|
+
}
|
|
448
|
+
} catch (_) {
|
|
449
|
+
// Fall back to standard HMR
|
|
450
|
+
}
|
|
451
|
+
},
|
|
452
|
+
|
|
453
|
+
transformIndexHtml(html) {
|
|
454
|
+
if (html.includes('__KREFT_VITE__')) return html;
|
|
455
|
+
return html.replace(/<head[^>]*>/i, (m) => `${m}\n <script>window.__KREFT_VITE__ = true;</script>`);
|
|
456
|
+
},
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
module.exports = kreftPlugin;
|
|
461
|
+
module.exports.default = kreftPlugin;
|
|
462
|
+
module.exports.kreftjs = kreftPlugin;
|
|
463
|
+
module.exports.kreft = kreftPlugin; // the earlier Kreft name
|
|
464
|
+
module.exports.som = kreftPlugin; // the Som.js names
|
|
465
|
+
module.exports.somPlugin = kreftPlugin;
|
|
466
|
+
module.exports.kreftPlugin = kreftPlugin;
|