vite-basepath 1.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+
12
+ - TypeScript source, `tsup` build, published `.d.ts` types
13
+ - Runtime helpers: `getBase`, `getAbsoluteBase`, `resolveUrl`
14
+ - CLI wrapper (`vite-basepath build` / `preview`)
15
+ - Verification scripts (`npm run verify`, `verify:build`)
16
+
17
+ ### Changed
18
+
19
+ - Relative-only model: build always uses `base: './'` (no fixed `/demo/` config)
20
+
21
+ ## [1.0.0] - 2026-06-04
22
+
23
+ ### Added
24
+
25
+ - Initial release: Vite plugin for subdirectory-safe asset paths
26
+
27
+ [Unreleased]: https://github.com/Foisalislambd/vite-basepath/compare/v1.0.0...HEAD
28
+ [1.0.0]: https://github.com/Foisalislambd/vite-basepath/releases/tag/v1.0.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Foisalislambd
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,321 @@
1
+ # vite-basepath
2
+
3
+ [![npm version](https://img.shields.io/npm/v/vite-basepath.svg)](https://www.npmjs.com/package/vite-basepath)
4
+ [![license](https://img.shields.io/npm/l/vite-basepath.svg)](https://github.com/Foisalislambd/vite-basepath/blob/main/LICENSE)
5
+
6
+ > Vite plugin that automatically fixes asset paths so your app works when deployed to **any subdirectory or domain path** — without changing your `vite.config.js` every time.
7
+
8
+ **Repository:** [github.com/Foisalislambd/vite-basepath](https://github.com/Foisalislambd/vite-basepath)
9
+
10
+ ---
11
+
12
+ ## The Problem
13
+
14
+ By default, Vite builds your app assuming it will be hosted at the **root** of a domain (`/`).
15
+
16
+ ```
17
+ ✅ Works: https://website.com/
18
+ ❌ Breaks: https://website.com/demo/template/
19
+ ❌ Breaks: https://website.com/projects/my-app/
20
+ ```
21
+
22
+ Assets fail to load because their paths start with `/`, which points to the wrong location.
23
+
24
+ ## The Solution
25
+
26
+ This plugin sets Vite's `base` to `./` (relative paths) by default. Relative paths work **anywhere** — root or subfolder — without any configuration.
27
+
28
+ ```
29
+ ✅ Works: https://website.com/
30
+ ✅ Works: https://website.com/demo/template/
31
+ ✅ Works: https://website.com/projects/my-app/
32
+ ✅ Works: https://any-domain.com/any/nested/path/
33
+ ```
34
+
35
+ ---
36
+
37
+ ## Setup (2 steps only)
38
+
39
+ ### Step 1 — Install
40
+
41
+ ```bash
42
+ npm install vite-basepath --save-dev
43
+ ```
44
+
45
+ ### Step 2 — Add to `vite.config.js`
46
+
47
+ ```js
48
+ import { defineConfig } from 'vite';
49
+ import react from '@vitejs/plugin-react'; // তোমার existing framework plugin
50
+ import viteBasepath from 'vite-basepath'; // ← এই import টা যোগ করো
51
+
52
+ export default defineConfig({
53
+ plugins: [
54
+ react(),
55
+ viteBasepath(), // ← এই line টা যোগ করো
56
+ ],
57
+ });
58
+ ```
59
+
60
+ > **ব্যস।** এখন `npm run build` করো — build যেকোনো folder-এ কাজ করবে।
61
+
62
+ ---
63
+
64
+ ### Router ব্যবহার করলে? (React Router / Vue Router)
65
+
66
+ একটা extra step লাগবে — router-কে বলতে হবে app কোথায় আছে।
67
+
68
+ **React Router v6:**
69
+
70
+ ```jsx
71
+ // src/main.jsx
72
+ import { BrowserRouter } from 'react-router-dom';
73
+ import { getBase } from 'vite-basepath/runtime'; // ← এটা import করো
74
+
75
+ root.render(
76
+ <BrowserRouter basename={getBase()}>
77
+ {' '}
78
+ {/* ← basename যোগ করো */}
79
+ <App />
80
+ </BrowserRouter>,
81
+ );
82
+ ```
83
+
84
+ **Vue Router:**
85
+
86
+ ```js
87
+ // src/router/index.js
88
+ import { createWebHistory, createRouter } from 'vue-router';
89
+ import { getBase } from 'vite-basepath/runtime'; // ← এটা import করো
90
+
91
+ const router = createRouter({
92
+ history: createWebHistory(getBase()), // ← getBase() দিয়ে wrap করো
93
+ routes: [...],
94
+ });
95
+ ```
96
+
97
+ > Router না থাকলে (plain Vite, no BrowserRouter) এই step দরকার নেই।
98
+
99
+ ---
100
+
101
+ ## TypeScript
102
+
103
+ Types ship with the package (`ViteBasepathOptions`, `getBase()`, etc.). No `@types` package needed.
104
+
105
+ ```ts
106
+ import { defineConfig } from 'vite';
107
+ import viteBasepath, { type ViteBasepathOptions } from 'vite-basepath';
108
+ import { getBase } from 'vite-basepath/runtime';
109
+ ```
110
+
111
+ ---
112
+
113
+ ## Installation
114
+
115
+ ```bash
116
+ npm install vite-basepath --save-dev
117
+ # or
118
+ yarn add vite-basepath --dev
119
+ # or
120
+ pnpm add vite-basepath --save-dev
121
+ ```
122
+
123
+ ---
124
+
125
+ ## Quick Start
126
+
127
+ **1. Add the plugin to `vite.config.js`:**
128
+
129
+ ```js
130
+ // vite.config.js
131
+ import { defineConfig } from 'vite';
132
+ import viteBasepath from 'vite-basepath';
133
+
134
+ export default defineConfig({
135
+ plugins: [
136
+ viteBasepath(), // That's it! ✅
137
+ ],
138
+ });
139
+ ```
140
+
141
+ **2. Build normally:**
142
+
143
+ ```bash
144
+ npx vite build
145
+ ```
146
+
147
+ Your built project will now work in **any folder** on any server.
148
+
149
+ ---
150
+
151
+ ## Usage with Routers (React / Vue)
152
+
153
+ When using client-side routing, your router needs to know the base path.
154
+ Import the `getBase()` helper from the runtime module:
155
+
156
+ ### React Router v6
157
+
158
+ ```jsx
159
+ // src/main.jsx
160
+ import { BrowserRouter } from 'react-router-dom';
161
+ import { getBase } from 'vite-basepath/runtime';
162
+
163
+ ReactDOM.createRoot(document.getElementById('root')).render(
164
+ <BrowserRouter basename={getBase()}>
165
+ <App />
166
+ </BrowserRouter>,
167
+ );
168
+ ```
169
+
170
+ ### Vue Router
171
+
172
+ ```js
173
+ // src/router/index.js
174
+ import { createRouter, createWebHistory } from 'vue-router';
175
+ import { getBase } from 'vite-basepath/runtime';
176
+
177
+ const router = createRouter({
178
+ history: createWebHistory(getBase()),
179
+ routes: [...]
180
+ });
181
+ ```
182
+
183
+ ### React Router v5
184
+
185
+ ```jsx
186
+ import { BrowserRouter } from 'react-router-dom';
187
+ import { getBase } from 'vite-basepath/runtime';
188
+
189
+ <BrowserRouter basename={getBase()}>
190
+ <App />
191
+ </BrowserRouter>;
192
+ ```
193
+
194
+ ---
195
+
196
+ ## Runtime Helpers
197
+
198
+ Import from `vite-basepath/runtime`:
199
+
200
+ ```js
201
+ import { getBase, getAbsoluteBase, resolveUrl } from 'vite-basepath/runtime';
202
+
203
+ // Detected deploy path (from ./ build + where assets load)
204
+ getBase(); // e.g. "/demo/template/"
205
+
206
+ // Get the full absolute URL
207
+ getAbsoluteBase(); // e.g. "https://website.com/demo/template/"
208
+
209
+ // Build a URL relative to the base
210
+ resolveUrl('api/users'); // e.g. "/demo/template/api/users"
211
+ ```
212
+
213
+ ---
214
+
215
+ ## CLI Reference
216
+
217
+ ```
218
+ vite-basepath <command> [options]
219
+
220
+ Commands:
221
+ build Build for production (default)
222
+ preview Preview the production build
223
+
224
+ Options:
225
+ --outDir, -o <dir> Output directory (default: dist)
226
+ --config, -c <file> Vite config file
227
+ --mode, -m <mode> Vite mode (production/staging/etc.)
228
+ --help, -h Show help
229
+
230
+ (Build always uses base ./ — no --base flag needed.)
231
+ ```
232
+
233
+ ---
234
+
235
+ ## Plugin Options
236
+
237
+ ```js
238
+ viteBasepath({
239
+ // Inject runtime script so getBase() can detect deploy path. Default: true
240
+ injectRuntime: true,
241
+
242
+ // Print build info in the terminal. Default: true
243
+ verbose: true,
244
+ });
245
+ ```
246
+
247
+ ---
248
+
249
+ ## How It Works
250
+
251
+ 1. **Build time:** The plugin sets Vite's `base` option to `./` (relative paths).
252
+ This makes all asset `<script src>` and `<link href>` tags use relative URLs — so they work no matter what folder the `index.html` is in.
253
+
254
+ 2. **HTML injection:** The plugin injects a tiny `<script>` and `<meta>` tag into `index.html` during build. The script detects the real base path at runtime by looking at where `/assets/` files are loaded from.
255
+
256
+ 3. **Runtime:** `getBase()` reads the detected base and returns the correct path for routers.
257
+
258
+ ---
259
+
260
+ ## Examples
261
+
262
+ ### Deploy same build to multiple paths
263
+
264
+ ```bash
265
+ # Build once
266
+ npx vite build
267
+
268
+ # Copy dist/ to multiple locations — all will work!
269
+ cp -r dist/ /var/www/html/
270
+ cp -r dist/ /var/www/html/demo/
271
+ cp -r dist/ /var/www/html/projects/my-app/
272
+ ```
273
+
274
+ ### Nginx subfolder (same ./ build)
275
+
276
+ ```nginx
277
+ location /demo/template/ {
278
+ alias /var/www/my-app/dist/;
279
+ try_files $uri $uri/ /demo/template/index.html;
280
+ }
281
+ ```
282
+
283
+ Copy `dist/` into that folder — no separate build per path.
284
+
285
+ ---
286
+
287
+ ## Compatibility
288
+
289
+ | Vite version | Supported |
290
+ | ------------ | --------- |
291
+ | Vite 3.x | ✅ |
292
+ | Vite 4.x | ✅ |
293
+ | Vite 5.x | ✅ |
294
+ | Vite 6.x | ✅ |
295
+ | Vite 7.x | ✅ |
296
+ | Vite 8.x | ✅ |
297
+
298
+ Works with all Vite-based frameworks:
299
+
300
+ - ⚛️ React (Create React App via Vite, Vite + React)
301
+ - 💚 Vue 3 / Vue 2
302
+ - 🟠 Svelte
303
+ - 🔷 SolidJS
304
+ - 🔶 Qwik
305
+ - ⚡ Astro (partial support)
306
+
307
+ ---
308
+
309
+ ## Project standards
310
+
311
+ | Document | Purpose |
312
+ | ------------------------------------------------ | -------------------------------------- |
313
+ | [Code standards](docs/CODE_STANDARDS.md) | TypeScript, layout, logic invariants |
314
+ | [Business standards](docs/BUSINESS_STANDARDS.md) | npm publish, SemVer, release checklist |
315
+ | [Contributing](CONTRIBUTING.md) | PR workflow |
316
+ | [Security](SECURITY.md) | Vulnerability reporting |
317
+ | [Changelog](CHANGELOG.md) | Version history |
318
+
319
+ ## License
320
+
321
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import '../dist/cli.js';
package/dist/cli.js ADDED
@@ -0,0 +1,76 @@
1
+ import { execSync } from 'child_process';
2
+
3
+ // bin/cli.ts
4
+ var args = process.argv.slice(2);
5
+ function getFlag(name, short) {
6
+ const longIdx = args.indexOf(`--${name}`);
7
+ const shortIdx = short ? args.indexOf(`-${short}`) : -1;
8
+ const idx = longIdx !== -1 ? longIdx : shortIdx;
9
+ if (idx === -1) return null;
10
+ return args[idx + 1] ?? null;
11
+ }
12
+ function hasFlag(name, short) {
13
+ return args.includes(`--${name}`) || (args.includes(`-${short}`) );
14
+ }
15
+ var firstArg = args[0];
16
+ var command = firstArg && !firstArg.startsWith("-") ? firstArg : "build";
17
+ var outDir = getFlag("outDir", "o");
18
+ var configFile = getFlag("config", "c");
19
+ var showHelp = hasFlag("help", "h");
20
+ var mode = getFlag("mode", "m");
21
+ if (showHelp) {
22
+ console.log(`
23
+ vite-basepath \u2014 build with relative base (./)
24
+
25
+ USAGE
26
+ vite-basepath <command> [options]
27
+
28
+ COMMANDS
29
+ build Build for production (default)
30
+ preview Preview the production build
31
+
32
+ OPTIONS
33
+ --outDir, -o <dir> Output directory (default: dist)
34
+ --config, -c <file> Vite config file
35
+ --mode, -m <mode> Vite mode
36
+ --help, -h Show this help
37
+
38
+ EXAMPLES
39
+ vite-basepath build
40
+ vite-basepath build --outDir public
41
+ vite-basepath preview
42
+ `);
43
+ process.exit(0);
44
+ }
45
+ var ALLOWED = ["build", "preview"];
46
+ if (!ALLOWED.includes(command)) {
47
+ console.error(` \u2717 Unknown command: "${command}"`);
48
+ console.error(` Allowed: ${ALLOWED.join(", ")}
49
+ `);
50
+ process.exit(1);
51
+ }
52
+ var parts = ["npx", "vite", command];
53
+ if (outDir) parts.push("--outDir", outDir);
54
+ if (configFile) parts.push("--config", configFile);
55
+ if (mode) parts.push("--mode", mode);
56
+ var cmd = parts.join(" ");
57
+ var successLabel = command === "preview" ? "Preview ready" : "Build complete";
58
+ console.log(`
59
+ vite-basepath \u2192 vite ${command} (base ./)
60
+ `);
61
+ try {
62
+ execSync(cmd, {
63
+ stdio: "inherit",
64
+ shell: process.platform === "win32" ? process.env.ComSpec ?? "cmd.exe" : "/bin/sh"
65
+ });
66
+ console.log(`
67
+ \u2705 ${successLabel}!
68
+ `);
69
+ } catch {
70
+ console.error(`
71
+ \u2717 ${command} failed.
72
+ `);
73
+ process.exit(1);
74
+ }
75
+ //# sourceMappingURL=cli.js.map
76
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../bin/cli.ts"],"names":[],"mappings":";;;AAEA,IAAM,IAAA,GAAO,OAAA,CAAQ,IAAA,CAAK,KAAA,CAAM,CAAC,CAAA;AAEjC,SAAS,OAAA,CAAQ,MAAc,KAAA,EAA+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AAA;AAC1C,EAAA,OAAA,CAAQ,KAAK,CAAC,CAAA;AAChB","file":"cli.js","sourcesContent":["import { execSync } from 'node:child_process';\n\nconst args = process.argv.slice(2);\n\nfunction getFlag(name: string, short?: string): string | null {\n const longIdx = args.indexOf(`--${name}`);\n const shortIdx = short ? args.indexOf(`-${short}`) : -1;\n const idx = longIdx !== -1 ? longIdx : shortIdx;\n if (idx === -1) return null;\n return args[idx + 1] ?? null;\n}\n\nfunction hasFlag(name: string, short?: string): boolean {\n return args.includes(`--${name}`) || (short ? args.includes(`-${short}`) : false);\n}\n\nconst firstArg = args[0];\nconst command = firstArg && !firstArg.startsWith('-') ? firstArg : 'build';\nconst outDir = getFlag('outDir', 'o');\nconst configFile = getFlag('config', 'c');\nconst showHelp = hasFlag('help', 'h');\nconst mode = getFlag('mode', 'm');\n\nif (showHelp) {\n console.log(`\n vite-basepath — build with relative base (./)\n\n USAGE\n vite-basepath <command> [options]\n\n COMMANDS\n build Build for production (default)\n preview Preview the production build\n\n OPTIONS\n --outDir, -o <dir> Output directory (default: dist)\n --config, -c <file> Vite config file\n --mode, -m <mode> Vite mode\n --help, -h Show this help\n\n EXAMPLES\n vite-basepath build\n vite-basepath build --outDir public\n vite-basepath preview\n`);\n process.exit(0);\n}\n\nconst ALLOWED = ['build', 'preview'] as const;\nif (!ALLOWED.includes(command as (typeof ALLOWED)[number])) {\n console.error(` ✗ Unknown command: \"${command}\"`);\n console.error(` Allowed: ${ALLOWED.join(', ')}\\n`);\n process.exit(1);\n}\n\nconst parts = ['npx', 'vite', command];\nif (outDir) parts.push('--outDir', outDir);\nif (configFile) parts.push('--config', configFile);\nif (mode) parts.push('--mode', mode);\n\nconst cmd = parts.join(' ');\nconst successLabel = command === 'preview' ? 'Preview ready' : 'Build complete';\n\nconsole.log(`\\n vite-basepath → vite ${command} (base ./)\\n`);\n\ntry {\n execSync(cmd, {\n stdio: 'inherit',\n shell: process.platform === 'win32' ? (process.env.ComSpec ?? 'cmd.exe') : '/bin/sh',\n });\n console.log(`\\n ✅ ${successLabel}!\\n`);\n} catch {\n console.error(`\\n ✗ ${command} failed.\\n`);\n process.exit(1);\n}\n"]}
@@ -0,0 +1,15 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface ViteBasepathOptions {
4
+ /** Inject runtime script for `getBase()` detection. @default true */
5
+ injectRuntime?: boolean;
6
+ /** Log build info to the terminal. @default true */
7
+ verbose?: boolean;
8
+ }
9
+
10
+ /**
11
+ * Vite plugin: build with `base: './'` and optional runtime base detection for routers.
12
+ */
13
+ declare function viteBasepath(options?: ViteBasepathOptions): Plugin;
14
+
15
+ export { type ViteBasepathOptions, viteBasepath as default };
package/dist/index.js ADDED
@@ -0,0 +1,106 @@
1
+ // src/shared.ts
2
+ var PLUGIN_NAME = "vite-basepath";
3
+ var META_NAME = "vite-app-base";
4
+ var META_ASSET_MARKER = "vite-app-asset-marker";
5
+ var WINDOW_VAR = "__VITE_BASE__";
6
+ var RELATIVE_BASE = "./";
7
+ var DEFAULT_ASSET_DIR = "assets";
8
+ function assetPathMarker(assetDir = DEFAULT_ASSET_DIR) {
9
+ const dir = assetDir.replace(/^\/+|\/+$/g, "");
10
+ return `/${dir}/`;
11
+ }
12
+
13
+ // src/inline-bootstrap.ts
14
+ function buildRuntimeInlineScript(assetMarker) {
15
+ return `(function () {
16
+ var marker = ${JSON.stringify(assetMarker)};
17
+ var winKey = ${JSON.stringify(WINDOW_VAR)};
18
+ function trail(p) {
19
+ if (!p) return '/';
20
+ return p.charAt(p.length - 1) === '/' ? p : p + '/';
21
+ }
22
+ function detect() {
23
+ var nodes = document.querySelectorAll('script[src], link[href]');
24
+ var origin = window.location.origin;
25
+ for (var i = 0; i < nodes.length; i++) {
26
+ var url = nodes[i].src || nodes[i].href || '';
27
+ var idx = url.indexOf(marker);
28
+ if (idx !== -1) {
29
+ return trail(url.substring(0, idx + 1).replace(origin, '') || '/');
30
+ }
31
+ }
32
+ return null;
33
+ }
34
+ function apply() {
35
+ var detected = detect();
36
+ if (detected) window[winKey] = detected;
37
+ }
38
+ apply();
39
+ if (!window[winKey]) {
40
+ if (document.readyState === 'loading') {
41
+ document.addEventListener('DOMContentLoaded', apply, { once: true });
42
+ } else {
43
+ apply();
44
+ }
45
+ }
46
+ })();`;
47
+ }
48
+
49
+ // src/index.ts
50
+ function viteBasepath(options = {}) {
51
+ const { injectRuntime = true, verbose = true } = options;
52
+ let assetMarker = assetPathMarker();
53
+ return {
54
+ name: PLUGIN_NAME,
55
+ apply: "build",
56
+ enforce: "pre",
57
+ config(_userConfig, { command }) {
58
+ if (command !== "build") return;
59
+ if (verbose) {
60
+ console.log(`
61
+ \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557`);
62
+ console.log(` \u2551 vite-basepath \u2551`);
63
+ console.log(` \u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563`);
64
+ console.log(` \u2551 Base path : "${RELATIVE_BASE}"`);
65
+ console.log(` \u2551 Mode : Relative (works anywhere)`);
66
+ console.log(` \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D
67
+ `);
68
+ }
69
+ return { base: RELATIVE_BASE };
70
+ },
71
+ configResolved(config) {
72
+ assetMarker = assetPathMarker(config.build.assetsDir);
73
+ },
74
+ transformIndexHtml: {
75
+ order: "post",
76
+ handler(html, ctx) {
77
+ if (!ctx.bundle) return html;
78
+ const tags = [
79
+ {
80
+ tag: "meta",
81
+ attrs: { name: META_NAME, content: RELATIVE_BASE },
82
+ injectTo: "head"
83
+ },
84
+ {
85
+ tag: "meta",
86
+ attrs: { name: META_ASSET_MARKER, content: assetMarker },
87
+ injectTo: "head"
88
+ }
89
+ ];
90
+ if (injectRuntime) {
91
+ tags.push({
92
+ tag: "script",
93
+ attrs: { "data-vite-basepath": "" },
94
+ children: buildRuntimeInlineScript(assetMarker),
95
+ injectTo: "head"
96
+ });
97
+ }
98
+ return { html, tags };
99
+ }
100
+ }
101
+ };
102
+ }
103
+
104
+ export { viteBasepath as default };
105
+ //# sourceMappingURL=index.js.map
106
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/shared.ts","../src/inline-bootstrap.ts","../src/index.ts"],"names":[],"mappings":";AAAO,IAAM,WAAA,GAAc,eAAA;AACpB,IAAM,SAAA,GAAY,eAAA;AAClB,IAAM,iBAAA,GAAoB,uBAAA;AAC1B,IAAM,UAAA,GAAa,eAAA;AACnB,IAAM,aAAA,GAAgB,IAAA;AACtB,IAAM,iBAAA,GAAoB,QAAA;AAE1B,SAAS,eAAA,CAAgB,WAAmB,iBAAA,EAA2B;AAC5E,EAAA,MAAM,GAAA,GAAM,QAAA,CAAS,OAAA,CAAQ,YAAA,EAAc,EAAE,CAAA;AAC7C,EAAA,OAAO,IAAI,GAAG,CAAA,CAAA,CAAA;AAChB;;;ACLO,SAAS,yBAAyB,WAAA,EAA6B;AACpE,EAAA,OAAO,CAAA;AAAA,eAAA,EACQ,IAAA,CAAK,SAAA,CAAU,WAAW,CAAC,CAAA;AAAA,eAAA,EAC3B,IAAA,CAAK,SAAA,CAAU,UAAU,CAAC,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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,GAAA,CAAI,MAAA,EAAQ,OAAO,IAAA;AAExB,QAAA,MAAM,IAAA,GAA4B;AAAA,UAChC;AAAA,YACE,GAAA,EAAK,MAAA;AAAA,YACL,KAAA,EAAO,EAAE,IAAA,EAAM,SAAA,EAAW,SAAS,aAAA,EAAc;AAAA,YACjD,QAAA,EAAU;AAAA,WACZ;AAAA,UACA;AAAA,YACE,GAAA,EAAK,MAAA;AAAA,YACL,KAAA,EAAO,EAAE,IAAA,EAAM,iBAAA,EAAmB,SAAS,WAAA,EAAY;AAAA,YACvD,QAAA,EAAU;AAAA;AACZ,SACF;AAEA,QAAA,IAAI,aAAA,EAAe;AACjB,UAAA,IAAA,CAAK,IAAA,CAAK;AAAA,YACR,GAAA,EAAK,QAAA;AAAA,YACL,KAAA,EAAO,EAAE,oBAAA,EAAsB,EAAA,EAAG;AAAA,YAClC,QAAA,EAAU,yBAAyB,WAAW,CAAA;AAAA,YAC9C,QAAA,EAAU;AAAA,WACX,CAAA;AAAA,QACH;AAEA,QAAA,OAAO,EAAE,MAAM,IAAA,EAAK;AAAA,MACtB;AAAA;AACF,GACF;AACF","file":"index.js","sourcesContent":["export const PLUGIN_NAME = 'vite-basepath';\nexport const META_NAME = 'vite-app-base';\nexport const META_ASSET_MARKER = 'vite-app-asset-marker';\nexport const WINDOW_VAR = '__VITE_BASE__';\nexport const RELATIVE_BASE = './';\nexport const DEFAULT_ASSET_DIR = 'assets';\n\nexport function assetPathMarker(assetDir: string = DEFAULT_ASSET_DIR): string {\n const dir = assetDir.replace(/^\\/+|\\/+$/g, '');\n return `/${dir}/`;\n}\n\nexport function ensureTrailingSlash(path: string): string {\n if (!path) return '/';\n return path.endsWith('/') ? path : `${path}/`;\n}\n\nexport function detectBaseFromAssetUrl(\n url: string,\n origin: string,\n marker: string,\n): string | null {\n const idx = url.indexOf(marker);\n if (idx === -1) return null;\n const raw = url.substring(0, idx + 1).replace(origin, '');\n return ensureTrailingSlash(raw) || '/';\n}\n","import { WINDOW_VAR } from './shared.js';\n\n/**\n * Minified IIFE injected into built HTML. Must stay aligned with {@link detectBaseFromAssetUrl}.\n */\nexport function buildRuntimeInlineScript(assetMarker: string): string {\n return `(function () {\n var marker = ${JSON.stringify(assetMarker)};\n var winKey = ${JSON.stringify(WINDOW_VAR)};\n function trail(p) {\n if (!p) return '/';\n return p.charAt(p.length - 1) === '/' ? p : p + '/';\n }\n function detect() {\n var nodes = document.querySelectorAll('script[src], link[href]');\n var origin = window.location.origin;\n for (var i = 0; i < nodes.length; i++) {\n var url = nodes[i].src || nodes[i].href || '';\n var idx = url.indexOf(marker);\n if (idx !== -1) {\n return trail(url.substring(0, idx + 1).replace(origin, '') || '/');\n }\n }\n return null;\n }\n function apply() {\n var detected = detect();\n if (detected) window[winKey] = detected;\n }\n apply();\n if (!window[winKey]) {\n if (document.readyState === 'loading') {\n document.addEventListener('DOMContentLoaded', apply, { once: true });\n } else {\n apply();\n }\n }\n})();`;\n}\n","import type { HtmlTagDescriptor, Plugin } from 'vite';\nimport { buildRuntimeInlineScript } from './inline-bootstrap.js';\nimport {\n PLUGIN_NAME,\n META_NAME,\n META_ASSET_MARKER,\n RELATIVE_BASE,\n assetPathMarker,\n} from './shared.js';\nimport type { ViteBasepathOptions } from './types.js';\n\nexport type { ViteBasepathOptions } from './types.js';\n\n/**\n * Vite plugin: build with `base: './'` and optional runtime base detection for routers.\n */\nexport default function viteBasepath(options: ViteBasepathOptions = {}): Plugin {\n const { injectRuntime = true, verbose = true } = options;\n\n let assetMarker = assetPathMarker();\n\n return {\n name: PLUGIN_NAME,\n apply: 'build',\n enforce: 'pre',\n\n config(_userConfig, { command }) {\n if (command !== 'build') return;\n\n if (verbose) {\n console.log(`\\n ╔═══════════════════════════════════════╗`);\n console.log(` ║ vite-basepath ║`);\n console.log(` ╠═══════════════════════════════════════╣`);\n console.log(` ║ Base path : \"${RELATIVE_BASE}\"`);\n console.log(` ║ Mode : Relative (works anywhere)`);\n console.log(` ╚═══════════════════════════════════════╝\\n`);\n }\n\n return { base: RELATIVE_BASE };\n },\n\n configResolved(config) {\n assetMarker = assetPathMarker(config.build.assetsDir);\n },\n\n transformIndexHtml: {\n order: 'post',\n handler(html, ctx) {\n if (!ctx.bundle) return html;\n\n const tags: HtmlTagDescriptor[] = [\n {\n tag: 'meta',\n attrs: { name: META_NAME, content: RELATIVE_BASE },\n injectTo: 'head',\n },\n {\n tag: 'meta',\n attrs: { name: META_ASSET_MARKER, content: assetMarker },\n injectTo: 'head',\n },\n ];\n\n if (injectRuntime) {\n tags.push({\n tag: 'script',\n attrs: { 'data-vite-basepath': '' },\n children: buildRuntimeInlineScript(assetMarker),\n injectTo: 'head',\n });\n }\n\n return { html, tags };\n },\n },\n };\n}\n"]}
@@ -0,0 +1,8 @@
1
+ /** Detected deploy base (always ends with `/`). e.g. `/`, `/demo/` */
2
+ declare function getBase(): string;
3
+ /** Full URL: origin + {@link getBase} */
4
+ declare function getAbsoluteBase(): string;
5
+ /** Path joined to detected base (no leading slash on `path`). */
6
+ declare function resolveUrl(path: string): string;
7
+
8
+ export { getAbsoluteBase, getBase, resolveUrl };
@@ -0,0 +1,57 @@
1
+ // src/shared.ts
2
+ var META_ASSET_MARKER = "vite-app-asset-marker";
3
+ var DEFAULT_ASSET_DIR = "assets";
4
+ function assetPathMarker(assetDir = DEFAULT_ASSET_DIR) {
5
+ const dir = assetDir.replace(/^\/+|\/+$/g, "");
6
+ return `/${dir}/`;
7
+ }
8
+ function ensureTrailingSlash(path) {
9
+ if (!path) return "/";
10
+ return path.endsWith("/") ? path : `${path}/`;
11
+ }
12
+ function detectBaseFromAssetUrl(url, origin, marker) {
13
+ const idx = url.indexOf(marker);
14
+ if (idx === -1) return null;
15
+ const raw = url.substring(0, idx + 1).replace(origin, "");
16
+ return ensureTrailingSlash(raw) || "/";
17
+ }
18
+
19
+ // src/runtime.ts
20
+ function getBase() {
21
+ if (typeof window === "undefined") return "/";
22
+ if (window.__VITE_BASE__) {
23
+ return ensureTrailingSlash(window.__VITE_BASE__);
24
+ }
25
+ return detectBaseFromAssets() || "/";
26
+ }
27
+ function getAbsoluteBase() {
28
+ if (typeof window === "undefined") return "";
29
+ return window.location.origin + getBase();
30
+ }
31
+ function resolveUrl(path) {
32
+ const base = getBase();
33
+ const cleanPath = path.startsWith("/") ? path.slice(1) : path;
34
+ return base + cleanPath;
35
+ }
36
+ function getAssetMarker() {
37
+ const meta = document.querySelector(`meta[name="${META_ASSET_MARKER}"]`);
38
+ return meta?.getAttribute("content") || assetPathMarker();
39
+ }
40
+ function detectBaseFromAssets() {
41
+ try {
42
+ const marker = getAssetMarker();
43
+ const nodes = document.querySelectorAll("script[src], link[href]");
44
+ const origin = window.location.origin;
45
+ for (const el of Array.from(nodes)) {
46
+ const url = el.src || el.href || "";
47
+ const detected = detectBaseFromAssetUrl(url, origin, marker);
48
+ if (detected) return detected;
49
+ }
50
+ } catch {
51
+ }
52
+ return null;
53
+ }
54
+
55
+ export { getAbsoluteBase, getBase, resolveUrl };
56
+ //# sourceMappingURL=runtime.js.map
57
+ //# sourceMappingURL=runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/shared.ts","../src/runtime.ts"],"names":[],"mappings":";AAEO,IAAM,iBAAA,GAAoB,uBAAA;AAG1B,IAAM,iBAAA,GAAoB,QAAA;AAE1B,SAAS,eAAA,CAAgB,WAAmB,iBAAA,EAA2B;AAC5E,EAAA,MAAM,GAAA,GAAM,QAAA,CAAS,OAAA,CAAQ,YAAA,EAAc,EAAE,CAAA;AAC7C,EAAA,OAAO,IAAI,GAAG,CAAA,CAAA,CAAA;AAChB;AAEO,SAAS,oBAAoB,IAAA,EAAsB;AACxD,EAAA,IAAI,CAAC,MAAM,OAAO,GAAA;AAClB,EAAA,OAAO,KAAK,QAAA,CAAS,GAAG,CAAA,GAAI,IAAA,GAAO,GAAG,IAAI,CAAA,CAAA,CAAA;AAC5C;AAEO,SAAS,sBAAA,CACd,GAAA,EACA,MAAA,EACA,MAAA,EACe;AACf,EAAA,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A,CAAS,gBAAA,CAAiB,yBAAyB,CAAA;AACjE,IAAA,MAAM,MAAA,GAAS,OAAO,QAAA,CAAS,MAAA;AAE/B,IAAA,KAAA,MAAW,EAAA,IAAM,KAAA,CAAM,IAAA,CAAK,KAAK,CAAA,EAAG;AAClC,MAAA,MAAM,GAAA,GAAO,EAAA,CAAyB,GAAA,IAAQ,EAAA,CAAuB,IAAA,IAAQ,EAAA;AAC7E,MAAA,MAAM,QAAA,GAAW,sBAAA,CAAuB,GAAA,EAAK,MAAA,EAAQ,MAAM,CAAA;AAC3D,MAAA,IAAI,UAAU,OAAO,QAAA;AAAA,IACvB;AAAA,EACF,CAAA,CAAA,MAAQ;AAAA,EAER;AACA,EAAA,OAAO,IAAA;AACT","file":"runtime.js","sourcesContent":["export const PLUGIN_NAME = 'vite-basepath';\nexport const META_NAME = 'vite-app-base';\nexport const META_ASSET_MARKER = 'vite-app-asset-marker';\nexport const WINDOW_VAR = '__VITE_BASE__';\nexport const RELATIVE_BASE = './';\nexport const DEFAULT_ASSET_DIR = 'assets';\n\nexport function assetPathMarker(assetDir: string = DEFAULT_ASSET_DIR): string {\n const dir = assetDir.replace(/^\\/+|\\/+$/g, '');\n return `/${dir}/`;\n}\n\nexport function ensureTrailingSlash(path: string): string {\n if (!path) return '/';\n return path.endsWith('/') ? path : `${path}/`;\n}\n\nexport function detectBaseFromAssetUrl(\n url: string,\n origin: string,\n marker: string,\n): string | null {\n const idx = url.indexOf(marker);\n if (idx === -1) return null;\n const raw = url.substring(0, idx + 1).replace(origin, '');\n return ensureTrailingSlash(raw) || '/';\n}\n","import {\n META_ASSET_MARKER,\n assetPathMarker,\n ensureTrailingSlash,\n detectBaseFromAssetUrl,\n} from './shared.js';\n\n/** Detected deploy base (always ends with `/`). e.g. `/`, `/demo/` */\nexport function getBase(): string {\n if (typeof window === 'undefined') return '/';\n\n if (window.__VITE_BASE__) {\n return ensureTrailingSlash(window.__VITE_BASE__);\n }\n\n return detectBaseFromAssets() || '/';\n}\n\n/** Full URL: origin + {@link getBase} */\nexport function getAbsoluteBase(): string {\n if (typeof window === 'undefined') return '';\n return window.location.origin + getBase();\n}\n\n/** Path joined to detected base (no leading slash on `path`). */\nexport function resolveUrl(path: string): string {\n const base = getBase();\n const cleanPath = path.startsWith('/') ? path.slice(1) : path;\n return base + cleanPath;\n}\n\nfunction getAssetMarker(): string {\n const meta = document.querySelector(`meta[name=\"${META_ASSET_MARKER}\"]`);\n return meta?.getAttribute('content') || assetPathMarker();\n}\n\nfunction detectBaseFromAssets(): string | null {\n try {\n const marker = getAssetMarker();\n const nodes = document.querySelectorAll('script[src], link[href]');\n const origin = window.location.origin;\n\n for (const el of Array.from(nodes)) {\n const url = (el as HTMLScriptElement).src || (el as HTMLLinkElement).href || '';\n const detected = detectBaseFromAssetUrl(url, origin, marker);\n if (detected) return detected;\n }\n } catch {\n /* ignore */\n }\n return null;\n}\n"]}
@@ -0,0 +1,11 @@
1
+ declare const PLUGIN_NAME = "vite-basepath";
2
+ declare const META_NAME = "vite-app-base";
3
+ declare const META_ASSET_MARKER = "vite-app-asset-marker";
4
+ declare const WINDOW_VAR = "__VITE_BASE__";
5
+ declare const RELATIVE_BASE = "./";
6
+ declare const DEFAULT_ASSET_DIR = "assets";
7
+ declare function assetPathMarker(assetDir?: string): string;
8
+ declare function ensureTrailingSlash(path: string): string;
9
+ declare function detectBaseFromAssetUrl(url: string, origin: string, marker: string): string | null;
10
+
11
+ export { DEFAULT_ASSET_DIR, META_ASSET_MARKER, META_NAME, PLUGIN_NAME, RELATIVE_BASE, WINDOW_VAR, assetPathMarker, detectBaseFromAssetUrl, ensureTrailingSlash };
package/dist/shared.js ADDED
@@ -0,0 +1,25 @@
1
+ // src/shared.ts
2
+ var PLUGIN_NAME = "vite-basepath";
3
+ var META_NAME = "vite-app-base";
4
+ var META_ASSET_MARKER = "vite-app-asset-marker";
5
+ var WINDOW_VAR = "__VITE_BASE__";
6
+ var RELATIVE_BASE = "./";
7
+ var DEFAULT_ASSET_DIR = "assets";
8
+ function assetPathMarker(assetDir = DEFAULT_ASSET_DIR) {
9
+ const dir = assetDir.replace(/^\/+|\/+$/g, "");
10
+ return `/${dir}/`;
11
+ }
12
+ function ensureTrailingSlash(path) {
13
+ if (!path) return "/";
14
+ return path.endsWith("/") ? path : `${path}/`;
15
+ }
16
+ function detectBaseFromAssetUrl(url, origin, marker) {
17
+ const idx = url.indexOf(marker);
18
+ if (idx === -1) return null;
19
+ const raw = url.substring(0, idx + 1).replace(origin, "");
20
+ return ensureTrailingSlash(raw) || "/";
21
+ }
22
+
23
+ export { DEFAULT_ASSET_DIR, META_ASSET_MARKER, META_NAME, PLUGIN_NAME, RELATIVE_BASE, WINDOW_VAR, assetPathMarker, detectBaseFromAssetUrl, ensureTrailingSlash };
24
+ //# sourceMappingURL=shared.js.map
25
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/shared.ts"],"names":[],"mappings":";AAAO,IAAM,WAAA,GAAc;AACpB,IAAM,SAAA,GAAY;AAClB,IAAM,iBAAA,GAAoB;AAC1B,IAAM,UAAA,GAAa;AACnB,IAAM,aAAA,GAAgB;AACtB,IAAM,iBAAA,GAAoB;AAE1B,SAAS,eAAA,CAAgB,WAAmB,iBAAA,EAA2B;AAC5E,EAAA,MAAM,GAAA,GAAM,QAAA,CAAS,OAAA,CAAQ,YAAA,EAAc,EAAE,CAAA;AAC7C,EAAA,OAAO,IAAI,GAAG,CAAA,CAAA,CAAA;AAChB;AAEO,SAAS,oBAAoB,IAAA,EAAsB;AACxD,EAAA,IAAI,CAAC,MAAM,OAAO,GAAA;AAClB,EAAA,OAAO,KAAK,QAAA,CAAS,GAAG,CAAA,GAAI,IAAA,GAAO,GAAG,IAAI,CAAA,CAAA,CAAA;AAC5C;AAEO,SAAS,sBAAA,CACd,GAAA,EACA,MAAA,EACA,MAAA,EACe;AACf,EAAA,MAAM,GAAA,GAAM,GAAA,CAAI,OAAA,CAAQ,MAAM,CAAA;AAC9B,EAAA,IAAI,GAAA,KAAQ,IAAI,OAAO,IAAA;AACvB,EAAA,MAAM,GAAA,GAAM,IAAI,SAAA,CAAU,CAAA,EAAG,MAAM,CAAC,CAAA,CAAE,OAAA,CAAQ,MAAA,EAAQ,EAAE,CAAA;AACxD,EAAA,OAAO,mBAAA,CAAoB,GAAG,CAAA,IAAK,GAAA;AACrC","file":"shared.js","sourcesContent":["export const PLUGIN_NAME = 'vite-basepath';\nexport const META_NAME = 'vite-app-base';\nexport const META_ASSET_MARKER = 'vite-app-asset-marker';\nexport const WINDOW_VAR = '__VITE_BASE__';\nexport const RELATIVE_BASE = './';\nexport const DEFAULT_ASSET_DIR = 'assets';\n\nexport function assetPathMarker(assetDir: string = DEFAULT_ASSET_DIR): string {\n const dir = assetDir.replace(/^\\/+|\\/+$/g, '');\n return `/${dir}/`;\n}\n\nexport function ensureTrailingSlash(path: string): string {\n if (!path) return '/';\n return path.endsWith('/') ? path : `${path}/`;\n}\n\nexport function detectBaseFromAssetUrl(\n url: string,\n origin: string,\n marker: string,\n): string | null {\n const idx = url.indexOf(marker);\n if (idx === -1) return null;\n const raw = url.substring(0, idx + 1).replace(origin, '');\n return ensureTrailingSlash(raw) || '/';\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "vite-basepath",
3
+ "version": "1.0.0",
4
+ "description": "Vite plugin: relative base (./) builds that work in any subdirectory, with runtime path detection for routers",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "sideEffects": false,
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "./runtime": {
17
+ "types": "./dist/runtime.d.ts",
18
+ "import": "./dist/runtime.js",
19
+ "default": "./dist/runtime.js"
20
+ }
21
+ },
22
+ "bin": {
23
+ "vite-basepath": "./bin/vite-basepath.js"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "bin/vite-basepath.js",
28
+ "README.md",
29
+ "LICENSE",
30
+ "CHANGELOG.md"
31
+ ],
32
+ "scripts": {
33
+ "build": "tsup",
34
+ "check": "tsc --noEmit",
35
+ "lint": "eslint .",
36
+ "lint:fix": "eslint . --fix",
37
+ "format": "prettier --write .",
38
+ "format:check": "prettier --check .",
39
+ "verify": "npm run build && node test/verify-logic.mjs",
40
+ "verify:build": "npm run build && npm install --prefix test/fixtures/minimal && node test/verify-build.mjs",
41
+ "test": "npm run lint && npm run format:check && npm run check && npm run verify && npm run verify:build",
42
+ "prepublishOnly": "npm run test"
43
+ },
44
+ "keywords": [
45
+ "vite",
46
+ "vite-plugin",
47
+ "plugin",
48
+ "base",
49
+ "base-path",
50
+ "subfolder",
51
+ "subdirectory",
52
+ "deploy",
53
+ "relative-path",
54
+ "assets",
55
+ "router",
56
+ "basename"
57
+ ],
58
+ "author": "Foisalislambd (https://github.com/Foisalislambd)",
59
+ "license": "MIT",
60
+ "peerDependencies": {
61
+ "vite": ">=3.0.0"
62
+ },
63
+ "devDependencies": {
64
+ "@eslint/js": "^9.17.0",
65
+ "@types/node": "^22.10.0",
66
+ "eslint": "^9.17.0",
67
+ "prettier": "^3.4.0",
68
+ "tsup": "^8.4.0",
69
+ "typescript": "^5.7.0",
70
+ "typescript-eslint": "^8.18.0",
71
+ "vite": "^8.0.0"
72
+ },
73
+ "engines": {
74
+ "node": ">=18.0.0"
75
+ },
76
+ "repository": {
77
+ "type": "git",
78
+ "url": "git+https://github.com/Foisalislambd/vite-basepath.git"
79
+ },
80
+ "bugs": {
81
+ "url": "https://github.com/Foisalislambd/vite-basepath/issues"
82
+ },
83
+ "homepage": "https://github.com/Foisalislambd/vite-basepath#readme",
84
+ "publishConfig": {
85
+ "access": "public"
86
+ }
87
+ }