sigpro 2.0.10 → 2.1.2
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 +3 -3
- package/package.json +31 -69
- package/sigpro.d.ts +88 -21
- package/src/sigpro.convert.js +151 -0
- package/src/sigpro.core.js +444 -0
- package/src/sigpro.db.js +25 -0
- package/src/sigpro.hmr.js +47 -0
- package/src/sigpro.js +271 -0
- package/src/sigpro.lang.js +21 -0
- package/src/sigpro.router.js +68 -0
- package/src/sigpro.vite.js +146 -0
- package/dist/sigpro.calendar.js +0 -1
- package/dist/sigpro.chart.js +0 -20
- package/dist/sigpro.convert.js +0 -34
- package/dist/sigpro.editor.js +0 -1
- package/dist/sigpro.grid.js +0 -81
- package/dist/sigpro.js +0 -1
- package/dist/sigpro.pdf.js +0 -119
- package/dist/sigpro.ui.js +0 -1
- package/dist/sigpro.utils.js +0 -1
- package/dist/sigpro.vite.js +0 -15
package/src/sigpro.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { effect, effectScope, reportError } from './sigpro.core.js';
|
|
2
|
+
export * from './sigpro.core.js';
|
|
3
|
+
|
|
4
|
+
const DOC = typeof document !== 'undefined' ? document : null;
|
|
5
|
+
|
|
6
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
7
|
+
const SVG_TAGS = new Set(
|
|
8
|
+
('svg path circle rect line polyline polygon g defs text textPath tspan use symbol image marker ellipse ' +
|
|
9
|
+
'foreignObject clipPath mask linearGradient radialGradient pattern filter stop ' +
|
|
10
|
+
'animate animateMotion animateTransform view desc metadata title switch ' +
|
|
11
|
+
'feGaussianBlur feOffset feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix ' +
|
|
12
|
+
'feDiffuseLighting feDisplacementMap feDistantLight feFlood feFuncA feFuncB feFuncG feFuncR ' +
|
|
13
|
+
'feImage feMerge feMergeNode feMorphology fePointLight feSpecularLighting feSpotLight feTile feTurbulence'
|
|
14
|
+
).split(' ')
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
const BOOLEAN_PROPS = new Set([
|
|
18
|
+
'allowFullscreen', 'async', 'autofocus', 'autoplay', 'checked', 'controls',
|
|
19
|
+
'defer', 'disabled', 'formNoValidate', 'hidden', 'inert', 'loop', 'multiple',
|
|
20
|
+
'muted', 'noValidate', 'open', 'playsInline', 'readOnly', 'required', 'reversed', 'selected',
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
function setAttr(el, key, res, isSVG) {
|
|
24
|
+
const k = key.toLowerCase();
|
|
25
|
+
if (k === 'innerhtml' || k === 'outerhtml' || k === 'srcdoc') return;
|
|
26
|
+
if (k.startsWith('on') && res != null && res !== false) return;
|
|
27
|
+
if (/^(href|src|action|formaction|poster|data|xlink:href)$/i.test(key) && res != null && res !== false) {
|
|
28
|
+
if (typeof res !== 'string') return;
|
|
29
|
+
try {
|
|
30
|
+
const proto = new URL(res, 'https://placeholder.invalid/').protocol;
|
|
31
|
+
const safeData = proto === 'data:' && k === 'src' &&
|
|
32
|
+
/^(img|source|video|audio|track|input)$/i.test(el.tagName) &&
|
|
33
|
+
/^data:image\/(png|jpe?g|gif|webp|avif|bmp);/i.test(res.trim());
|
|
34
|
+
if (!/^(https?|mailto|tel|blob):$/.test(proto) && !safeData) return;
|
|
35
|
+
} catch { return; }
|
|
36
|
+
}
|
|
37
|
+
if (key === 'class' || key === 'className') {
|
|
38
|
+
if (isSVG) {
|
|
39
|
+
if (res == null || res === false) el.removeAttribute('class');
|
|
40
|
+
else el.setAttribute('class', res === true ? '' : res);
|
|
41
|
+
} else {
|
|
42
|
+
el.className = res ?? '';
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (BOOLEAN_PROPS.has(key) && !isSVG && key in el) {
|
|
47
|
+
const on = res != null && res !== false;
|
|
48
|
+
el[key] = on;
|
|
49
|
+
if (on) el.setAttribute(key, '');
|
|
50
|
+
else el.removeAttribute(key);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (res == null || res === false) {
|
|
54
|
+
if (key === 'style') el._lastStyle = undefined;
|
|
55
|
+
el.removeAttribute(key);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (key === 'style') {
|
|
59
|
+
const prev = el._lastStyle;
|
|
60
|
+
if (typeof res === 'string') {
|
|
61
|
+
if (prev !== undefined) {
|
|
62
|
+
el.removeAttribute('style');
|
|
63
|
+
el._lastStyle = undefined;
|
|
64
|
+
}
|
|
65
|
+
el.setAttribute('style', res);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (typeof res === 'object') {
|
|
69
|
+
if (prev) for (const p in prev) if (!(p in res)) {
|
|
70
|
+
if (p.startsWith('--')) el.style.removeProperty(p);
|
|
71
|
+
else el.style[p] = '';
|
|
72
|
+
}
|
|
73
|
+
for (const p in res) {
|
|
74
|
+
const v = res[p];
|
|
75
|
+
if (p.startsWith('--')) {
|
|
76
|
+
if (v == null || v === false) el.style.removeProperty(p);
|
|
77
|
+
else el.style.setProperty(p, String(v));
|
|
78
|
+
} else {
|
|
79
|
+
el.style[p] = v == null || v === false ? '' : String(v);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
el._lastStyle = res;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (isSVG) {
|
|
87
|
+
el.setAttribute(key, res === true ? '' : res);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (key in el && typeof el[key] !== 'function') el[key] = res;
|
|
91
|
+
else el.setAttribute(key, res === true ? '' : res);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
export function h(tag, props = {}, ...children) {
|
|
97
|
+
if (tag == null) {
|
|
98
|
+
console.error('[sigpro] h() called with no tag');
|
|
99
|
+
return DOC.createComment('invalid tag');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (typeof tag === 'function') {
|
|
103
|
+
let element;
|
|
104
|
+
const stop = effectScope(() => {
|
|
105
|
+
try {
|
|
106
|
+
element = tag(props, children);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
reportError(err, tag);
|
|
109
|
+
element = DOC.createComment('error');
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
const mark = (n) => {
|
|
113
|
+
if (n instanceof Node) n._stopScope = stop;
|
|
114
|
+
else if (Array.isArray(n)) n.forEach(mark);
|
|
115
|
+
};
|
|
116
|
+
mark(element);
|
|
117
|
+
return element;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (typeof tag !== 'string') {
|
|
121
|
+
console.error('[sigpro] h() tag must be string or function, got', typeof tag);
|
|
122
|
+
return DOC.createComment('invalid tag');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (
|
|
126
|
+
props instanceof Node ||
|
|
127
|
+
Array.isArray(props) ||
|
|
128
|
+
(props !== null && typeof props !== 'object')
|
|
129
|
+
) {
|
|
130
|
+
children = [props, ...children];
|
|
131
|
+
props = {};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const isSVG = SVG_TAGS.has(tag);
|
|
135
|
+
const el = isSVG ? DOC.createElementNS(SVG_NS, tag) : DOC.createElement(tag);
|
|
136
|
+
|
|
137
|
+
let deferredSelectValue;
|
|
138
|
+
|
|
139
|
+
for (const key of Object.keys(props || {})) {
|
|
140
|
+
const val = props[key];
|
|
141
|
+
|
|
142
|
+
if (key === 'ref') {
|
|
143
|
+
if (typeof val === 'function') val(el);
|
|
144
|
+
else if (val != null && typeof val === 'object') val.current = el;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (tag === 'select' && key === 'value') {
|
|
149
|
+
deferredSelectValue = val;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (key.startsWith('on') && typeof val === 'function') {
|
|
154
|
+
const ev = key.slice(2).toLowerCase();
|
|
155
|
+
el.addEventListener(ev, val);
|
|
156
|
+
(el._cln || (el._cln = [])).push(() => el.removeEventListener(ev, val));
|
|
157
|
+
} else if (key.startsWith('on') && val != null && val !== false) {
|
|
158
|
+
console.error(`[sigpro] "${key}" expects a function, got ${typeof val}`);
|
|
159
|
+
} else if (typeof val === 'function') {
|
|
160
|
+
effect(() => setAttr(el, key, val(), isSVG));
|
|
161
|
+
} else {
|
|
162
|
+
setAttr(el, key, val, isSVG);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (tag === 'a' && el.target === '_blank' && !el.rel) {
|
|
167
|
+
el.rel = 'noopener noreferrer';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const append = (c) => {
|
|
171
|
+
if (Array.isArray(c)) { for (const x of c) append(x); return; }
|
|
172
|
+
if (c == null || c === false || c === true) return;
|
|
173
|
+
|
|
174
|
+
if (typeof c === 'function') {
|
|
175
|
+
const anchor = DOC.createComment('');
|
|
176
|
+
el.appendChild(anchor);
|
|
177
|
+
const stop = effect(() => {
|
|
178
|
+
const res = c();
|
|
179
|
+
const list = Array.isArray(res) ? res : [res];
|
|
180
|
+
const nodes = [];
|
|
181
|
+
for (const item of list) {
|
|
182
|
+
if (item == null || item === false || item === true) continue;
|
|
183
|
+
nodes.push(item instanceof Node ? item : DOC.createTextNode(String(item)));
|
|
184
|
+
}
|
|
185
|
+
for (const n of nodes) el.insertBefore(n, anchor);
|
|
186
|
+
return () => {
|
|
187
|
+
for (const n of nodes) {
|
|
188
|
+
cleanupNode(n);
|
|
189
|
+
n.remove();
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
});
|
|
193
|
+
(el._cln || (el._cln = [])).push(stop);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
el.appendChild(c instanceof Node ? c : DOC.createTextNode(String(c)));
|
|
198
|
+
};
|
|
199
|
+
append(children);
|
|
200
|
+
|
|
201
|
+
if (deferredSelectValue !== undefined) {
|
|
202
|
+
if (typeof deferredSelectValue === 'function') {
|
|
203
|
+
effect(() => setAttr(el, 'value', deferredSelectValue(), isSVG));
|
|
204
|
+
} else {
|
|
205
|
+
setAttr(el, 'value', deferredSelectValue, isSVG);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return el;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function mount(component, target) {
|
|
213
|
+
const el = typeof target === 'string' ? DOC.querySelector(target) : target;
|
|
214
|
+
if (!el) return () => { };
|
|
215
|
+
|
|
216
|
+
let node;
|
|
217
|
+
const stopRoot = effectScope(() => { node = h(component, {}); });
|
|
218
|
+
|
|
219
|
+
const nodes = (Array.isArray(node) ? node : [node])
|
|
220
|
+
.filter(n => n != null && n !== false && n !== true)
|
|
221
|
+
.map(n => (n instanceof Node ? n : DOC.createTextNode(String(n))));
|
|
222
|
+
|
|
223
|
+
while (el.firstChild) {
|
|
224
|
+
cleanupNode(el.firstChild);
|
|
225
|
+
el.removeChild(el.firstChild);
|
|
226
|
+
}
|
|
227
|
+
for (const n of nodes) el.appendChild(n);
|
|
228
|
+
|
|
229
|
+
return () => {
|
|
230
|
+
stopRoot();
|
|
231
|
+
for (const n of nodes) cleanupNode(n);
|
|
232
|
+
el.replaceChildren();
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function unmount(node) {
|
|
237
|
+
if (!(node instanceof Node)) return;
|
|
238
|
+
cleanupNode(node);
|
|
239
|
+
if (node.parentNode) node.parentNode.removeChild(node);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const htmlTags = "a abbr article aside audio b blockquote br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main mark meter nav object ol optgroup option output p picture pre progress section select slot small source span strong sub summary sup table tbody td template textarea tfoot th thead time tr u ul video";
|
|
243
|
+
|
|
244
|
+
export const exposeTags = (target = typeof window !== 'undefined' ? window : null) => {
|
|
245
|
+
if (!target) return;
|
|
246
|
+
for (const tag of htmlTags.split(' ')) {
|
|
247
|
+
target[tag] = (props, ...children) => h(tag, props, ...children);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
export const slots = children => {
|
|
252
|
+
const s = children[0]
|
|
253
|
+
return (
|
|
254
|
+
children.length === 1 && s && typeof s === 'object' &&
|
|
255
|
+
!Array.isArray(s) && !(s instanceof Node)
|
|
256
|
+
? s
|
|
257
|
+
: { default: () => children }
|
|
258
|
+
)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/* HELPERS */
|
|
262
|
+
export const onPrevent = fn => e => { e.preventDefault(); fn(e); };
|
|
263
|
+
export const onStop = fn => e => { e.stopPropagation(); fn(e); };
|
|
264
|
+
export const onSelf = fn => e => { if (e.target === e.currentTarget) fn(e); };
|
|
265
|
+
export const onOnce = fn => { let c = false; return e => { if (!c) { c = true; fn(e); } }; };
|
|
266
|
+
export const onDebounce = (ms, fn) => { let t; return e => { clearTimeout(t); t = setTimeout(() => fn(e), ms); }; };
|
|
267
|
+
export const onKey = (key, fn) => e => { if (e.key === key) fn(e); };
|
|
268
|
+
export const onEnter = fn => onKey("Enter", fn);
|
|
269
|
+
export const onEscape = fn => onKey("Escape", fn);
|
|
270
|
+
export const bindvalue = s => ({ value: () => s(), oninput: e => s(e.target.value) });
|
|
271
|
+
export const bindcheck = s => ({ checked: () => !!s(), onchange: e => s(e.target.checked) });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// sigpro.lang.js
|
|
2
|
+
|
|
3
|
+
import { signal } from 'sigpro';
|
|
4
|
+
|
|
5
|
+
export const currentLocale = signal('en');
|
|
6
|
+
|
|
7
|
+
const translations = {};
|
|
8
|
+
|
|
9
|
+
export const addLang = obj => {
|
|
10
|
+
for (const locale of Object.keys(obj)) {
|
|
11
|
+
translations[locale] ||= {};
|
|
12
|
+
Object.assign(translations[locale], obj[locale]);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const setLocale = locale => {
|
|
17
|
+
if (locale && translations[locale]) currentLocale(locale);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const t = key => () =>
|
|
21
|
+
translations[currentLocale()]?.[key] ?? key;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// sigpro.router.js
|
|
2
|
+
import { signal, effect } from 'sigpro';
|
|
3
|
+
|
|
4
|
+
export const currentPath = signal(typeof window !== 'undefined' ? location.hash.slice(1) || '/' : '/');
|
|
5
|
+
export const routerParams = signal({});
|
|
6
|
+
|
|
7
|
+
if (typeof window !== 'undefined') {
|
|
8
|
+
addEventListener('hashchange', () => currentPath(location.hash.slice(1) || '/'));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const hmrTick = signal(0);
|
|
12
|
+
const hmrRegistry = new Map();
|
|
13
|
+
window.__sigpro_hmr_page__ = (url, component) => { hmrRegistry.set(url, component); hmrTick(hmrTick() + 1); };
|
|
14
|
+
|
|
15
|
+
export const router = routes => {
|
|
16
|
+
const sorted = [...routes].sort((a, b) => a.path.split('/').filter(p => p[0] === ':').length -
|
|
17
|
+
b.path.split('/').filter(p => p[0] === ':').length || (b.path === '*') - (a.path === '*'));
|
|
18
|
+
|
|
19
|
+
const hook = document.createElement('div');
|
|
20
|
+
hook.classList.add('router-hook');
|
|
21
|
+
let scopeStop, lastPath, lastComponent;
|
|
22
|
+
|
|
23
|
+
const clearHook = () => {
|
|
24
|
+
const stack = [...hook.childNodes];
|
|
25
|
+
while (stack.length) {
|
|
26
|
+
const n = stack.pop();
|
|
27
|
+
if (n._stopScope) { n._stopScope(); n._stopScope = null; }
|
|
28
|
+
if (n._cln) { for (const f of n._cln) f(); n._cln = null; }
|
|
29
|
+
stack.push(...n.childNodes);
|
|
30
|
+
}
|
|
31
|
+
hook.replaceChildren();
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const effectStop = effect(() => {
|
|
35
|
+
hmrTick();
|
|
36
|
+
const hash = currentPath().split(/[?#]/)[0];
|
|
37
|
+
const p2 = hash.split('/').filter(Boolean);
|
|
38
|
+
|
|
39
|
+
let route = sorted.find(r => r.path.split('/').filter(Boolean).length === p2.length &&
|
|
40
|
+
r.path.split('/').every((p, i) => p[0] === ':' || p === p2[i]));
|
|
41
|
+
if (!route) route = sorted[sorted.length - 1];
|
|
42
|
+
|
|
43
|
+
const params = {};
|
|
44
|
+
route.path.split('/').filter(Boolean).reduce((acc, p, i) => {
|
|
45
|
+
if (p[0] === ':') try { params[p.slice(1)] = decodeURIComponent(p2[i]); } catch { params[p.slice(1)] = p2[i]; }
|
|
46
|
+
return acc;
|
|
47
|
+
}, null);
|
|
48
|
+
|
|
49
|
+
const component = hmrRegistry.get(route.path) ?? route.component;
|
|
50
|
+
if (hash === lastPath && component === lastComponent) return;
|
|
51
|
+
lastPath = hash; lastComponent = component;
|
|
52
|
+
|
|
53
|
+
if (scopeStop) { scopeStop(); scopeStop = undefined; }
|
|
54
|
+
clearHook();
|
|
55
|
+
routerParams(params);
|
|
56
|
+
scopeStop = effect(() => {
|
|
57
|
+
const node = typeof component === 'function' ? component(params) : component;
|
|
58
|
+
hook.replaceChildren(...(node == null ? [] : Array.isArray(node) ? node : [node]));
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
hook._stopScope = () => { if (scopeStop) { scopeStop(); scopeStop = undefined; } effectStop(); };
|
|
63
|
+
return hook;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
router.to = path => location.hash = '#/' + path.replace(/^\//, '');
|
|
67
|
+
router.back = () => history.back();
|
|
68
|
+
router.path = () => currentPath();
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// sigpro.vite.js
|
|
2
|
+
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
|
|
6
|
+
export function sigproRouter({ pagesDir = "src/pages" } = {}) {
|
|
7
|
+
const virtualModuleId = "virtual:sigpro-routes";
|
|
8
|
+
const resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
9
|
+
|
|
10
|
+
const root = process.cwd();
|
|
11
|
+
const pagesRoot = path.resolve(root, pagesDir);
|
|
12
|
+
const pagesRootPosix = pagesRoot.split(path.sep).join("/");
|
|
13
|
+
|
|
14
|
+
const isPage = (f) =>
|
|
15
|
+
/\.(js|jsx)$/.test(f) && !path.basename(f).startsWith("_");
|
|
16
|
+
|
|
17
|
+
const getFiles = (dir) => {
|
|
18
|
+
if (!fs.existsSync(dir)) return [];
|
|
19
|
+
return fs
|
|
20
|
+
.readdirSync(dir, { recursive: true })
|
|
21
|
+
.filter(isPage)
|
|
22
|
+
.map((file) => path.resolve(dir, file));
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const pathToUrl = (dir, filePath) => {
|
|
26
|
+
const relative = path
|
|
27
|
+
.relative(dir, filePath)
|
|
28
|
+
.replace(/\\/g, "/")
|
|
29
|
+
.replace(/\.(js|jsx)$/, "")
|
|
30
|
+
.replace(/\/index$/, "")
|
|
31
|
+
.replace(/^index$/, "");
|
|
32
|
+
return (
|
|
33
|
+
("/" + relative)
|
|
34
|
+
.replace(/\/+/g, "/")
|
|
35
|
+
.replace(/\[\.\.\.([^\]]+)\]/g, "*")
|
|
36
|
+
.replace(/\[([^\]]+)\]/g, ":$1")
|
|
37
|
+
.replace(/\/$/, "") || "/"
|
|
38
|
+
);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const sortRoutes = (dir, files) =>
|
|
42
|
+
files.slice().sort((a, b) => {
|
|
43
|
+
const ua = pathToUrl(dir, a),
|
|
44
|
+
ub = pathToUrl(dir, b);
|
|
45
|
+
const wa = ua.includes(":") || ua.includes("*");
|
|
46
|
+
const wb = ub.includes(":") || ub.includes("*");
|
|
47
|
+
if (wa !== wb) return wa ? 1 : -1;
|
|
48
|
+
return ub.length - ua.length;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const generateModule = () => {
|
|
52
|
+
const dir = pagesRoot;
|
|
53
|
+
const files = sortRoutes(dir, getFiles(dir));
|
|
54
|
+
|
|
55
|
+
let imports = "";
|
|
56
|
+
let routes = "";
|
|
57
|
+
|
|
58
|
+
files.forEach((fullPath, i) => {
|
|
59
|
+
const url = pathToUrl(dir, fullPath);
|
|
60
|
+
const importPath =
|
|
61
|
+
"/" + path.relative(root, fullPath).replace(/\\/g, "/");
|
|
62
|
+
const name = `Page${i}`;
|
|
63
|
+
imports += `import ${name} from '${importPath}';\n`;
|
|
64
|
+
routes += ` { path: '${url}', component: ${name} },\n`;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
routes += ` { path: '*', component: () => { const d = document.createElement('div'); d.className = 'not-found'; d.textContent = '404 - Not Found'; return d; } },\n`;
|
|
68
|
+
|
|
69
|
+
return `${imports}\nexport const routes = [\n${routes}];`;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
name: "sigpro-router",
|
|
74
|
+
|
|
75
|
+
resolveId(id) {
|
|
76
|
+
if (id === virtualModuleId) return resolvedVirtualModuleId;
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
load(id) {
|
|
80
|
+
if (id !== resolvedVirtualModuleId) return;
|
|
81
|
+
return generateModule();
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
transform(code, id) {
|
|
85
|
+
const clean = id.split("?")[0];
|
|
86
|
+
const cleanPosix = clean.split(path.sep).join("/");
|
|
87
|
+
|
|
88
|
+
if (!cleanPosix.startsWith(pagesRootPosix + "/")) return;
|
|
89
|
+
if (!/\.(js|jsx)$/.test(cleanPosix)) return;
|
|
90
|
+
if (path.basename(cleanPosix).startsWith("_")) return;
|
|
91
|
+
|
|
92
|
+
const url = pathToUrl(pagesRoot, clean);
|
|
93
|
+
|
|
94
|
+
const hmr = `
|
|
95
|
+
;
|
|
96
|
+
if (import.meta.hot) {
|
|
97
|
+
import.meta.hot.accept((newMod) => {
|
|
98
|
+
if (newMod && typeof window !== 'undefined' && window.__sigpro_hmr_page__) {
|
|
99
|
+
window.__sigpro_hmr_page__(${JSON.stringify(url)}, newMod.default ?? newMod);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
`;
|
|
104
|
+
return { code: code + hmr, map: null };
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
configureServer(server) {
|
|
108
|
+
const onChange = (file) => {
|
|
109
|
+
const posix = file.split(path.sep).join("/");
|
|
110
|
+
if (!posix.startsWith(pagesRootPosix + "/")) return;
|
|
111
|
+
if (!isPage(posix)) return;
|
|
112
|
+
const vm = server.moduleGraph.getModuleById(resolvedVirtualModuleId);
|
|
113
|
+
if (vm) server.moduleGraph.invalidateModule(vm);
|
|
114
|
+
server.ws.send({ type: "full-reload" });
|
|
115
|
+
};
|
|
116
|
+
server.watcher.on("add", onChange);
|
|
117
|
+
server.watcher.on("unlink", onChange);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* Vite Config
|
|
123
|
+
|
|
124
|
+
import { defineConfig } from "vite";
|
|
125
|
+
import tailwindcss from "@tailwindcss/vite";
|
|
126
|
+
import { sigproRouter } from "./sigpro.vite.js";
|
|
127
|
+
|
|
128
|
+
const dev = process.env.NODE_ENV === "development";
|
|
129
|
+
|
|
130
|
+
export default defineConfig({
|
|
131
|
+
plugins: [sigproRouter({ pagesDir: "src/pages" }), tailwindcss()],
|
|
132
|
+
base: dev ? "/absproxy/5173/" : "/",
|
|
133
|
+
server: { host: "0.0.0.0", allowedHosts: true, port: 5173 },
|
|
134
|
+
build: { chunkSizeWarningLimit: 1500 },
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
*/
|
|
138
|
+
|
|
139
|
+
/* main.js
|
|
140
|
+
if (import.meta.hot) {
|
|
141
|
+
import.meta.hot.accept(["./App.js"], ([newApp]) => {
|
|
142
|
+
dispose?.();
|
|
143
|
+
dispose = mount(newApp.App, "#app");
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
*/
|