@decantr/vite-plugin 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/dist/index.d.ts +12 -0
- package/dist/index.js +136 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Decantr AI
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
export { EssenceFile, GuardContext, GuardViolation } from '@decantr/essence-spec';
|
|
3
|
+
|
|
4
|
+
interface DecantrPluginOptions {
|
|
5
|
+
/** Path to the essence file, relative to project root. Default: 'decantr.essence.json' */
|
|
6
|
+
essencePath?: string;
|
|
7
|
+
/** Debounce delay in ms before running guard after a file change. Default: 300 */
|
|
8
|
+
debounceMs?: number;
|
|
9
|
+
}
|
|
10
|
+
declare function decantrPlugin(options?: DecantrPluginOptions): Plugin;
|
|
11
|
+
|
|
12
|
+
export { type DecantrPluginOptions, decantrPlugin, decantrPlugin as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { evaluateGuard } from "@decantr/essence-spec";
|
|
4
|
+
|
|
5
|
+
// src/watcher.ts
|
|
6
|
+
import { readFileSync, existsSync } from "fs";
|
|
7
|
+
import { normalizeEssence } from "@decantr/essence-spec";
|
|
8
|
+
function loadEssence(filePath) {
|
|
9
|
+
if (!existsSync(filePath)) return null;
|
|
10
|
+
try {
|
|
11
|
+
const raw = JSON.parse(readFileSync(filePath, "utf-8"));
|
|
12
|
+
return normalizeEssence(raw);
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
var TRIGGER_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
18
|
+
".ts",
|
|
19
|
+
".tsx",
|
|
20
|
+
".js",
|
|
21
|
+
".jsx",
|
|
22
|
+
".vue",
|
|
23
|
+
".svelte",
|
|
24
|
+
".astro"
|
|
25
|
+
]);
|
|
26
|
+
var IGNORE_PATTERNS = [
|
|
27
|
+
"node_modules",
|
|
28
|
+
".decantr",
|
|
29
|
+
"decantr.essence.json"
|
|
30
|
+
];
|
|
31
|
+
function shouldTriggerGuard(filePath) {
|
|
32
|
+
if (IGNORE_PATTERNS.some((p) => filePath.includes(p))) return false;
|
|
33
|
+
const ext = filePath.slice(filePath.lastIndexOf("."));
|
|
34
|
+
return TRIGGER_EXTENSIONS.has(ext);
|
|
35
|
+
}
|
|
36
|
+
function createDebouncedGuard(callback, delayMs) {
|
|
37
|
+
let timer = null;
|
|
38
|
+
return () => {
|
|
39
|
+
if (timer) clearTimeout(timer);
|
|
40
|
+
timer = setTimeout(() => {
|
|
41
|
+
timer = null;
|
|
42
|
+
callback();
|
|
43
|
+
}, delayMs);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/overlay.ts
|
|
48
|
+
function formatViolation(v) {
|
|
49
|
+
const layerTag = v.layer ? `[${v.layer === "dna" ? "DNA" : "Blueprint"}] ` : "";
|
|
50
|
+
const fixHint = v.autoFixable ? " (auto-fixable via decantr_accept_drift)" : "";
|
|
51
|
+
return `${layerTag}[${v.rule}] ${v.message}${fixHint}`;
|
|
52
|
+
}
|
|
53
|
+
function formatViolations(violations) {
|
|
54
|
+
if (violations.length === 0) return null;
|
|
55
|
+
const errors = violations.filter((v) => v.severity === "error");
|
|
56
|
+
const warnings = violations.filter((v) => v.severity === "warning");
|
|
57
|
+
const sections = [];
|
|
58
|
+
if (errors.length > 0) {
|
|
59
|
+
sections.push("Errors:");
|
|
60
|
+
for (const v of errors) {
|
|
61
|
+
sections.push(` ${formatViolation(v)}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (warnings.length > 0) {
|
|
65
|
+
if (sections.length > 0) sections.push("");
|
|
66
|
+
sections.push("Warnings:");
|
|
67
|
+
for (const v of warnings) {
|
|
68
|
+
sections.push(` ${formatViolation(v)}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
id: "decantr-guard",
|
|
73
|
+
message: `Decantr: ${violations.length} guard violation${violations.length === 1 ? "" : "s"} detected`,
|
|
74
|
+
frame: sections.join("\n"),
|
|
75
|
+
plugin: "@decantr/vite-plugin"
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/index.ts
|
|
80
|
+
function decantrPlugin(options = {}) {
|
|
81
|
+
const essenceFileName = options.essencePath ?? "decantr.essence.json";
|
|
82
|
+
const debounceMs = options.debounceMs ?? 300;
|
|
83
|
+
let essence = null;
|
|
84
|
+
let root = "";
|
|
85
|
+
function runGuard(server) {
|
|
86
|
+
const essencePath = join(root, essenceFileName);
|
|
87
|
+
essence = loadEssence(essencePath);
|
|
88
|
+
if (!essence) {
|
|
89
|
+
server.hot.send({
|
|
90
|
+
type: "error",
|
|
91
|
+
err: { message: "", stack: "", plugin: "@decantr/vite-plugin" }
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const violations = evaluateGuard(essence, {});
|
|
96
|
+
const overlayError = formatViolations(violations);
|
|
97
|
+
if (overlayError) {
|
|
98
|
+
server.config.logger.warn(
|
|
99
|
+
`[decantr] ${violations.length} guard violation(s) detected`,
|
|
100
|
+
{ timestamp: true }
|
|
101
|
+
);
|
|
102
|
+
server.hot.send({
|
|
103
|
+
type: "error",
|
|
104
|
+
err: {
|
|
105
|
+
message: overlayError.message,
|
|
106
|
+
stack: overlayError.frame,
|
|
107
|
+
plugin: overlayError.plugin
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
name: "decantr-guard",
|
|
114
|
+
configureServer(server) {
|
|
115
|
+
root = server.config.root;
|
|
116
|
+
const debouncedGuard = createDebouncedGuard(() => runGuard(server), debounceMs);
|
|
117
|
+
server.httpServer?.once("listening", () => {
|
|
118
|
+
runGuard(server);
|
|
119
|
+
});
|
|
120
|
+
server.watcher.on("change", (filePath) => {
|
|
121
|
+
const relative = filePath.startsWith(root) ? filePath.slice(root.length + 1) : filePath;
|
|
122
|
+
if (relative === essenceFileName || relative.endsWith("decantr.essence.json")) {
|
|
123
|
+
runGuard(server);
|
|
124
|
+
} else if (shouldTriggerGuard(relative)) {
|
|
125
|
+
debouncedGuard();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
var index_default = decantrPlugin;
|
|
132
|
+
export {
|
|
133
|
+
decantrPlugin,
|
|
134
|
+
index_default as default
|
|
135
|
+
};
|
|
136
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/watcher.ts","../src/overlay.ts"],"sourcesContent":["import type { Plugin, ViteDevServer } from 'vite';\nimport { join } from 'node:path';\nimport { evaluateGuard } from '@decantr/essence-spec';\nimport type { EssenceFile } from '@decantr/essence-spec';\nimport { loadEssence, shouldTriggerGuard, createDebouncedGuard } from './watcher.js';\nimport { formatViolations } from './overlay.js';\n\nexport interface DecantrPluginOptions {\n /** Path to the essence file, relative to project root. Default: 'decantr.essence.json' */\n essencePath?: string;\n /** Debounce delay in ms before running guard after a file change. Default: 300 */\n debounceMs?: number;\n}\n\nexport function decantrPlugin(options: DecantrPluginOptions = {}): Plugin {\n const essenceFileName = options.essencePath ?? 'decantr.essence.json';\n const debounceMs = options.debounceMs ?? 300;\n\n let essence: EssenceFile | null = null;\n let root = '';\n\n function runGuard(server: ViteDevServer): void {\n const essencePath = join(root, essenceFileName);\n essence = loadEssence(essencePath);\n\n if (!essence) {\n server.hot.send({\n type: 'error',\n err: { message: '', stack: '', plugin: '@decantr/vite-plugin' },\n });\n return;\n }\n\n const violations = evaluateGuard(essence, {});\n const overlayError = formatViolations(violations);\n\n if (overlayError) {\n server.config.logger.warn(\n `[decantr] ${violations.length} guard violation(s) detected`,\n { timestamp: true },\n );\n server.hot.send({\n type: 'error',\n err: {\n message: overlayError.message,\n stack: overlayError.frame,\n plugin: overlayError.plugin,\n },\n });\n }\n }\n\n return {\n name: 'decantr-guard',\n\n configureServer(server) {\n root = server.config.root;\n\n const debouncedGuard = createDebouncedGuard(() => runGuard(server), debounceMs);\n\n server.httpServer?.once('listening', () => {\n runGuard(server);\n });\n\n server.watcher.on('change', (filePath: string) => {\n const relative = filePath.startsWith(root)\n ? filePath.slice(root.length + 1)\n : filePath;\n\n if (relative === essenceFileName || relative.endsWith('decantr.essence.json')) {\n runGuard(server);\n } else if (shouldTriggerGuard(relative)) {\n debouncedGuard();\n }\n });\n },\n };\n}\n\nexport default decantrPlugin;\nexport type { GuardViolation, GuardContext, EssenceFile } from '@decantr/essence-spec';\n","import { readFileSync, existsSync } from 'node:fs';\nimport { normalizeEssence } from '@decantr/essence-spec';\nimport type { EssenceFile } from '@decantr/essence-spec';\n\nexport function loadEssence(filePath: string): EssenceFile | null {\n if (!existsSync(filePath)) return null;\n\n try {\n const raw = JSON.parse(readFileSync(filePath, 'utf-8'));\n return normalizeEssence(raw);\n } catch {\n return null;\n }\n}\n\nconst TRIGGER_EXTENSIONS = new Set([\n '.ts', '.tsx', '.js', '.jsx', '.vue', '.svelte', '.astro',\n]);\n\nconst IGNORE_PATTERNS = [\n 'node_modules',\n '.decantr',\n 'decantr.essence.json',\n];\n\nexport function shouldTriggerGuard(filePath: string): boolean {\n if (IGNORE_PATTERNS.some(p => filePath.includes(p))) return false;\n\n const ext = filePath.slice(filePath.lastIndexOf('.'));\n return TRIGGER_EXTENSIONS.has(ext);\n}\n\nexport function createDebouncedGuard(\n callback: () => void,\n delayMs: number,\n): () => void {\n let timer: ReturnType<typeof setTimeout> | null = null;\n\n return () => {\n if (timer) clearTimeout(timer);\n timer = setTimeout(() => {\n timer = null;\n callback();\n }, delayMs);\n };\n}\n","import type { GuardViolation } from '@decantr/essence-spec';\n\nexport interface OverlayError {\n id: string;\n message: string;\n frame: string;\n plugin: string;\n}\n\nexport function formatViolation(v: GuardViolation): string {\n const layerTag = v.layer ? `[${v.layer === 'dna' ? 'DNA' : 'Blueprint'}] ` : '';\n const fixHint = v.autoFixable ? ' (auto-fixable via decantr_accept_drift)' : '';\n return `${layerTag}[${v.rule}] ${v.message}${fixHint}`;\n}\n\nexport function formatViolations(violations: GuardViolation[]): OverlayError | null {\n if (violations.length === 0) return null;\n\n const errors = violations.filter(v => v.severity === 'error');\n const warnings = violations.filter(v => v.severity === 'warning');\n\n const sections: string[] = [];\n\n if (errors.length > 0) {\n sections.push('Errors:');\n for (const v of errors) {\n sections.push(` ${formatViolation(v)}`);\n }\n }\n\n if (warnings.length > 0) {\n if (sections.length > 0) sections.push('');\n sections.push('Warnings:');\n for (const v of warnings) {\n sections.push(` ${formatViolation(v)}`);\n }\n }\n\n return {\n id: 'decantr-guard',\n message: `Decantr: ${violations.length} guard violation${violations.length === 1 ? '' : 's'} detected`,\n frame: sections.join('\\n'),\n plugin: '@decantr/vite-plugin',\n };\n}\n"],"mappings":";AACA,SAAS,YAAY;AACrB,SAAS,qBAAqB;;;ACF9B,SAAS,cAAc,kBAAkB;AACzC,SAAS,wBAAwB;AAG1B,SAAS,YAAY,UAAsC;AAChE,MAAI,CAAC,WAAW,QAAQ,EAAG,QAAO;AAElC,MAAI;AACF,UAAM,MAAM,KAAK,MAAM,aAAa,UAAU,OAAO,CAAC;AACtD,WAAO,iBAAiB,GAAG;AAAA,EAC7B,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,IAAM,qBAAqB,oBAAI,IAAI;AAAA,EACjC;AAAA,EAAO;AAAA,EAAQ;AAAA,EAAO;AAAA,EAAQ;AAAA,EAAQ;AAAA,EAAW;AACnD,CAAC;AAED,IAAM,kBAAkB;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AACF;AAEO,SAAS,mBAAmB,UAA2B;AAC5D,MAAI,gBAAgB,KAAK,OAAK,SAAS,SAAS,CAAC,CAAC,EAAG,QAAO;AAE5D,QAAM,MAAM,SAAS,MAAM,SAAS,YAAY,GAAG,CAAC;AACpD,SAAO,mBAAmB,IAAI,GAAG;AACnC;AAEO,SAAS,qBACd,UACA,SACY;AACZ,MAAI,QAA8C;AAElD,SAAO,MAAM;AACX,QAAI,MAAO,cAAa,KAAK;AAC7B,YAAQ,WAAW,MAAM;AACvB,cAAQ;AACR,eAAS;AAAA,IACX,GAAG,OAAO;AAAA,EACZ;A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IAAI,KAAK;AAAA,QACd,MAAM;AAAA,QACN,KAAK,EAAE,SAAS,IAAI,OAAO,IAAI,QAAQ,uBAAuB;AAAA,MAChE,CAAC;AACD;AAAA,IACF;AAEA,UAAM,aAAa,cAAc,SAAS,CAAC,CAAC;AAC5C,UAAM,eAAe,iBAAiB,UAAU;AAEhD,QAAI,cAAc;AAChB,aAAO,OAAO,OAAO;AAAA,QACnB,aAAa,WAAW,MAAM;AAAA,QAC9B,EAAE,WAAW,KAAK;AAAA,MACpB;AACA,aAAO,IAAI,KAAK;AAAA,QACd,MAAM;AAAA,QACN,KAAK;AAAA,UACH,SAAS,aAAa;AAAA,UACtB,OAAO,aAAa;AAAA,UACpB,QAAQ,aAAa;AAAA,QACvB;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IAEN,gBAAgB,QAAQ;AACtB,aAAO,OAAO,OAAO;AAErB,YAAM,iBAAiB,qBAAqB,MAAM,SAAS,MAAM,GAAG,UAAU;AAE9E,aAAO,YAAY,KAAK,aAAa,MAAM;AACzC,iBAAS,MAAM;AAAA,MACjB,CAAC;AAED,aAAO,QAAQ,GAAG,UAAU,CAAC,aAAqB;AAChD,cAAM,WAAW,SAAS,WAAW,IAAI,IACrC,SAAS,MAAM,KAAK,SAAS,CAAC,IAC9B;AAEJ,YAAI,aAAa,mBAAmB,SAAS,SAAS,sBAAsB,GAAG;AAC7E,mBAAS,MAAM;AAAA,QACjB,WAAW,mBAAmB,QAAQ,GAAG;AACvC,yBAAe;AAAA,QACjB;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEA,IAAO,gBAAQ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@decantr/vite-plugin",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vite plugin for real-time Decantr design drift detection",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/decantr-ai/decantr.git",
|
|
9
|
+
"directory": "packages/vite-plugin"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@decantr/essence-spec": "1.0.0-beta.6"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"vite": "^5.0.0 || ^6.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"vite": "^6.0.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"test:watch": "vitest"
|
|
39
|
+
}
|
|
40
|
+
}
|