@svelte-vitals/vite 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.md +21 -0
- package/README.md +31 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +197 -0
- package/package.json +52 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kazuma Oe
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @svelte-vitals/vite
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@svelte-vitals/vite)
|
|
4
|
+
|
|
5
|
+
Vite/SvelteKit plugin for [svelte-vitals](https://github.com/oekazuma/svelte-vitals). It piggybacks on `vite build`, parses the **prerendered HTML's `<head>`**, and runs the same SEO rules as the CLI — library-agnostic, because it inspects the real output. Fails the build when findings reach `failOn`.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// vite.config.ts
|
|
11
|
+
import { sveltekit } from '@sveltejs/kit/vite';
|
|
12
|
+
import { svelteVitals } from '@svelte-vitals/vite';
|
|
13
|
+
|
|
14
|
+
export default {
|
|
15
|
+
plugins: [sveltekit(), svelteVitals({ failOn: 'critical', report: 'console' })]
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Options
|
|
20
|
+
|
|
21
|
+
- `failOn` — minimum severity that fails the build (default `critical`).
|
|
22
|
+
- `report` — `'console' | 'json' | false` (default `console`).
|
|
23
|
+
- `outFile` — write the JSON report to a path.
|
|
24
|
+
- `rules` / `metaComponents` / `treatDynamicAs` — same as the CLI/core config.
|
|
25
|
+
- `prerenderDir` — override the prerendered-pages directory.
|
|
26
|
+
|
|
27
|
+
Only **prerendered** routes are analyzed; for SSR/dynamic routes use the `svelte-vitals` CLI.
|
|
28
|
+
|
|
29
|
+
## License
|
|
30
|
+
|
|
31
|
+
[MIT](https://github.com/oekazuma/svelte-vitals/blob/main/LICENSE.md) © [Kazuma Oe](https://github.com/oekazuma)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import { TreatDynamicAs, RuleSetting, Severity } from '@svelte-vitals/core';
|
|
3
|
+
|
|
4
|
+
interface SvelteVitalsOptions {
|
|
5
|
+
/** Project root (defaults to the Vite config root / cwd). */
|
|
6
|
+
cwd?: string;
|
|
7
|
+
treatDynamicAs?: TreatDynamicAs;
|
|
8
|
+
metaComponents?: string[];
|
|
9
|
+
rules?: Record<string, RuleSetting>;
|
|
10
|
+
/** Minimum severity that fails the build (default: 'critical'). */
|
|
11
|
+
failOn?: Severity;
|
|
12
|
+
/** Report output (default: 'console'). */
|
|
13
|
+
report?: 'console' | 'json' | false;
|
|
14
|
+
/** Write the JSON report to this path. */
|
|
15
|
+
outFile?: string;
|
|
16
|
+
/** Override the prerendered-pages directory (default: .svelte-kit/output/prerendered/pages). */
|
|
17
|
+
prerenderDir?: string;
|
|
18
|
+
}
|
|
19
|
+
/** svelte-vitals Vite/SvelteKit plugin. */
|
|
20
|
+
declare function svelteVitals(options?: SvelteVitalsOptions): Plugin;
|
|
21
|
+
|
|
22
|
+
export { type SvelteVitalsOptions, svelteVitals };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
import { writeFile, mkdir } from "fs/promises";
|
|
4
|
+
import { join as join3, isAbsolute, dirname } from "path";
|
|
5
|
+
|
|
6
|
+
// src/analyze.ts
|
|
7
|
+
import {
|
|
8
|
+
allRules,
|
|
9
|
+
selectRules,
|
|
10
|
+
applyRuleSeverities,
|
|
11
|
+
runRules,
|
|
12
|
+
computeScore,
|
|
13
|
+
summarize,
|
|
14
|
+
hasFailureAtOrAbove,
|
|
15
|
+
formatConsoleReport,
|
|
16
|
+
formatJsonReport,
|
|
17
|
+
defineConfig
|
|
18
|
+
} from "@svelte-vitals/core";
|
|
19
|
+
|
|
20
|
+
// src/providers/rendered/collect.ts
|
|
21
|
+
import { readFile } from "fs/promises";
|
|
22
|
+
import { join } from "path";
|
|
23
|
+
import { glob } from "tinyglobby";
|
|
24
|
+
|
|
25
|
+
// src/providers/rendered/parse-html.ts
|
|
26
|
+
import { parse } from "node-html-parser";
|
|
27
|
+
function attrValue(v) {
|
|
28
|
+
return v !== void 0 && v.trim().length > 0 ? "static" : "absent";
|
|
29
|
+
}
|
|
30
|
+
function parseHtmlHead(html) {
|
|
31
|
+
const root = parse(html);
|
|
32
|
+
const head = root.querySelector("head") ?? root;
|
|
33
|
+
const tags = [];
|
|
34
|
+
const title = head.querySelector("title");
|
|
35
|
+
if (title) tags.push({ kind: "title", presence: "own", value: attrValue(title.text) });
|
|
36
|
+
for (const meta of head.querySelectorAll("meta")) {
|
|
37
|
+
const name = meta.getAttribute("name");
|
|
38
|
+
const property = meta.getAttribute("property");
|
|
39
|
+
if (!name && !property) continue;
|
|
40
|
+
tags.push({
|
|
41
|
+
kind: "meta",
|
|
42
|
+
...name ? { name } : {},
|
|
43
|
+
...property ? { property } : {},
|
|
44
|
+
presence: "own",
|
|
45
|
+
value: attrValue(meta.getAttribute("content"))
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
for (const link of head.querySelectorAll("link")) {
|
|
49
|
+
const rel = link.getAttribute("rel");
|
|
50
|
+
if (!rel) continue;
|
|
51
|
+
tags.push({ kind: "link", rel, presence: "own", value: attrValue(link.getAttribute("href")) });
|
|
52
|
+
}
|
|
53
|
+
for (const script of head.querySelectorAll("script")) {
|
|
54
|
+
if (script.getAttribute("type") === "application/ld+json") {
|
|
55
|
+
tags.push({ kind: "jsonld", presence: "own", value: attrValue(script.text) });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const htmlEl = root.querySelector("html");
|
|
59
|
+
const lang = htmlEl?.getAttribute("lang");
|
|
60
|
+
const htmlLang = lang === void 0 || lang === null ? { presence: "none", value: "absent" } : { presence: "own", value: attrValue(lang) };
|
|
61
|
+
return { tags, htmlLang };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/providers/rendered/collect.ts
|
|
65
|
+
function deriveRouteFromHtmlPath(relPath) {
|
|
66
|
+
let p = relPath.replace(/\\/g, "/").replace(/\.html$/, "");
|
|
67
|
+
if (p === "index") return "/";
|
|
68
|
+
if (p.endsWith("/index")) p = p.slice(0, -"/index".length);
|
|
69
|
+
return "/" + p;
|
|
70
|
+
}
|
|
71
|
+
async function collectRenderedHeads(prerenderPagesDir) {
|
|
72
|
+
const files = (await glob("**/*.html", { cwd: prerenderPagesDir })).sort();
|
|
73
|
+
const parsedFiles = await Promise.all(
|
|
74
|
+
files.map(async (rel) => ({ rel, parsed: parseHtmlHead(await readFile(join(prerenderPagesDir, rel), "utf8")) }))
|
|
75
|
+
);
|
|
76
|
+
const heads = [];
|
|
77
|
+
let htmlLang = { presence: "none", value: "absent" };
|
|
78
|
+
for (const { rel, parsed } of parsedFiles) {
|
|
79
|
+
if (htmlLang.presence === "none" && parsed.htmlLang.presence === "own") htmlLang = parsed.htmlLang;
|
|
80
|
+
heads.push({
|
|
81
|
+
route: deriveRouteFromHtmlPath(rel),
|
|
82
|
+
source: "rendered",
|
|
83
|
+
tags: parsed.tags,
|
|
84
|
+
file: rel
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return { heads, htmlLang };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/providers/rendered/project.ts
|
|
91
|
+
import { access } from "fs/promises";
|
|
92
|
+
import { join as join2 } from "path";
|
|
93
|
+
import { ROBOTS_SOURCE_PATHS, SITEMAP_SOURCE_PATHS } from "@svelte-vitals/core";
|
|
94
|
+
async function exists(path) {
|
|
95
|
+
try {
|
|
96
|
+
await access(path);
|
|
97
|
+
return true;
|
|
98
|
+
} catch {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async function existsAny(cwd, paths) {
|
|
103
|
+
const found = await Promise.all(paths.map((p) => exists(join2(cwd, p))));
|
|
104
|
+
return found.some(Boolean);
|
|
105
|
+
}
|
|
106
|
+
async function collectRenderedProject(cwd, htmlLang) {
|
|
107
|
+
const [hasRobotsTxt, hasSitemap] = await Promise.all([
|
|
108
|
+
existsAny(cwd, ROBOTS_SOURCE_PATHS),
|
|
109
|
+
existsAny(cwd, SITEMAP_SOURCE_PATHS)
|
|
110
|
+
]);
|
|
111
|
+
return { hasRobotsTxt, hasSitemap, htmlLang };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/version.ts
|
|
115
|
+
import { readFileSync } from "fs";
|
|
116
|
+
function readPackageVersion() {
|
|
117
|
+
try {
|
|
118
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
119
|
+
return pkg.version ?? "0.0.0";
|
|
120
|
+
} catch {
|
|
121
|
+
return "0.0.0";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/analyze.ts
|
|
126
|
+
async function analyze(prerenderPagesDir, cwd, options) {
|
|
127
|
+
const config = defineConfig({
|
|
128
|
+
treatDynamicAs: options.treatDynamicAs ?? "pass",
|
|
129
|
+
metaComponents: options.metaComponents ?? [],
|
|
130
|
+
rules: options.rules ?? {},
|
|
131
|
+
failOn: options.failOn ?? "critical"
|
|
132
|
+
});
|
|
133
|
+
const { heads, htmlLang } = await collectRenderedHeads(prerenderPagesDir);
|
|
134
|
+
const project = await collectRenderedProject(cwd, htmlLang);
|
|
135
|
+
const results = applyRuleSeverities(
|
|
136
|
+
await runRules(selectRules(allRules, config), { heads, project, config }),
|
|
137
|
+
config
|
|
138
|
+
);
|
|
139
|
+
const { score } = computeScore(results, config);
|
|
140
|
+
const summary = summarize(results, config);
|
|
141
|
+
const failed = hasFailureAtOrAbove(summary, config.failOn);
|
|
142
|
+
const coverageNote = `Analyzed ${heads.length} prerendered route(s). SSR/dynamic routes are not covered \u2014 run \`npx svelte-vitals\` for those.`;
|
|
143
|
+
const consoleReport = formatConsoleReport(results, config, { mode: "rendered / plugin" }) + "\n" + coverageNote + "\n";
|
|
144
|
+
const jsonReport = formatJsonReport(results, config, { version: readPackageVersion() });
|
|
145
|
+
return {
|
|
146
|
+
score,
|
|
147
|
+
summary,
|
|
148
|
+
results,
|
|
149
|
+
consoleReport,
|
|
150
|
+
jsonReport,
|
|
151
|
+
routeCount: heads.length,
|
|
152
|
+
failed,
|
|
153
|
+
failOn: config.failOn
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/plugin.ts
|
|
158
|
+
var DEFAULT_PRERENDER_DIR = ".svelte-kit/output/prerendered/pages";
|
|
159
|
+
function svelteVitals(options = {}) {
|
|
160
|
+
let root = options.cwd ?? process.cwd();
|
|
161
|
+
return {
|
|
162
|
+
name: "svelte-vitals",
|
|
163
|
+
apply: "build",
|
|
164
|
+
enforce: "post",
|
|
165
|
+
configResolved(config) {
|
|
166
|
+
if (!options.cwd) root = config.root;
|
|
167
|
+
},
|
|
168
|
+
async closeBundle() {
|
|
169
|
+
const pagesDir = options.prerenderDir ? options.prerenderDir : join3(root, DEFAULT_PRERENDER_DIR);
|
|
170
|
+
const resolved = isAbsolute(pagesDir) ? pagesDir : join3(root, pagesDir);
|
|
171
|
+
if (!existsSync(resolved)) return;
|
|
172
|
+
let result;
|
|
173
|
+
try {
|
|
174
|
+
result = await analyze(resolved, root, options);
|
|
175
|
+
} catch (err) {
|
|
176
|
+
console.warn(`svelte-vitals: skipped \u2014 analysis failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (result.routeCount === 0) return;
|
|
180
|
+
if (options.report !== false) {
|
|
181
|
+
const out = options.report === "json" ? result.jsonReport : result.consoleReport;
|
|
182
|
+
console.log(out);
|
|
183
|
+
}
|
|
184
|
+
if (options.outFile) {
|
|
185
|
+
const outPath = isAbsolute(options.outFile) ? options.outFile : join3(root, options.outFile);
|
|
186
|
+
await mkdir(dirname(outPath), { recursive: true });
|
|
187
|
+
await writeFile(outPath, result.jsonReport);
|
|
188
|
+
}
|
|
189
|
+
if (result.failed) {
|
|
190
|
+
throw new Error(`svelte-vitals: build failed \u2014 findings at or above "${result.failOn}".`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
export {
|
|
196
|
+
svelteVitals
|
|
197
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@svelte-vitals/vite",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vite/SvelteKit plugin for svelte-vitals — analyzes prerendered HTML during vite build.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Kazuma Oe (https://github.com/oekazuma)",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"svelte",
|
|
10
|
+
"sveltekit",
|
|
11
|
+
"seo",
|
|
12
|
+
"vite-plugin",
|
|
13
|
+
"svelte-vitals"
|
|
14
|
+
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/oekazuma/svelte-vitals.git",
|
|
18
|
+
"directory": "packages/vite"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/oekazuma/svelte-vitals/issues"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/oekazuma/svelte-vitals#readme",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"node-html-parser": "^6.1.13",
|
|
37
|
+
"tinyglobby": "^0.2.17",
|
|
38
|
+
"@svelte-vitals/core": "0.3.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"vite": "^8.0.16"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^24.7.0",
|
|
45
|
+
"vite": "^8.0.16"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsup",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"test": "vitest run"
|
|
51
|
+
}
|
|
52
|
+
}
|