vandler 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 ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2026 dyrgd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,44 @@
1
- # Temporary Holding Version
1
+ # vandler
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Vandler is a route-aware bundler for vanilla JavaScript frontends. Powered by [esbuild](https://esbuild.github.io/) and [esbuild-plugin-copy](https://github.com/LinbuduLab/esbuild-plugins/tree/master/packages/esbuild-plugin-copy), it automatically bundles route scripts and copies route pages from the configured input directory into matching output paths.
4
+
5
+ By default, vandler reads routes from `src/routes/` and writes the build to `dist/`. To change these paths, create `vandler.config.mjs` in the frontend project root and export a configuration object:
6
+
7
+ ```js
8
+ export default {
9
+ indir: "app/routes",
10
+ outdir: "build",
11
+ };
12
+ ```
13
+
14
+ `indir` is the route directory. `outdir` is the build output directory. Both paths are resolved relative to the frontend project root.
15
+
16
+ ## CLI
17
+
18
+ ```sh
19
+ vandler --dev
20
+ vandler --prod
21
+ vandler --watch --dev
22
+ ```
23
+
24
+ Choose exactly one of `--dev` or `--prod`. `--watch` rebuilds when route files change, including when routes are added or removed.
25
+
26
+ ## JavaScript API
27
+
28
+ Install the package, then import the build configuration in a script:
29
+
30
+ ```js
31
+ import { bundleOptions, vandler } from "vandler";
32
+
33
+ await vandler.build(await bundleOptions({ minify: true }));
34
+ ```
35
+
36
+ For development, create a context with `vandler.context(await bundleOptions({ minify: false, watch: true }))` and call `context.watch()`. `watchRoutes(onChange, await loadConfig())` watches the configured route directory and calls `onChange` when route files are added, changed, or removed; use it to recreate the context so the entry points stay in sync. Import `loadConfig` from `vandler` for this helper.
37
+
38
+ ## Tests
39
+
40
+ Run the tests with:
41
+
42
+ ```sh
43
+ npm test
44
+ ```
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { bundleOptions, loadConfig, vandler, watchRoutes } from "../index.mjs";
4
+
5
+ function parseArguments(args) {
6
+ const options = { watch: false, mode: undefined };
7
+ for (const argument of args) {
8
+ switch (argument) {
9
+ case "--watch":
10
+ options.watch = true;
11
+ break;
12
+ case "--dev":
13
+ if (options.mode && options.mode !== "dev") throw new Error("Choose either --dev or --prod.");
14
+ options.mode = "dev";
15
+ break;
16
+ case "--prod":
17
+ if (options.mode && options.mode !== "prod") throw new Error("Choose either --dev or --prod.");
18
+ options.mode = "prod";
19
+ break;
20
+ default:
21
+ throw new Error(`Unknown option: ${argument}`);
22
+ }
23
+ }
24
+
25
+ if (!options.mode) throw new Error("Select a build mode with --dev or --prod.");
26
+ return options;
27
+ }
28
+
29
+ async function main() {
30
+ const { watch, mode } = parseArguments(process.argv.slice(2));
31
+ const minify = mode === "prod";
32
+ const config = await loadConfig();
33
+
34
+ if (!watch) {
35
+ await vandler.build(await bundleOptions({ minify, config }));
36
+ return;
37
+ }
38
+
39
+ let context = await vandler.context(await bundleOptions({ minify, watch: true, config }));
40
+ await context.watch();
41
+ await watchRoutes(async () => {
42
+ await context.dispose();
43
+ context = await vandler.context(await bundleOptions({ minify, watch: true, config }));
44
+ await context.watch();
45
+ }, config);
46
+ }
47
+
48
+ main().catch((error) => {
49
+ console.error(error.message);
50
+ process.exitCode = 1;
51
+ });
package/index.mjs ADDED
@@ -0,0 +1,176 @@
1
+ import { watch } from "node:fs";
2
+ import { access, readdir, rm, rmdir } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+ import { copy } from "esbuild-plugin-copy";
6
+ import * as vandler from "esbuild";
7
+
8
+ export { vandler };
9
+
10
+ const defaultConfig = { indir: "src/routes", outdir: "dist" };
11
+
12
+ export async function loadConfig() {
13
+ const configPath = path.resolve("vandler.config.mjs");
14
+ try {
15
+ await access(configPath);
16
+ } catch (error) {
17
+ if (error.code === "ENOENT") return defaultConfig;
18
+ throw error;
19
+ }
20
+
21
+ const { default: config = {} } = await import(pathToFileURL(configPath).href);
22
+ const resolved = { ...defaultConfig, ...config };
23
+ for (const key of ["indir", "outdir"]) {
24
+ if (typeof resolved[key] !== "string" || resolved[key].length === 0) {
25
+ throw new TypeError(`vandler.config.mjs must define a non-empty string for ${key}.`);
26
+ }
27
+ }
28
+ return resolved;
29
+ }
30
+
31
+ async function filesIn(directory) {
32
+ const entries = await readdir(directory, { withFileTypes: true });
33
+ const nested = await Promise.all(entries.map(async (entry) => {
34
+ const file = path.join(directory, entry.name);
35
+ return entry.isDirectory() ? filesIn(file) : [file];
36
+ }));
37
+ return nested.flat();
38
+ }
39
+
40
+ function routeOutput(indir, file) {
41
+ const relative = path.relative(indir, file);
42
+ const parsed = path.parse(relative);
43
+ const segments = parsed.dir ? parsed.dir.split(path.sep) : [];
44
+ const routeName = segments.at(-1) === parsed.name
45
+ ? [...segments, "index"]
46
+ : [parsed.name, "index"];
47
+ return routeName.join("/");
48
+ }
49
+
50
+ async function removeStaleRouteOutputs(directory, relativeDirectory, activeScripts, activePages) {
51
+ let entries;
52
+ try {
53
+ entries = await readdir(directory, { withFileTypes: true });
54
+ } catch (error) {
55
+ if (error.code === "ENOENT") return;
56
+ throw error;
57
+ }
58
+
59
+ const routeDirectory = relativeDirectory.split(path.sep).join("/");
60
+ if (routeDirectory && routeDirectory !== "assets" && routeDirectory !== "components") {
61
+ if (!activeScripts.has(routeDirectory)) {
62
+ await Promise.all(["index.js", "index.css"].map((file) => rm(path.join(directory, file), { force: true })));
63
+ }
64
+ if (!activePages.has(routeDirectory)) {
65
+ await rm(path.join(directory, "index.html"), { force: true });
66
+ }
67
+ }
68
+
69
+ await Promise.all(entries
70
+ .filter((entry) => entry.isDirectory())
71
+ .filter((entry) => relativeDirectory || (entry.name !== "assets" && entry.name !== "components"))
72
+ .map(async (entry) => {
73
+ const child = path.join(directory, entry.name);
74
+ await removeStaleRouteOutputs(child, path.join(relativeDirectory, entry.name), activeScripts, activePages);
75
+ await rmdir(child).catch((error) => {
76
+ if (error.code !== "ENOTEMPTY" && error.code !== "ENOENT") throw error;
77
+ });
78
+ }));
79
+ }
80
+
81
+ export async function watchRoutes(onChange, { indir = defaultConfig.indir } = {}) {
82
+ const routeRoot = path.resolve(indir);
83
+ const watchers = new Map();
84
+ let rebuildTimer;
85
+ let changeQueue = Promise.resolve();
86
+
87
+ async function syncDirectoryWatchers(directory, activeDirectories) {
88
+ activeDirectories.add(directory);
89
+ if (!watchers.has(directory)) {
90
+ watchers.set(directory, watch(directory, () => {
91
+ void refreshWatchers().then(scheduleRebuild);
92
+ }));
93
+ }
94
+
95
+ let entries;
96
+ try {
97
+ entries = await readdir(directory, { withFileTypes: true });
98
+ } catch (error) {
99
+ if (error.code === "ENOENT") return;
100
+ throw error;
101
+ }
102
+ await Promise.all(entries
103
+ .filter((entry) => entry.isDirectory())
104
+ .map((entry) => syncDirectoryWatchers(path.join(directory, entry.name), activeDirectories)));
105
+ }
106
+
107
+ async function refreshWatchers() {
108
+ const activeDirectories = new Set();
109
+ await syncDirectoryWatchers(routeRoot, activeDirectories);
110
+ for (const [directory, watcher] of watchers) {
111
+ if (!activeDirectories.has(directory)) {
112
+ watcher.close();
113
+ watchers.delete(directory);
114
+ }
115
+ }
116
+ }
117
+
118
+ function scheduleRebuild() {
119
+ clearTimeout(rebuildTimer);
120
+ rebuildTimer = setTimeout(() => {
121
+ changeQueue = changeQueue.then(onChange);
122
+ }, 100);
123
+ }
124
+
125
+ await refreshWatchers();
126
+ }
127
+
128
+ export async function bundleOptions({ minify, watch = false, config } = {}) {
129
+ const { indir, outdir } = config ?? await loadConfig();
130
+ const projectRoot = process.cwd();
131
+ const routeRoot = path.resolve(projectRoot, indir);
132
+ const outputRoot = path.resolve(projectRoot, outdir);
133
+ const routeFiles = (await filesIn(routeRoot))
134
+ .filter((file) => file.endsWith(".mjs") || file.endsWith(".html"));
135
+ const scripts = routeFiles.filter((file) => file.endsWith(".mjs"));
136
+ const pages = routeFiles.filter((file) => file.endsWith(".html"));
137
+ const entryPoints = {
138
+ index: path.join(projectRoot, "src/index.mjs"),
139
+ sw: path.join(projectRoot, "src/sw.mjs"),
140
+ };
141
+
142
+ for (const file of scripts) {
143
+ const output = routeOutput(routeRoot, file);
144
+ if (entryPoints[output]) {
145
+ throw new Error(`Duplicate frontend entry point: ${output}`);
146
+ }
147
+ entryPoints[output] = file;
148
+ }
149
+
150
+ const activeScripts = new Set(scripts.map((file) => routeOutput(routeRoot, file).replace(/\/index$/, "")));
151
+ const activePages = new Set(pages.map((file) => routeOutput(routeRoot, file).replace(/\/index$/, "")));
152
+ await removeStaleRouteOutputs(outputRoot, "", activeScripts, activePages);
153
+
154
+ const assets = [
155
+ {
156
+ from: ["src/index.html", "src/favicon-192x192.png", "src/favicon-512x512.png", "src/logo.svg", "src/page-loader.css"],
157
+ to: [`${outputRoot}${path.sep}`],
158
+ },
159
+ ...pages.map((file) => ({ from: [file], to: [path.join(outputRoot, routeOutput(routeRoot, file).replace(/\/index$/, ""), "index.html")] })),
160
+ { from: ["src/components/footer/footer.html"], to: [`${path.join(outputRoot, "components/footer")}${path.sep}`] },
161
+ { from: ["src/components/header/header.html"], to: [`${path.join(outputRoot, "components/header")}${path.sep}`] },
162
+ { from: ["src/fontawesome/svgs/**/*"], to: [`${path.join(outputRoot, "assets/fontawesome/svgs")}${path.sep}`] },
163
+ ];
164
+
165
+ return {
166
+ absWorkingDir: projectRoot,
167
+ entryPoints,
168
+ outdir: outputRoot,
169
+ bundle: true,
170
+ format: "esm",
171
+ minify,
172
+ ...(watch ? { sourcemap: "inline" } : {}),
173
+ loader: { ".woff2": "file", ".woff": "file" },
174
+ plugins: [copy({ resolveFrom: "cwd", assets: assets.map((asset) => ({ ...asset, watch })) })],
175
+ };
176
+ }
package/package.json CHANGED
@@ -1,6 +1,24 @@
1
1
  {
2
2
  "name": "vandler",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Route-aware esbuild bundler for vanilla JavaScript frontends",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "test": "node tests/vandler.test.mjs"
9
+ },
10
+ "exports": {
11
+ ".": "./index.mjs"
12
+ },
13
+ "bin": {
14
+ "vandler": "./bin/vandler.mjs"
15
+ },
16
+ "files": [
17
+ "index.mjs",
18
+ "bin"
19
+ ],
20
+ "dependencies": {
21
+ "esbuild": "^0.28.2",
22
+ "esbuild-plugin-copy": "^2.1.1"
23
+ }
24
+ }