@magelight/vite-plugin 0.28.6

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Severause
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,22 @@
1
+ # @magelight/vite-plugin
2
+
3
+ ```ts
4
+ // vite.config.ts
5
+ import { defineConfig } from 'vite';
6
+ import react from '@vitejs/plugin-react';
7
+ import { magelight } from '@magelight/vite-plugin';
8
+
9
+ export default defineConfig({
10
+ plugins: [react(), magelight({ entries: { config: 'index.html', hud: 'hud.html' } })],
11
+ });
12
+ ```
13
+
14
+ `vite build` then writes `views/config/index.html` and `views/hud/index.html`, each with only the
15
+ assets that page reaches, ready to be named from `manifest.json` (`"path": "views/config/index.html"`).
16
+
17
+ What it sets for you: `base: './'` (file:// loading), es2022 output (Ultralight 1.4's WebKit
18
+ 615; es2019 was the 1.3-era rule), no `crossorigin` on module tags (a null origin refuses CORS
19
+ loads → blank page), no modulepreload polyfill, per-entry pruning (the rule SeverActions'
20
+ `build-ui.ps1` enforced by hand).
21
+
22
+ Options: `entries`, `viewsDir` (`views`), `single` (`index`), `outDir` (`dist`), `keepDist`.
@@ -0,0 +1,28 @@
1
+ import type { Plugin } from 'vite';
2
+ export interface MagelightPluginOptions {
3
+ /**
4
+ * Entry pages, view name → html file (relative to the project root).
5
+ * Each becomes its own pruned folder `<viewsDir>/<name>/index.html` holding
6
+ * only the assets that entry reaches. Omit for a single-entry project
7
+ * (Vite's own `index.html` → `<viewsDir>/<single>`, default `index`).
8
+ */
9
+ entries?: Record<string, string>;
10
+ /** where the per-view folders go, relative to the project root (default `views`) */
11
+ viewsDir?: string;
12
+ /** name for the single-entry case (default `index`) */
13
+ single?: string;
14
+ /** Vite's intermediate outDir (default `dist`) — pruned copies are made from it */
15
+ outDir?: string;
16
+ /** leave `dist` in place after the views are written (default false) */
17
+ keepDist?: boolean;
18
+ }
19
+ /**
20
+ * What every Magelight page needs from Vite, learned on SeverActions:
21
+ * - `base: './'` — pages load from file:///, absolute asset URLs break
22
+ * - `crossorigin` stripped from module tags — a null origin refuses CORS-mode loads (blank page)
23
+ * - es2022 output — Ultralight 1.4 (WebKit 615) evaluates it; es2019 was the 1.3-era rule
24
+ * - multi-entry builds emit ONE dist; each view folder must carry only its own closure
25
+ * (SeverActions' build-ui.ps1 did this by hand; here it is the plugin's job)
26
+ */
27
+ export declare function magelight(opts?: MagelightPluginOptions): Plugin;
28
+ export default magelight;
package/dist/index.js ADDED
@@ -0,0 +1,153 @@
1
+ import * as fs from 'node:fs';
2
+ import * as path from 'node:path';
3
+ /**
4
+ * What every Magelight page needs from Vite, learned on SeverActions:
5
+ * - `base: './'` — pages load from file:///, absolute asset URLs break
6
+ * - `crossorigin` stripped from module tags — a null origin refuses CORS-mode loads (blank page)
7
+ * - es2022 output — Ultralight 1.4 (WebKit 615) evaluates it; es2019 was the 1.3-era rule
8
+ * - multi-entry builds emit ONE dist; each view folder must carry only its own closure
9
+ * (SeverActions' build-ui.ps1 did this by hand; here it is the plugin's job)
10
+ */
11
+ export function magelight(opts = {}) {
12
+ var _a, _b;
13
+ const viewsDir = (_a = opts.viewsDir) !== null && _a !== void 0 ? _a : 'views';
14
+ const outDir = (_b = opts.outDir) !== null && _b !== void 0 ? _b : 'dist';
15
+ let root = process.cwd();
16
+ // chunk fileName → the files it needs (imports + css + assets), filled in generateBundle
17
+ const closure = new Map();
18
+ const entryFile = new Map(); // view name → html fileName in the bundle
19
+ return {
20
+ name: 'magelight',
21
+ config() {
22
+ const input = {};
23
+ if (opts.entries)
24
+ for (const [k, v] of Object.entries(opts.entries))
25
+ input[k] = path.resolve(root, v);
26
+ return {
27
+ base: './',
28
+ build: {
29
+ outDir,
30
+ assetsDir: 'assets',
31
+ target: 'es2022',
32
+ sourcemap: false,
33
+ chunkSizeWarningLimit: 4000,
34
+ modulePreload: { polyfill: false },
35
+ rollupOptions: opts.entries ? { input, output: { manualChunks: undefined } } : { output: { manualChunks: undefined } },
36
+ },
37
+ };
38
+ },
39
+ configResolved(cfg) {
40
+ root = cfg.root;
41
+ },
42
+ transformIndexHtml(html) {
43
+ return html.replace(/\scrossorigin(="[^"]*")?/g, '');
44
+ },
45
+ generateBundle(_, bundle) {
46
+ var _a, _b;
47
+ // Chunks and assets are here; the HTML entries are emitted by Vite's
48
+ // own html plugin AFTER this hook, so they are discovered on disk in
49
+ // closeBundle instead.
50
+ for (const [file, out] of Object.entries(bundle)) {
51
+ if (out.type !== 'chunk')
52
+ continue;
53
+ const deps = new Set();
54
+ for (const i of out.imports)
55
+ deps.add(i);
56
+ for (const i of out.dynamicImports)
57
+ deps.add(i);
58
+ const meta = out.viteMetadata;
59
+ (_a = meta === null || meta === void 0 ? void 0 : meta.importedCss) === null || _a === void 0 ? void 0 : _a.forEach((c) => deps.add(c));
60
+ (_b = meta === null || meta === void 0 ? void 0 : meta.importedAssets) === null || _b === void 0 ? void 0 : _b.forEach((a) => deps.add(a));
61
+ closure.set(file, deps);
62
+ }
63
+ },
64
+ closeBundle() {
65
+ var _a, _b;
66
+ const dist = path.resolve(root, outDir);
67
+ const views = path.resolve(root, viewsDir);
68
+ if (!fs.existsSync(dist))
69
+ return;
70
+ // Entry html files as Vite wrote them (relative to dist), mapped to view names.
71
+ const htmls = fs.readdirSync(dist, { recursive: true })
72
+ .filter((f) => f.endsWith('.html'))
73
+ .map((f) => f.split(path.sep).join('/'));
74
+ const norm = (s) => s.split(path.sep).join('/').replace(/^\.\//, '');
75
+ for (const html of htmls) {
76
+ let name;
77
+ if (opts.entries) {
78
+ const exact = Object.entries(opts.entries).filter(([, v]) => norm(v) === html);
79
+ if (exact.length) {
80
+ name = exact[0][0];
81
+ }
82
+ else {
83
+ // Vite flattens some inputs — fall back to the file name, but only
84
+ // when ONE entry carries it: pages/a/index.html + pages/b/index.html
85
+ // must not both claim the first match.
86
+ const byBase = Object.entries(opts.entries).filter(([, v]) => path.posix.basename(norm(v)) === path.posix.basename(html));
87
+ if (byBase.length === 1)
88
+ name = byBase[0][0];
89
+ else if (byBase.length > 1)
90
+ console.warn(`[magelight] ${html}: ambiguous — entries ${byBase.map(([k]) => k).join(', ')} share a file name; skipped`);
91
+ }
92
+ }
93
+ else {
94
+ if (htmls.length > 1 && html !== 'index.html') {
95
+ console.warn(`[magelight] ${html}: single-entry mode emits one view ('${(_a = opts.single) !== null && _a !== void 0 ? _a : 'index'}'); extra html skipped — use 'entries' for a multi-view project`);
96
+ continue;
97
+ }
98
+ name = (_b = opts.single) !== null && _b !== void 0 ? _b : 'index';
99
+ }
100
+ if (name)
101
+ entryFile.set(name, html);
102
+ }
103
+ let total = 0;
104
+ for (const [name, htmlFile] of entryFile) {
105
+ const src = fs.readFileSync(path.join(dist, htmlFile), 'utf8');
106
+ const htmlDir = path.posix.dirname(htmlFile);
107
+ const roots = new Set();
108
+ for (const m of src.matchAll(/(?:src|href)="\.\/([^"]+)"/g))
109
+ roots.add(path.posix.normalize(path.posix.join(htmlDir, m[1])));
110
+ const needed = new Set();
111
+ const walk = (f) => {
112
+ var _a;
113
+ if (needed.has(f))
114
+ return;
115
+ needed.add(f);
116
+ (_a = closure.get(f)) === null || _a === void 0 ? void 0 : _a.forEach(walk);
117
+ };
118
+ roots.forEach(walk);
119
+ const dst = path.join(views, name);
120
+ fs.rmSync(dst, { recursive: true, force: true });
121
+ fs.mkdirSync(dst, { recursive: true });
122
+ fs.writeFileSync(path.join(dst, 'index.html'), src.replace(/(src|href)="\.\/([^"]+)"/g, (_m, a, rel) => `${a}="./${path.posix.relative('', path.posix.normalize(path.posix.join(htmlDir, rel)))}"`));
123
+ for (const f of needed) {
124
+ const from = path.join(dist, f);
125
+ if (!fs.existsSync(from))
126
+ continue;
127
+ const to = path.join(dst, f);
128
+ fs.mkdirSync(path.dirname(to), { recursive: true });
129
+ fs.copyFileSync(from, to);
130
+ total++;
131
+ }
132
+ // public/ files (copied verbatim by Vite) are not in the graph — carry them all.
133
+ const pub = path.resolve(root, 'public');
134
+ if (fs.existsSync(pub)) {
135
+ for (const f of fs.readdirSync(pub, { recursive: true })) {
136
+ const from = path.join(pub, f);
137
+ if (fs.statSync(from).isFile()) {
138
+ const to = path.join(dst, f);
139
+ fs.mkdirSync(path.dirname(to), { recursive: true });
140
+ fs.copyFileSync(from, to);
141
+ }
142
+ }
143
+ }
144
+ console.log(`[magelight] view '${name}' -> ${path.relative(root, dst)} (${needed.size + 1} files)`);
145
+ }
146
+ if (!opts.keepDist)
147
+ fs.rmSync(dist, { recursive: true, force: true });
148
+ if (!total)
149
+ console.warn('[magelight] no entries were written — check `entries` names match the html files');
150
+ },
151
+ };
152
+ }
153
+ export default magelight;
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@magelight/vite-plugin",
3
+ "version": "0.28.6",
4
+ "description": "Vite plugin for Magelight UI pages: file:// safe output, per-entry pruned view folders, crossorigin strip.",
5
+ "license": "MIT",
6
+ "repository": { "type": "git", "url": "git+https://github.com/Severause/MagelightUI.git", "directory": "packages/vite-plugin" },
7
+ "publishConfig": { "access": "public" },
8
+ "type": "module",
9
+ "main": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" }
13
+ },
14
+ "files": ["dist", "README.md"],
15
+ "engines": { "node": ">=20" },
16
+ "scripts": {
17
+ "build": "tsc -p tsconfig.json",
18
+ "prepack": "npm run build"
19
+ },
20
+ "peerDependencies": {
21
+ "vite": ">=5"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^24.10.1",
25
+ "typescript": "~5.9.3",
26
+ "vite": "^7.3.1"
27
+ }
28
+ }