skeleton-load 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +455 -2
- package/dist/cli.js +255 -0
- package/dist/index.cjs +1347 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1338 -0
- package/dist/index.js.map +1 -0
- package/dist/next/index.cjs +425 -0
- package/dist/next/index.cjs.map +1 -0
- package/dist/next/index.d.cts +19 -0
- package/dist/next/index.d.ts +19 -0
- package/dist/next/index.js +420 -0
- package/dist/next/index.js.map +1 -0
- package/dist/plugins/vite.cjs +413 -0
- package/dist/plugins/vite.cjs.map +1 -0
- package/dist/plugins/vite.d.cts +40 -0
- package/dist/plugins/vite.d.ts +40 -0
- package/dist/plugins/vite.js +408 -0
- package/dist/plugins/vite.js.map +1 -0
- package/dist/plugins/webpack.cjs +411 -0
- package/dist/plugins/webpack.cjs.map +1 -0
- package/dist/plugins/webpack.d.cts +43 -0
- package/dist/plugins/webpack.d.ts +43 -0
- package/dist/plugins/webpack.js +406 -0
- package/dist/plugins/webpack.js.map +1 -0
- package/dist/react/index.cjs +1387 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +48 -0
- package/dist/react/index.d.ts +48 -0
- package/dist/react/index.js +1382 -0
- package/dist/react/index.js.map +1 -0
- package/dist/singleton-D0608W4u.d.cts +74 -0
- package/dist/singleton-D4bvf9fp.d.ts +74 -0
- package/dist/skeleton-load.auto.js +328 -0
- package/dist/skeleton-load.css +326 -0
- package/dist/skeleton-load.record.js +21 -0
- package/dist/types-DNYMIyOJ.d.cts +73 -0
- package/dist/types-DNYMIyOJ.d.ts +73 -0
- package/dist/vanilla.cjs +1356 -0
- package/dist/vanilla.cjs.map +1 -0
- package/dist/vanilla.d.cts +23 -0
- package/dist/vanilla.d.ts +23 -0
- package/dist/vanilla.js +1353 -0
- package/dist/vanilla.js.map +1 -0
- package/dist/vue/index.cjs +1379 -0
- package/dist/vue/index.cjs.map +1 -0
- package/dist/vue/index.d.cts +145 -0
- package/dist/vue/index.d.ts +145 -0
- package/dist/vue/index.js +1373 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +104 -4
package/dist/cli.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli/index.ts
|
|
4
|
+
import { createServer } from "node:http";
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
7
|
+
import { dirname, extname, join, relative, resolve } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
var here = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
var paint = (code) => (value) => process.stdout.isTTY ? `\x1B[${code}m${value}\x1B[0m` : value;
|
|
11
|
+
var bold = paint(1);
|
|
12
|
+
var dim = paint(2);
|
|
13
|
+
var green = paint(32);
|
|
14
|
+
var cyan = paint(36);
|
|
15
|
+
var yellow = paint(33);
|
|
16
|
+
var red = paint(31);
|
|
17
|
+
function minifyCss(css) {
|
|
18
|
+
return css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\s+/g, " ").replace(/\s*([{}:;,>])\s*/g, "$1").replace(/;}/g, "}").trim();
|
|
19
|
+
}
|
|
20
|
+
async function asset(name) {
|
|
21
|
+
try {
|
|
22
|
+
return await readFile(join(here, name), "utf8");
|
|
23
|
+
} catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function args(argv) {
|
|
28
|
+
const [command = "help", ...rest] = argv;
|
|
29
|
+
const flags = /* @__PURE__ */ new Map();
|
|
30
|
+
for (let i = 0; i < rest.length; i++) {
|
|
31
|
+
const token = rest[i];
|
|
32
|
+
if (!token || !token.startsWith("--")) continue;
|
|
33
|
+
const key = token.slice(2);
|
|
34
|
+
const next = rest[i + 1];
|
|
35
|
+
if (next && !next.startsWith("--")) {
|
|
36
|
+
flags.set(key, next);
|
|
37
|
+
i++;
|
|
38
|
+
} else {
|
|
39
|
+
flags.set(key, true);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { command, flags };
|
|
43
|
+
}
|
|
44
|
+
async function walk(dir, files = [], depth = 0) {
|
|
45
|
+
if (depth > 8) return files;
|
|
46
|
+
let entries;
|
|
47
|
+
try {
|
|
48
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
49
|
+
} catch {
|
|
50
|
+
return files;
|
|
51
|
+
}
|
|
52
|
+
for (const entry of entries) {
|
|
53
|
+
if (entry.name === "node_modules" || entry.name.startsWith(".")) continue;
|
|
54
|
+
const full = join(dir, entry.name);
|
|
55
|
+
if (entry.isDirectory()) await walk(full, files, depth + 1);
|
|
56
|
+
else if (extname(entry.name) === ".html") files.push(full);
|
|
57
|
+
}
|
|
58
|
+
return files;
|
|
59
|
+
}
|
|
60
|
+
async function detect(dir) {
|
|
61
|
+
try {
|
|
62
|
+
const raw = await readFile(join(dir, "package.json"), "utf8");
|
|
63
|
+
const pkg = JSON.parse(raw);
|
|
64
|
+
const all = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
65
|
+
return Object.keys(all).filter(
|
|
66
|
+
(name) => ["next", "nuxt", "react", "vue", "vite", "webpack", "@sveltejs/kit", "astro", "svelte"].includes(name)
|
|
67
|
+
).map((name) => ({ name, version: all[name] ?? "" }));
|
|
68
|
+
} catch {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function header() {
|
|
73
|
+
console.log(`${bold("skeleton-load")} ${dim("\xB7 beautiful skeletons for every page")}`);
|
|
74
|
+
console.log();
|
|
75
|
+
}
|
|
76
|
+
function usage() {
|
|
77
|
+
header();
|
|
78
|
+
console.log(`${bold("USAGE")}`);
|
|
79
|
+
console.log(" npx skeleton-load <command> [options]");
|
|
80
|
+
console.log();
|
|
81
|
+
console.log(`${bold("COMMANDS")}`);
|
|
82
|
+
console.log(` ${cyan("init")} Detect your framework and print the exact setup`);
|
|
83
|
+
console.log(` ${cyan("inject")} [--dir dist] Add the runtime + CSS to every built .html file`);
|
|
84
|
+
console.log(` ${cyan("record")} [url] [--port 7317] Click elements in a live page to copy snippets`);
|
|
85
|
+
console.log();
|
|
86
|
+
console.log(`${bold("ELEMENT API")}`);
|
|
87
|
+
console.log(" <img> / <video> / <iframe> automatic, no attribute needed");
|
|
88
|
+
console.log(' data-skeleton="auto" skeleton every child of a container');
|
|
89
|
+
console.log(' data-skeleton="card|grid|list|text|table|media|avatar|chart|profile"');
|
|
90
|
+
console.log(' data-skeleton-state="loading|done" drive it from your own code');
|
|
91
|
+
console.log(' data-skeleton-preset="shimmer|pulse|static"');
|
|
92
|
+
console.log(' data-skeleton="off" never touch this subtree');
|
|
93
|
+
console.log();
|
|
94
|
+
console.log(`${dim("Docs: https://github.com/skeleton-load")}`);
|
|
95
|
+
}
|
|
96
|
+
async function init(dir) {
|
|
97
|
+
header();
|
|
98
|
+
const found = await detect(dir);
|
|
99
|
+
const names = found.map((f) => f.name);
|
|
100
|
+
console.log(`${dim("project:")} ${resolve(dir)}`);
|
|
101
|
+
console.log(`${dim("detected:")} ${found.length ? found.map((f) => `${f.name}@${f.version}`).join(", ") : "no framework (plain HTML)"}`);
|
|
102
|
+
console.log();
|
|
103
|
+
if (names.includes("next")) {
|
|
104
|
+
console.log(`${bold("1) next.config.mjs")}`);
|
|
105
|
+
console.log(cyan(`import { withSkeleton } from 'skeleton-load/next'`));
|
|
106
|
+
console.log(cyan(`export default withSkeleton({}, { preset: 'shimmer', delay: 120 })`));
|
|
107
|
+
} else if (names.includes("vite") || names.includes("svelte") || names.includes("astro")) {
|
|
108
|
+
console.log(`${bold("1) vite.config.ts")}`);
|
|
109
|
+
console.log(cyan(`import { skeletonLoad } from 'skeleton-load/vite'`));
|
|
110
|
+
console.log(cyan(`export default defineConfig({ plugins: [skeletonLoad({ preset: 'shimmer' })] })`));
|
|
111
|
+
} else if (names.includes("react") || names.includes("vue") || names.includes("nuxt")) {
|
|
112
|
+
console.log(`${bold("1) start the engine once (main.ts / plugin)")}`);
|
|
113
|
+
console.log(cyan(`import { ensureStarted } from 'skeleton-load'`));
|
|
114
|
+
console.log(cyan(`ensureStarted({ preset: 'shimmer' })`));
|
|
115
|
+
} else {
|
|
116
|
+
console.log(`${bold("1) add one line before </body>")}`);
|
|
117
|
+
console.log(cyan(`<script src="https://cdn.jsdelivr.net/npm/skeleton-load/dist/skeleton-load.auto.js" defer></script>`));
|
|
118
|
+
}
|
|
119
|
+
console.log();
|
|
120
|
+
console.log(`${bold("2) mark the dynamic zones (one attribute each)")}`);
|
|
121
|
+
console.log(cyan(`<main data-skeleton="auto"> ... </main>`));
|
|
122
|
+
console.log(dim(" images, video and iframes need nothing at all."));
|
|
123
|
+
console.log();
|
|
124
|
+
console.log(`${bold("3) define the shapes you care about")}`);
|
|
125
|
+
console.log(cyan(`<div id="products" data-skeleton="grid" data-skeleton-cols="3"></div>`));
|
|
126
|
+
console.log(cyan(`<section data-skeleton="card"></section>`));
|
|
127
|
+
console.log();
|
|
128
|
+
console.log(`${bold("4) build, then measure")}`);
|
|
129
|
+
console.log(cyan(`npm run build && npx skeleton-load inject --dir dist`));
|
|
130
|
+
console.log(cyan(`npx skeleton-load record http://localhost:3000`));
|
|
131
|
+
}
|
|
132
|
+
async function inject(dir, flags) {
|
|
133
|
+
header();
|
|
134
|
+
const css = await asset("skeleton-load.css");
|
|
135
|
+
const runtime = await asset("skeleton-load.auto.js");
|
|
136
|
+
if (!css || !runtime) {
|
|
137
|
+
console.error(red("dist assets are missing. Run `npm run build` inside the package first."));
|
|
138
|
+
process.exitCode = 1;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const external = typeof flags.get("external") === "string" ? String(flags.get("external")) : null;
|
|
142
|
+
const runtimeTag = external ? `<script src="${external}" defer></script>` : `<script>${runtime}</script>`;
|
|
143
|
+
const code = `<style data-skeleton-load>${minifyCss(css)}</style>${runtimeTag}`;
|
|
144
|
+
const output = resolve(dir);
|
|
145
|
+
const files = await walk(output);
|
|
146
|
+
let touched = 0;
|
|
147
|
+
for (const file of files) {
|
|
148
|
+
const html = await readFile(file, "utf8");
|
|
149
|
+
if (html.includes("data-skeleton-load")) continue;
|
|
150
|
+
const next = /<\/head>/i.test(html) ? html.replace(/<\/head>/i, `${code}</head>`) : `${code}${html}`;
|
|
151
|
+
const { writeFile } = await import("node:fs/promises");
|
|
152
|
+
await writeFile(file, next, "utf8");
|
|
153
|
+
touched++;
|
|
154
|
+
console.log(` ${green("\u2713")} ${relative(process.cwd(), file)}`);
|
|
155
|
+
}
|
|
156
|
+
console.log();
|
|
157
|
+
console.log(`${bold(String(touched))} page(s) updated in ${dim(output)}`);
|
|
158
|
+
console.log(dim("Re-run after a rebuild \u2014 the marker keeps it idempotent."));
|
|
159
|
+
}
|
|
160
|
+
function openBrowser(target) {
|
|
161
|
+
const platform = process.platform;
|
|
162
|
+
const command = platform === "win32" ? "cmd" : platform === "darwin" ? "open" : "xdg-open";
|
|
163
|
+
const params = platform === "win32" ? ["/c", "start", "", target] : [target];
|
|
164
|
+
try {
|
|
165
|
+
spawn(command, params, { stdio: "ignore", detached: true }).unref();
|
|
166
|
+
} catch {
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async function record(target, port) {
|
|
170
|
+
header();
|
|
171
|
+
const code = await asset("skeleton-load.record.js");
|
|
172
|
+
if (!code) {
|
|
173
|
+
console.error(red("skeleton-load.record.js is missing. Run `npm run build` first."));
|
|
174
|
+
process.exitCode = 1;
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const server = createServer((request, response) => {
|
|
178
|
+
const url = request.url ?? "/";
|
|
179
|
+
response.setHeader("Access-Control-Allow-Origin", "*");
|
|
180
|
+
if (url.startsWith("/skeleton-load.record.js")) {
|
|
181
|
+
response.setHeader("Content-Type", "application/javascript; charset=utf-8");
|
|
182
|
+
response.end(code);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
response.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
186
|
+
response.end(`<!doctype html><meta charset="utf-8"><title>skeleton-load recorder</title>
|
|
187
|
+
<body style="font:14px/1.6 ui-sans-serif,system-ui;max-width:640px;margin:64px auto;padding:0 24px">
|
|
188
|
+
<h1 style="font-size:20px">skeleton-load recorder</h1>
|
|
189
|
+
<p>Paste this in the DevTools console of the page you are styling:</p>
|
|
190
|
+
<pre style="background:#111;color:#eee;padding:16px;border-radius:10px;overflow:auto"><code>const s=document.createElement('script');s.src='http://localhost:${port}/skeleton-load.record.js';document.head.appendChild(s)</code></pre>
|
|
191
|
+
<p>Then click any element to copy its data attributes.</p></body>`);
|
|
192
|
+
});
|
|
193
|
+
server.on("error", (error) => {
|
|
194
|
+
if (error.code === "EADDRINUSE") {
|
|
195
|
+
console.error(red(`port ${port} is busy \u2014 try --port ${port + 1}`));
|
|
196
|
+
} else {
|
|
197
|
+
console.error(red(error.message));
|
|
198
|
+
}
|
|
199
|
+
process.exitCode = 1;
|
|
200
|
+
});
|
|
201
|
+
server.listen(port, () => {
|
|
202
|
+
const snippet = `const s=document.createElement('script');s.src='http://localhost:${port}/skeleton-load.record.js';document.head.appendChild(s)`;
|
|
203
|
+
console.log(`${green("recorder ready")} ${dim(`http://localhost:${port}`)}`);
|
|
204
|
+
console.log();
|
|
205
|
+
console.log(`${bold("Paste in the DevTools console:")}`);
|
|
206
|
+
console.log(cyan(snippet));
|
|
207
|
+
console.log();
|
|
208
|
+
if (target) {
|
|
209
|
+
openBrowser(target);
|
|
210
|
+
console.log(`${dim("opened")} ${target}`);
|
|
211
|
+
}
|
|
212
|
+
console.log(dim("Ctrl+C stops the recorder."));
|
|
213
|
+
});
|
|
214
|
+
const shutdown = () => {
|
|
215
|
+
server.close();
|
|
216
|
+
process.exit(0);
|
|
217
|
+
};
|
|
218
|
+
process.on("SIGINT", shutdown);
|
|
219
|
+
process.on("SIGTERM", shutdown);
|
|
220
|
+
}
|
|
221
|
+
async function main() {
|
|
222
|
+
const { command, flags } = args(process.argv.slice(2));
|
|
223
|
+
const cwd = process.cwd();
|
|
224
|
+
switch (command) {
|
|
225
|
+
case "init":
|
|
226
|
+
await init(cwd);
|
|
227
|
+
break;
|
|
228
|
+
case "inject": {
|
|
229
|
+
const dir = flags.get("dir");
|
|
230
|
+
await inject(typeof dir === "string" ? dir : "dist", flags);
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
case "record": {
|
|
234
|
+
const rawPort = flags.get("port");
|
|
235
|
+
const port = typeof rawPort === "string" ? Number(rawPort) : 7317;
|
|
236
|
+
const target = process.argv.slice(3).find((value) => /^https?:\/\//.test(value)) ?? null;
|
|
237
|
+
await record(target, Number.isFinite(port) ? port : 7317);
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
case "version":
|
|
241
|
+
case "--version":
|
|
242
|
+
case "-v":
|
|
243
|
+
console.log(JSON.parse(await readFile(join(here, "..", "package.json"), "utf8")).version);
|
|
244
|
+
break;
|
|
245
|
+
default:
|
|
246
|
+
usage();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
void main();
|
|
250
|
+
export {
|
|
251
|
+
init,
|
|
252
|
+
inject,
|
|
253
|
+
main,
|
|
254
|
+
record
|
|
255
|
+
};
|