@buding0904/vitepad 0.3.1 → 0.3.2
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/README.md +4 -6
- package/dist/cli-runtime.cjs +818 -0
- package/dist/cli-runtime.cjs.map +1 -0
- package/dist/cli-runtime.d.ts +3 -0
- package/dist/cli-runtime.d.ts.map +1 -0
- package/dist/cli-runtime.js +807 -0
- package/dist/cli-runtime.js.map +1 -0
- package/dist/cli.cjs +33 -765
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +32 -761
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +104 -72
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +102 -70
- package/dist/index.js.map +1 -1
- package/dist/runtime/index.d.ts +2 -2
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,775 +1,46 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import process2 from 'process';
|
|
6
|
-
import { fileURLToPath, pathToFileURL } from 'url';
|
|
7
|
-
import { createServer, mergeConfig, normalizePath, createLogger } from 'vite';
|
|
8
|
-
import pc2 from 'picocolors';
|
|
9
|
-
import { spawn } from 'child_process';
|
|
10
|
-
import { createRequire } from 'module';
|
|
2
|
+
import fs from 'fs/promises';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
11
5
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
framework(requested, resolved) {
|
|
17
|
-
console.log(`
|
|
18
|
-
${pc2.cyan("vitepad")} ${pc2.bold("framework")} ${pc2.gray(requested)} ${pc2.gray("->")} ${pc2.green(resolved)}`);
|
|
19
|
-
},
|
|
20
|
-
install(packages) {
|
|
21
|
-
console.log(` ${pc2.gray("install")} ${packages.map((pkg) => pc2.cyan(pkg)).join(pc2.gray(", "))}`);
|
|
22
|
-
},
|
|
23
|
-
done(message) {
|
|
24
|
-
console.log(` ${pc2.green(message)}`);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
async function resolveFramework(spec, options) {
|
|
28
|
-
if (spec.name === "auto") {
|
|
29
|
-
throw new Error("Internal error: unresolved framework auto.");
|
|
30
|
-
}
|
|
31
|
-
if (spec.name === "vanilla") {
|
|
32
|
-
return {
|
|
33
|
-
name: "vanilla",
|
|
34
|
-
version: "local",
|
|
35
|
-
requested: "vanilla",
|
|
36
|
-
cacheStatus: "local",
|
|
37
|
-
aliases: [],
|
|
38
|
-
packageLinks: packageLinks(["unocss"]),
|
|
39
|
-
editorPackageLinks: []
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
const resolved = await resolveFrameworkPackages(spec.name, spec.version);
|
|
43
|
-
const cacheDir = frameworkCacheDir(spec.name, resolved.version);
|
|
44
|
-
const nodeModules = path2.join(cacheDir, "node_modules");
|
|
45
|
-
const installed = await isInstalled(cacheDir, resolved.packages);
|
|
46
|
-
if (options.forceInstall && await pathExists(cacheDir)) {
|
|
47
|
-
await fs2.rm(cacheDir, { recursive: true, force: true });
|
|
48
|
-
}
|
|
49
|
-
log.framework(`${spec.name}@${spec.version}`, `${spec.name}@${resolved.version}`);
|
|
50
|
-
const cacheStatus = options.forceInstall ? "miss" : installed ? "hit" : "miss";
|
|
51
|
-
if (options.forceInstall || !installed) {
|
|
52
|
-
log.install(resolved.packages);
|
|
53
|
-
await installFrameworkCache(cacheDir, resolved.packages);
|
|
54
|
-
}
|
|
55
|
-
await linkPeerPackages(cacheDir);
|
|
56
|
-
return {
|
|
57
|
-
name: spec.name,
|
|
58
|
-
version: resolved.version,
|
|
59
|
-
requested: `${spec.name}@${spec.version}`,
|
|
60
|
-
cacheStatus,
|
|
61
|
-
cacheDir,
|
|
62
|
-
aliases: [],
|
|
63
|
-
packageLinks: [
|
|
64
|
-
...packageLinks(["unocss"]),
|
|
65
|
-
...packageLinks(nodeModules, frameworkRuntimePackages(spec.name))
|
|
66
|
-
],
|
|
67
|
-
editorPackageLinks: packageLinks(nodeModules, frameworkEditorPackages(spec.name))
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
async function resolveFrameworkPackages(framework, version) {
|
|
71
|
-
const specs = frameworkPackageSpecs(framework, version);
|
|
72
|
-
const resolved = await Promise.all(specs.map(async (spec) => {
|
|
73
|
-
const parsed = splitPackageSpec(spec);
|
|
74
|
-
const resolvedVersion = await resolvePackageVersion(parsed.name, parsed.version);
|
|
75
|
-
return {
|
|
76
|
-
name: parsed.name,
|
|
77
|
-
version: resolvedVersion,
|
|
78
|
-
spec: `${parsed.name}@${resolvedVersion}`
|
|
79
|
-
};
|
|
80
|
-
}));
|
|
81
|
-
const primary = splitPackageSpec(specs[0]).name;
|
|
82
|
-
const primaryVersion = resolved.find((pkg) => pkg.name === primary)?.version;
|
|
83
|
-
if (!primaryVersion) {
|
|
84
|
-
throw new Error(`Failed to resolve ${framework}@${version}.`);
|
|
85
|
-
}
|
|
86
|
-
return {
|
|
87
|
-
version: primaryVersion,
|
|
88
|
-
packages: resolved.map((pkg) => pkg.spec)
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
function frameworkDedupe(framework) {
|
|
92
|
-
return frameworkRuntimePackages(framework);
|
|
93
|
-
}
|
|
94
|
-
function frameworkOptimizeDeps(framework) {
|
|
95
|
-
switch (framework) {
|
|
96
|
-
case "react":
|
|
97
|
-
return ["react", "react-dom", "react/jsx-runtime", "react/jsx-dev-runtime"];
|
|
98
|
-
case "preact":
|
|
99
|
-
return ["preact", "preact/jsx-runtime", "preact/hooks"];
|
|
100
|
-
case "solid":
|
|
101
|
-
return ["solid-js", "solid-js/web"];
|
|
102
|
-
case "vue":
|
|
103
|
-
return ["vue"];
|
|
104
|
-
case "svelte":
|
|
105
|
-
case "vanilla":
|
|
106
|
-
return [];
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
async function loadFrameworkPlugins(framework) {
|
|
110
|
-
if (framework.name === "vanilla") {
|
|
111
|
-
return [];
|
|
112
|
-
}
|
|
113
|
-
if (!framework.cacheDir) {
|
|
114
|
-
throw new Error(`Missing framework cache for ${framework.name}.`);
|
|
115
|
-
}
|
|
116
|
-
const plugins = [];
|
|
117
|
-
for (const pluginPackage of frameworkPluginPackages(framework.name)) {
|
|
118
|
-
const mod = await importCachePackage(framework.cacheDir, pluginPackage);
|
|
119
|
-
appendPlugin(plugins, createFrameworkPlugin(pluginPackage, mod));
|
|
120
|
-
}
|
|
121
|
-
return plugins;
|
|
6
|
+
async function packageVersion() {
|
|
7
|
+
const packageJson = await readPackageJson();
|
|
8
|
+
if (typeof packageJson.version === "string") return packageJson.version;
|
|
9
|
+
throw new Error("Failed to read vitepad version from package.json.");
|
|
122
10
|
}
|
|
123
|
-
function
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
if (pluginPackage === "@preact/preset-vite" && typeof mod.preact === "function") {
|
|
132
|
-
return mod.preact;
|
|
133
|
-
}
|
|
134
|
-
if (typeof mod.default === "function") {
|
|
135
|
-
return mod.default;
|
|
136
|
-
}
|
|
137
|
-
if (mod.default && typeof mod.default.default === "function") {
|
|
138
|
-
return mod.default.default;
|
|
139
|
-
}
|
|
140
|
-
throw new Error(`Failed to load Vite plugin ${pluginPackage}: no callable plugin export found.`);
|
|
141
|
-
}
|
|
142
|
-
function appendPlugin(plugins, plugin) {
|
|
143
|
-
if (Array.isArray(plugin)) {
|
|
144
|
-
plugins.push(...plugin);
|
|
145
|
-
} else {
|
|
146
|
-
plugins.push(plugin);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
function frameworkPackageSpecs(framework, version) {
|
|
150
|
-
switch (framework) {
|
|
151
|
-
case "react":
|
|
152
|
-
return [
|
|
153
|
-
`react@${version}`,
|
|
154
|
-
`react-dom@${version}`,
|
|
155
|
-
`@types/react@${reactTypesVersion(version)}`,
|
|
156
|
-
`@types/react-dom@${reactTypesVersion(version)}`,
|
|
157
|
-
"@vitejs/plugin-react@latest"
|
|
158
|
-
];
|
|
159
|
-
case "preact":
|
|
160
|
-
return [`preact@${version}`, "@preact/preset-vite@latest"];
|
|
161
|
-
case "solid":
|
|
162
|
-
return [`solid-js@${version}`, "vite-plugin-solid@latest"];
|
|
163
|
-
case "vue":
|
|
164
|
-
return [`vue@${version}`, "@vitejs/plugin-vue@latest", "@vitejs/plugin-vue-jsx@latest"];
|
|
165
|
-
case "svelte":
|
|
166
|
-
return [`svelte@${version}`, "@sveltejs/vite-plugin-svelte@latest"];
|
|
167
|
-
case "vanilla":
|
|
168
|
-
return [];
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
function reactTypesVersion(version) {
|
|
172
|
-
const major = version.match(/^\D*(\d+)/)?.[1];
|
|
173
|
-
return major || "latest";
|
|
174
|
-
}
|
|
175
|
-
function resolvePackageVersion(name, range) {
|
|
176
|
-
return new Promise((resolve, reject) => {
|
|
177
|
-
const child = spawn("npm", ["view", `${name}@${range}`, "version", "--json"], {
|
|
178
|
-
stdio: ["ignore", "pipe", "pipe"]
|
|
179
|
-
});
|
|
180
|
-
let stdout = "";
|
|
181
|
-
let stderr = "";
|
|
182
|
-
child.stdout.on("data", (chunk) => {
|
|
183
|
-
stdout += chunk;
|
|
184
|
-
});
|
|
185
|
-
child.stderr.on("data", (chunk) => {
|
|
186
|
-
stderr += chunk;
|
|
187
|
-
});
|
|
188
|
-
child.on("error", reject);
|
|
189
|
-
child.on("exit", (code) => {
|
|
190
|
-
if (code !== 0) {
|
|
191
|
-
reject(new Error(`npm view failed for ${name}@${range}
|
|
192
|
-
${stderr.trim()}`));
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
try {
|
|
196
|
-
const parsed = JSON.parse(stdout.trim());
|
|
197
|
-
const version = Array.isArray(parsed) ? parsed[parsed.length - 1] : parsed;
|
|
198
|
-
if (typeof version !== "string" || !version) {
|
|
199
|
-
throw new Error(`Unexpected npm view output: ${stdout}`);
|
|
200
|
-
}
|
|
201
|
-
resolve(version);
|
|
202
|
-
} catch (error) {
|
|
203
|
-
reject(error);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
function frameworkRuntimePackages(framework) {
|
|
209
|
-
switch (framework) {
|
|
210
|
-
case "react":
|
|
211
|
-
return ["react", "react-dom"];
|
|
212
|
-
case "preact":
|
|
213
|
-
return ["preact"];
|
|
214
|
-
case "solid":
|
|
215
|
-
return ["solid-js"];
|
|
216
|
-
case "vue":
|
|
217
|
-
return ["vue"];
|
|
218
|
-
case "svelte":
|
|
219
|
-
return ["svelte"];
|
|
220
|
-
case "vanilla":
|
|
221
|
-
return [];
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
function frameworkEditorPackages(framework) {
|
|
225
|
-
switch (framework) {
|
|
226
|
-
case "react":
|
|
227
|
-
return ["react", "react-dom", "@types/react", "@types/react-dom"];
|
|
228
|
-
case "preact":
|
|
229
|
-
case "solid":
|
|
230
|
-
case "vue":
|
|
231
|
-
case "svelte":
|
|
232
|
-
case "vanilla":
|
|
233
|
-
return frameworkRuntimePackages(framework);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
function frameworkPluginPackages(framework) {
|
|
237
|
-
switch (framework) {
|
|
238
|
-
case "react":
|
|
239
|
-
return ["@vitejs/plugin-react"];
|
|
240
|
-
case "preact":
|
|
241
|
-
return ["@preact/preset-vite"];
|
|
242
|
-
case "solid":
|
|
243
|
-
return ["vite-plugin-solid"];
|
|
244
|
-
case "vue":
|
|
245
|
-
return ["@vitejs/plugin-vue", "@vitejs/plugin-vue-jsx"];
|
|
246
|
-
case "svelte":
|
|
247
|
-
return ["@sveltejs/vite-plugin-svelte"];
|
|
248
|
-
case "vanilla":
|
|
249
|
-
return [];
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
function packageLinks(nodeModulesOrPackageNames, maybePackageNames) {
|
|
253
|
-
if (Array.isArray(nodeModulesOrPackageNames)) {
|
|
254
|
-
return nodeModulesOrPackageNames.map((packageName) => ({
|
|
255
|
-
name: packageName,
|
|
256
|
-
source: resolveInstalledPackageDir(packageName)
|
|
257
|
-
}));
|
|
258
|
-
}
|
|
259
|
-
const nodeModules = nodeModulesOrPackageNames;
|
|
260
|
-
const packageNames = maybePackageNames ?? [];
|
|
261
|
-
return packageNames.map((packageName) => ({
|
|
262
|
-
name: packageName,
|
|
263
|
-
source: path2.join(nodeModules, packageName)
|
|
264
|
-
}));
|
|
265
|
-
}
|
|
266
|
-
function resolveInstalledPackageDir(packageName) {
|
|
267
|
-
const packageJson = requireFromPackage.resolve(`${packageName}/package.json`);
|
|
268
|
-
return path2.dirname(packageJson);
|
|
269
|
-
}
|
|
270
|
-
function frameworkCacheDir(framework, version) {
|
|
271
|
-
const base = process.env.VITEPAD_CACHE_DIR || (process.env.XDG_CACHE_HOME ? path2.join(process.env.XDG_CACHE_HOME, "vitepad") : path2.join(os.homedir(), ".cache", "vitepad"));
|
|
272
|
-
return path2.join(base, "frameworks", `${framework}-${sanitizeCacheKey(version)}`);
|
|
273
|
-
}
|
|
274
|
-
function sanitizeCacheKey(value) {
|
|
275
|
-
return value.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
276
|
-
}
|
|
277
|
-
async function isInstalled(cacheDir, packages) {
|
|
278
|
-
if (!await pathExists(path2.join(cacheDir, "node_modules"))) return false;
|
|
279
|
-
for (const pkg of packages) {
|
|
280
|
-
const { name } = splitPackageSpec(pkg);
|
|
281
|
-
if (!await pathExists(path2.join(cacheDir, "node_modules", name))) {
|
|
282
|
-
return false;
|
|
11
|
+
async function readPackageJson() {
|
|
12
|
+
let current = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
while (true) {
|
|
14
|
+
const packageJsonPath = path.join(current, "package.json");
|
|
15
|
+
const source = await fs.readFile(packageJsonPath, "utf8").catch(() => null);
|
|
16
|
+
if (source) {
|
|
17
|
+
const packageJson = JSON.parse(source);
|
|
18
|
+
if (packageJson.name === "@buding0904/vitepad") return packageJson;
|
|
283
19
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
async function installFrameworkCache(cacheDir, packages) {
|
|
288
|
-
await fs2.mkdir(cacheDir, { recursive: true });
|
|
289
|
-
await fs2.writeFile(path2.join(cacheDir, "package.json"), JSON.stringify({
|
|
290
|
-
private: true,
|
|
291
|
-
type: "module",
|
|
292
|
-
dependencies: Object.fromEntries(packages.map((pkg) => packageToDependency(pkg)))
|
|
293
|
-
}, null, 2));
|
|
294
|
-
await runInstall(cacheDir);
|
|
295
|
-
}
|
|
296
|
-
function runInstall(cwd) {
|
|
297
|
-
return new Promise((resolve, reject) => {
|
|
298
|
-
const child = spawn("npm", ["install", "--legacy-peer-deps", "--no-audit", "--no-fund", "--loglevel=notice"], {
|
|
299
|
-
cwd,
|
|
300
|
-
stdio: ["ignore", "pipe", "pipe"]
|
|
301
|
-
});
|
|
302
|
-
const progress = createProgress("vitepad: installing framework packages");
|
|
303
|
-
let output = "";
|
|
304
|
-
child.stdout.on("data", (chunk) => {
|
|
305
|
-
output += chunk;
|
|
306
|
-
progress.tick();
|
|
307
|
-
});
|
|
308
|
-
child.stderr.on("data", (chunk) => {
|
|
309
|
-
output += chunk;
|
|
310
|
-
progress.tick();
|
|
311
|
-
});
|
|
312
|
-
child.on("error", (error) => {
|
|
313
|
-
progress.stop();
|
|
314
|
-
reject(error);
|
|
315
|
-
});
|
|
316
|
-
child.on("exit", (code) => {
|
|
317
|
-
progress.stop();
|
|
318
|
-
if (code === 0) {
|
|
319
|
-
log.done("install complete");
|
|
320
|
-
resolve();
|
|
321
|
-
} else {
|
|
322
|
-
reject(new Error(`npm install failed with exit code ${code}
|
|
323
|
-
${output.trim()}`));
|
|
324
|
-
}
|
|
325
|
-
});
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
async function linkPeerPackages(cacheDir) {
|
|
329
|
-
await fs2.mkdir(path2.join(cacheDir, "node_modules"), { recursive: true });
|
|
330
|
-
for (const packageName of linkedPeerPackages) {
|
|
331
|
-
try {
|
|
332
|
-
await linkPackage({
|
|
333
|
-
source: resolveInstalledPackageDir(packageName),
|
|
334
|
-
target: path2.join(cacheDir, "node_modules", packageName)
|
|
335
|
-
});
|
|
336
|
-
} catch (error) {
|
|
337
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
338
|
-
throw new Error(`Failed to link ${packageName} into vitepad cache at ${cacheDir}.
|
|
339
|
-
${message}`);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
async function linkPackage(input) {
|
|
344
|
-
const source = await fs2.realpath(input.source);
|
|
345
|
-
const existing = await fs2.lstat(input.target).catch(() => null);
|
|
346
|
-
if (existing) {
|
|
347
|
-
if (existing.isSymbolicLink()) {
|
|
348
|
-
const current = await fs2.realpath(input.target).catch(() => null);
|
|
349
|
-
if (current === source) return;
|
|
20
|
+
const parent = path.dirname(current);
|
|
21
|
+
if (parent === current) {
|
|
22
|
+
throw new Error("Failed to locate vitepad package.json.");
|
|
350
23
|
}
|
|
351
|
-
|
|
24
|
+
current = parent;
|
|
352
25
|
}
|
|
353
|
-
await fs2.symlink(source, input.target, "dir");
|
|
354
|
-
}
|
|
355
|
-
function createProgress(label) {
|
|
356
|
-
const width = 18;
|
|
357
|
-
let frame = 0;
|
|
358
|
-
const render = () => {
|
|
359
|
-
const position = frame % width;
|
|
360
|
-
const bar = Array.from({ length: width }, (_, index) => index === position ? "=" : "-").join("");
|
|
361
|
-
process.stderr.write(`\r${label} [${bar}]`);
|
|
362
|
-
frame += 1;
|
|
363
|
-
};
|
|
364
|
-
const timer = setInterval(render, 120);
|
|
365
|
-
render();
|
|
366
|
-
return {
|
|
367
|
-
tick: render,
|
|
368
|
-
stop() {
|
|
369
|
-
clearInterval(timer);
|
|
370
|
-
process.stderr.write(`\r${" ".repeat(label.length + width + 4)}\r`);
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
function packageToDependency(pkg) {
|
|
375
|
-
const parsed = splitPackageSpec(pkg);
|
|
376
|
-
return [parsed.name, parsed.version];
|
|
377
|
-
}
|
|
378
|
-
function splitPackageSpec(spec) {
|
|
379
|
-
if (spec.startsWith("@")) {
|
|
380
|
-
const secondAt = spec.indexOf("@", 1);
|
|
381
|
-
if (secondAt === -1) return { name: spec, version: "latest" };
|
|
382
|
-
return { name: spec.slice(0, secondAt), version: spec.slice(secondAt + 1) };
|
|
383
|
-
}
|
|
384
|
-
const at = spec.indexOf("@");
|
|
385
|
-
if (at === -1) return { name: spec, version: "latest" };
|
|
386
|
-
return { name: spec.slice(0, at), version: spec.slice(at + 1) };
|
|
387
|
-
}
|
|
388
|
-
async function importCachePackage(cacheDir, packageName) {
|
|
389
|
-
const requireFromCache = createRequire(path2.join(cacheDir, "package.json"));
|
|
390
|
-
const resolved = requireFromCache.resolve(packageName);
|
|
391
|
-
return import(pathToFileURL(resolved).href);
|
|
392
|
-
}
|
|
393
|
-
async function pathExists(file) {
|
|
394
|
-
return fs2.access(file).then(() => true, () => false);
|
|
395
26
|
}
|
|
396
27
|
|
|
397
|
-
// src/
|
|
398
|
-
var
|
|
399
|
-
var
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
if (!options.entry) {
|
|
409
|
-
throw new Error(helpText("Missing entry file."));
|
|
410
|
-
}
|
|
411
|
-
const entry = path2.resolve(process2.cwd(), options.entry);
|
|
412
|
-
const stat = await fs2.stat(entry).catch(() => null);
|
|
413
|
-
if (!stat?.isFile()) {
|
|
414
|
-
throw new Error(`Entry file does not exist: ${entry}`);
|
|
415
|
-
}
|
|
416
|
-
const extension = path2.extname(entry).toLowerCase();
|
|
417
|
-
const mode = inferMode(extension);
|
|
418
|
-
const source = await fs2.readFile(entry, "utf8");
|
|
419
|
-
const framework = inferFramework({
|
|
420
|
-
extension,
|
|
421
|
-
source,
|
|
422
|
-
requested: options.framework,
|
|
423
|
-
version: options.frameworkVersion
|
|
424
|
-
});
|
|
425
|
-
validateCombination({ mode, framework: framework.name, extension });
|
|
426
|
-
const resolvedFramework = await resolveFramework(framework, { forceInstall: options.forceInstall });
|
|
427
|
-
await setupEditorPackages(path2.dirname(entry), resolvedFramework.editorPackageLinks);
|
|
428
|
-
const workspace = await createWorkspace({
|
|
429
|
-
entry,
|
|
430
|
-
mode,
|
|
431
|
-
framework: resolvedFramework.name,
|
|
432
|
-
packageLinks: resolvedFramework.packageLinks
|
|
433
|
-
});
|
|
434
|
-
const config = await loadUserConfig(options.config);
|
|
435
|
-
const server = await createServer(mergeConfig({
|
|
436
|
-
root: workspace,
|
|
437
|
-
configFile: false,
|
|
438
|
-
server: {
|
|
439
|
-
host: options.host,
|
|
440
|
-
port: options.port,
|
|
441
|
-
open: options.open,
|
|
442
|
-
watch: {
|
|
443
|
-
ignored: (file) => normalizePath(file).startsWith(`${normalizePath(workspace)}/`)
|
|
444
|
-
},
|
|
445
|
-
fs: {
|
|
446
|
-
allow: [
|
|
447
|
-
rootDir,
|
|
448
|
-
process2.cwd(),
|
|
449
|
-
path2.dirname(entry),
|
|
450
|
-
workspace,
|
|
451
|
-
...resolvedFramework.cacheDir ? [resolvedFramework.cacheDir] : []
|
|
452
|
-
]
|
|
453
|
-
}
|
|
454
|
-
},
|
|
455
|
-
plugins: await loadPlugins(resolvedFramework),
|
|
456
|
-
resolve: {
|
|
457
|
-
alias: [
|
|
458
|
-
...resolvedFramework.aliases,
|
|
459
|
-
{
|
|
460
|
-
find: "@",
|
|
461
|
-
replacement: path2.dirname(entry)
|
|
462
|
-
}
|
|
463
|
-
],
|
|
464
|
-
dedupe: frameworkDedupe(resolvedFramework.name)
|
|
465
|
-
},
|
|
466
|
-
optimizeDeps: {
|
|
467
|
-
entries: [path2.join(workspace, "src/main.js")],
|
|
468
|
-
include: frameworkOptimizeDeps(resolvedFramework.name)
|
|
469
|
-
},
|
|
470
|
-
customLogger: createVitepadLogger()
|
|
471
|
-
}, config));
|
|
472
|
-
await server.listen();
|
|
473
|
-
printReady({
|
|
474
|
-
entry,
|
|
475
|
-
framework: resolvedFramework,
|
|
476
|
-
urls: server.resolvedUrls
|
|
28
|
+
// src/cli.ts
|
|
29
|
+
var argv = process.argv.slice(2);
|
|
30
|
+
var runtimeEntry = "./cli-runtime.js";
|
|
31
|
+
if (argv.includes("-v") || argv.includes("--version")) {
|
|
32
|
+
packageVersion().then((version) => {
|
|
33
|
+
console.log(version);
|
|
34
|
+
}).catch((error) => {
|
|
35
|
+
console.error(error instanceof Error ? error.message : error);
|
|
36
|
+
process.exit(1);
|
|
477
37
|
});
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
process2.exit(0);
|
|
481
|
-
};
|
|
482
|
-
process2.once("SIGINT", close);
|
|
483
|
-
process2.once("SIGTERM", close);
|
|
484
|
-
}
|
|
485
|
-
function parseArgs(argv) {
|
|
486
|
-
const options = {
|
|
487
|
-
framework: "auto",
|
|
488
|
-
frameworkVersion: "latest",
|
|
489
|
-
forceInstall: false,
|
|
490
|
-
port: 8e3,
|
|
491
|
-
host: "0.0.0.0",
|
|
492
|
-
open: "/",
|
|
493
|
-
help: false
|
|
494
|
-
};
|
|
495
|
-
for (let index = 0; index < argv.length; index += 1) {
|
|
496
|
-
const arg = argv[index];
|
|
497
|
-
if (arg === "-h" || arg === "--help") {
|
|
498
|
-
options.help = true;
|
|
499
|
-
} else if (arg === "--framework" || arg === "-f") {
|
|
500
|
-
Object.assign(options, parseFramework(readValue(argv, ++index, arg)));
|
|
501
|
-
} else if (arg.startsWith("--framework=")) {
|
|
502
|
-
Object.assign(options, parseFramework(arg.slice("--framework=".length)));
|
|
503
|
-
} else if (arg === "--force-install") {
|
|
504
|
-
options.forceInstall = true;
|
|
505
|
-
} else if (arg === "--port" || arg === "-p") {
|
|
506
|
-
options.port = Number(readValue(argv, ++index, arg));
|
|
507
|
-
} else if (arg.startsWith("--port=")) {
|
|
508
|
-
options.port = Number(arg.slice("--port=".length));
|
|
509
|
-
} else if (arg === "--host") {
|
|
510
|
-
options.host = readValue(argv, ++index, arg);
|
|
511
|
-
} else if (arg.startsWith("--host=")) {
|
|
512
|
-
options.host = arg.slice("--host=".length);
|
|
513
|
-
} else if (arg === "--no-open") {
|
|
514
|
-
options.open = false;
|
|
515
|
-
} else if (arg === "--open") {
|
|
516
|
-
options.open = "/";
|
|
517
|
-
} else if (arg === "--config" || arg === "-c") {
|
|
518
|
-
options.config = readValue(argv, ++index, arg);
|
|
519
|
-
} else if (arg.startsWith("--config=")) {
|
|
520
|
-
options.config = arg.slice("--config=".length);
|
|
521
|
-
} else if (arg.startsWith("-")) {
|
|
522
|
-
throw new Error(`Unknown option: ${arg}`);
|
|
523
|
-
} else if (!options.entry) {
|
|
524
|
-
options.entry = arg;
|
|
525
|
-
} else {
|
|
526
|
-
throw new Error(`Unexpected argument: ${arg}`);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
if (!Number.isInteger(options.port) || options.port < 0 || options.port > 65535) {
|
|
530
|
-
throw new Error(`Invalid port: ${options.port}`);
|
|
531
|
-
}
|
|
532
|
-
return options;
|
|
533
|
-
}
|
|
534
|
-
function parseFramework(value) {
|
|
535
|
-
const match = value.match(/^([^@]+)(?:@(.+))?$/);
|
|
536
|
-
const name = match?.[1];
|
|
537
|
-
const version = match?.[2] || "latest";
|
|
538
|
-
if (name && frameworkValues.has(name)) {
|
|
539
|
-
return { framework: name, frameworkVersion: version };
|
|
540
|
-
}
|
|
541
|
-
throw new Error(`Unsupported framework "${value}". Use one of: ${[...frameworkValues].join(", ")}, optionally with @version.`);
|
|
542
|
-
}
|
|
543
|
-
function readValue(argv, index, option) {
|
|
544
|
-
const value = argv[index];
|
|
545
|
-
if (!value || value.startsWith("-")) {
|
|
546
|
-
throw new Error(`Missing value for ${option}`);
|
|
547
|
-
}
|
|
548
|
-
return value;
|
|
549
|
-
}
|
|
550
|
-
function inferMode(extension) {
|
|
551
|
-
if (supportedMainExts.has(extension)) return "main";
|
|
552
|
-
if (supportedComponentExts.has(extension)) return "component";
|
|
553
|
-
throw new Error(`Unsupported entry extension "${extension}". Supported: js, ts, jsx, tsx, vue, svelte.`);
|
|
554
|
-
}
|
|
555
|
-
function inferFramework(input) {
|
|
556
|
-
const { extension, source, requested, version } = input;
|
|
557
|
-
if (requested !== "auto") return { name: requested, version };
|
|
558
|
-
if (extension === ".vue") return { name: "vue", version };
|
|
559
|
-
if (extension === ".svelte") return { name: "svelte", version };
|
|
560
|
-
if (/\bfrom\s+['"]solid-js\b|\bfrom\s+['"]solid-js\/web\b|\bsolid-js\b/.test(source)) return { name: "solid", version };
|
|
561
|
-
if (/\bfrom\s+['"]preact\b|\bfrom\s+['"]preact\/compat\b|\bfrom\s+['"]preact\/hooks\b/.test(source)) return { name: "preact", version };
|
|
562
|
-
if (/\bfrom\s+['"]vue\b/.test(source)) return { name: "vue", version };
|
|
563
|
-
if (/\bfrom\s+['"]svelte\b|\bfrom\s+['"]svelte\//.test(source)) return { name: "svelte", version };
|
|
564
|
-
if (extension === ".jsx" || extension === ".tsx") return { name: "react", version };
|
|
565
|
-
return { name: "vanilla", version: "local" };
|
|
566
|
-
}
|
|
567
|
-
function validateCombination(input) {
|
|
568
|
-
const { mode, framework, extension } = input;
|
|
569
|
-
if (mode === "component" && framework === "vanilla") {
|
|
570
|
-
throw new Error(`Component entry ${extension} needs a framework. Try --framework react, vue, svelte, preact, or solid.`);
|
|
571
|
-
}
|
|
572
|
-
if (extension === ".vue" && framework !== "vue") {
|
|
573
|
-
throw new Error(".vue entries must use --framework vue.");
|
|
574
|
-
}
|
|
575
|
-
if (extension === ".svelte" && framework !== "svelte") {
|
|
576
|
-
throw new Error(".svelte entries must use --framework svelte.");
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
async function createWorkspace(input) {
|
|
580
|
-
const workspace = await fs2.mkdtemp(path2.join(await fs2.realpath(os.tmpdir()), "vitepad-"));
|
|
581
|
-
const srcDir = path2.join(workspace, "src");
|
|
582
|
-
const nodeModulesDir = path2.join(workspace, "node_modules");
|
|
583
|
-
await fs2.mkdir(srcDir, { recursive: true });
|
|
584
|
-
await fs2.mkdir(nodeModulesDir, { recursive: true });
|
|
585
|
-
await Promise.all([
|
|
586
|
-
fs2.writeFile(path2.join(workspace, "index.html"), htmlTemplate()),
|
|
587
|
-
fs2.writeFile(path2.join(srcDir, "main.js"), mainTemplate(input)),
|
|
588
|
-
...input.packageLinks.map((link) => linkWorkspacePackage(nodeModulesDir, link))
|
|
589
|
-
]);
|
|
590
|
-
return workspace;
|
|
591
|
-
}
|
|
592
|
-
async function linkWorkspacePackage(nodeModulesDir, link) {
|
|
593
|
-
const source = await fs2.realpath(link.source);
|
|
594
|
-
const target = path2.join(nodeModulesDir, link.name);
|
|
595
|
-
await fs2.mkdir(path2.dirname(target), { recursive: true });
|
|
596
|
-
await fs2.symlink(source, target, "dir");
|
|
597
|
-
}
|
|
598
|
-
async function setupEditorPackages(projectDir, links) {
|
|
599
|
-
if (links.length === 0) return;
|
|
600
|
-
const nodeModulesDir = path2.join(projectDir, "node_modules");
|
|
601
|
-
await fs2.mkdir(nodeModulesDir, { recursive: true });
|
|
602
|
-
const linked = [];
|
|
603
|
-
for (const link of links) {
|
|
604
|
-
const result = await linkEditorPackage(nodeModulesDir, link);
|
|
605
|
-
if (result === "linked") {
|
|
606
|
-
linked.push(link.name);
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
if (linked.length > 0) {
|
|
610
|
-
console.log(`${pc2.cyan("vitepad")} ${pc2.green("editor")} linked ${linked.map((name) => pc2.cyan(name)).join(pc2.gray(", "))}`);
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
async function linkEditorPackage(nodeModulesDir, link) {
|
|
614
|
-
const source = await fs2.realpath(link.source);
|
|
615
|
-
const target = path2.join(nodeModulesDir, link.name);
|
|
616
|
-
await fs2.mkdir(path2.dirname(target), { recursive: true });
|
|
617
|
-
const existing = await fs2.lstat(target).catch(() => null);
|
|
618
|
-
if (existing) {
|
|
619
|
-
if (existing.isSymbolicLink()) {
|
|
620
|
-
const current = await fs2.realpath(target).catch(() => null);
|
|
621
|
-
if (current === source) return "skipped";
|
|
622
|
-
}
|
|
623
|
-
return "skipped";
|
|
624
|
-
}
|
|
625
|
-
await fs2.symlink(source, target, "dir");
|
|
626
|
-
return "linked";
|
|
38
|
+
} else {
|
|
39
|
+
import(runtimeEntry).catch(handleError);
|
|
627
40
|
}
|
|
628
|
-
function
|
|
629
|
-
return `<!doctype html>
|
|
630
|
-
<html lang="en">
|
|
631
|
-
<head>
|
|
632
|
-
<meta charset="UTF-8" />
|
|
633
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
634
|
-
<title>vitepad</title>
|
|
635
|
-
</head>
|
|
636
|
-
<body>
|
|
637
|
-
<div id="root"></div>
|
|
638
|
-
<script type="module" src="/src/main.js"></script>
|
|
639
|
-
</body>
|
|
640
|
-
</html>
|
|
641
|
-
`;
|
|
642
|
-
}
|
|
643
|
-
function mainTemplate(input) {
|
|
644
|
-
const { entry, mode, framework } = input;
|
|
645
|
-
const importPath = `/@fs/${normalizePath(entry)}`;
|
|
646
|
-
if (mode === "main") {
|
|
647
|
-
return `import 'virtual:uno.css'
|
|
648
|
-
import ${JSON.stringify(importPath)}
|
|
649
|
-
`;
|
|
650
|
-
}
|
|
651
|
-
switch (framework) {
|
|
652
|
-
case "react":
|
|
653
|
-
return `import 'virtual:uno.css'
|
|
654
|
-
import React from 'react'
|
|
655
|
-
import { createRoot } from 'react-dom/client'
|
|
656
|
-
import App from ${JSON.stringify(importPath)}
|
|
657
|
-
|
|
658
|
-
createRoot(document.getElementById('root')).render(React.createElement(App))
|
|
659
|
-
`;
|
|
660
|
-
case "preact":
|
|
661
|
-
return `import 'virtual:uno.css'
|
|
662
|
-
import { h, render } from 'preact'
|
|
663
|
-
import App from ${JSON.stringify(importPath)}
|
|
664
|
-
|
|
665
|
-
render(h(App, null), document.getElementById('root'))
|
|
666
|
-
`;
|
|
667
|
-
case "solid":
|
|
668
|
-
return `import 'virtual:uno.css'
|
|
669
|
-
import { render } from 'solid-js/web'
|
|
670
|
-
import App from ${JSON.stringify(importPath)}
|
|
671
|
-
|
|
672
|
-
render(() => App({}), document.getElementById('root'))
|
|
673
|
-
`;
|
|
674
|
-
case "vue":
|
|
675
|
-
return `import 'virtual:uno.css'
|
|
676
|
-
import { createApp } from 'vue'
|
|
677
|
-
import App from ${JSON.stringify(importPath)}
|
|
678
|
-
|
|
679
|
-
createApp(App).mount('#root')
|
|
680
|
-
`;
|
|
681
|
-
case "svelte":
|
|
682
|
-
return `import 'virtual:uno.css'
|
|
683
|
-
import { mount } from 'svelte'
|
|
684
|
-
import App from ${JSON.stringify(importPath)}
|
|
685
|
-
|
|
686
|
-
const target = document.getElementById('root')
|
|
687
|
-
const app = mount(App, { target })
|
|
688
|
-
export default app
|
|
689
|
-
`;
|
|
690
|
-
case "vanilla":
|
|
691
|
-
throw new Error("Vanilla entries cannot be used as component entries.");
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
async function loadUserConfig(configFile) {
|
|
695
|
-
if (!configFile) return {};
|
|
696
|
-
const resolved = path2.resolve(process2.cwd(), configFile);
|
|
697
|
-
const configModule = await import(pathToFileURL(resolved).href);
|
|
698
|
-
return configModule.default ?? configModule;
|
|
699
|
-
}
|
|
700
|
-
async function loadPlugins(framework) {
|
|
701
|
-
const [{ default: unocss }, { presetUno }, frameworkPlugins] = await Promise.all([
|
|
702
|
-
import('unocss/vite'),
|
|
703
|
-
import('unocss'),
|
|
704
|
-
loadFrameworkPlugins(framework)
|
|
705
|
-
]);
|
|
706
|
-
const plugins = [];
|
|
707
|
-
appendPlugin(plugins, unocss({ presets: [presetUno()] }));
|
|
708
|
-
plugins.push(...frameworkPlugins);
|
|
709
|
-
return plugins;
|
|
710
|
-
}
|
|
711
|
-
function printReady(input) {
|
|
712
|
-
const { entry, framework, urls } = input;
|
|
713
|
-
const frameworkLabel = `${framework.name}@${framework.version}`;
|
|
714
|
-
console.log(`${pc2.cyan("vitepad")} ${pc2.green("ready")} ${pc2.green(frameworkLabel)}`);
|
|
715
|
-
console.log(` ${pc2.gray("entry")} ${pc2.gray(entry)}`);
|
|
716
|
-
if (framework.cacheDir) {
|
|
717
|
-
console.log(` ${pc2.gray("cache")} ${pc2.gray(framework.cacheDir)}`);
|
|
718
|
-
}
|
|
719
|
-
printUrls(urls);
|
|
720
|
-
}
|
|
721
|
-
function printUrls(urls) {
|
|
722
|
-
if (!urls) return;
|
|
723
|
-
for (const url of urls.local) {
|
|
724
|
-
console.log(` ${pc2.green("\u279C")} ${pc2.bold("Local:")} ${pc2.bold(url)}`);
|
|
725
|
-
}
|
|
726
|
-
for (const url of urls.network) {
|
|
727
|
-
console.log(` ${pc2.green("\u279C")} ${pc2.bold("Network:")} ${pc2.bold(url)}`);
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
function createVitepadLogger() {
|
|
731
|
-
const logger = createLogger("info");
|
|
732
|
-
const info = logger.info.bind(logger);
|
|
733
|
-
return {
|
|
734
|
-
...logger,
|
|
735
|
-
info(message, options) {
|
|
736
|
-
if (isNoisyViteInfo(message)) return;
|
|
737
|
-
info(message, options);
|
|
738
|
-
}
|
|
739
|
-
};
|
|
740
|
-
}
|
|
741
|
-
function isNoisyViteInfo(message) {
|
|
742
|
-
return /\[vite\]\s+\(client\)\s+(hmr update|page reload)\b/.test(stripAnsi(message));
|
|
743
|
-
}
|
|
744
|
-
function stripAnsi(value) {
|
|
745
|
-
return value.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, "");
|
|
746
|
-
}
|
|
747
|
-
function helpText(prefix) {
|
|
748
|
-
return `${prefix ? `${prefix}
|
|
749
|
-
|
|
750
|
-
` : ""}Usage:
|
|
751
|
-
vitepad <entry> [options]
|
|
752
|
-
|
|
753
|
-
Entries:
|
|
754
|
-
.js, .ts Treated as main entry files.
|
|
755
|
-
.jsx, .tsx, .vue, .svelte Treated as App component files.
|
|
756
|
-
|
|
757
|
-
Options:
|
|
758
|
-
-f, --framework <name> auto, react, preact, solid, vue, svelte, vanilla
|
|
759
|
-
Version specs are supported, e.g. react@18, vue@3.4.
|
|
760
|
-
--force-install Reinstall the selected framework cache.
|
|
761
|
-
-p, --port <number> Dev server port. Default: 8000
|
|
762
|
-
--host <host> Dev server host. Default: 0.0.0.0
|
|
763
|
-
--no-open Do not open the browser automatically.
|
|
764
|
-
-c, --config <file> Merge an extra Vite config file.
|
|
765
|
-
-h, --help Show help.
|
|
766
|
-
`;
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
// src/cli.ts
|
|
770
|
-
run(process.argv.slice(2)).catch((error) => {
|
|
41
|
+
function handleError(error) {
|
|
771
42
|
console.error(error instanceof Error ? error.message : error);
|
|
772
43
|
process.exit(1);
|
|
773
|
-
}
|
|
44
|
+
}
|
|
774
45
|
//# sourceMappingURL=cli.js.map
|
|
775
46
|
//# sourceMappingURL=cli.js.map
|