@buding0904/vitepad 0.3.0 → 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 +7 -9
- 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 -829
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +32 -825
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +114 -146
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +112 -144
- 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 +3 -4
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs3 from 'fs/promises';
|
|
2
2
|
import os from 'os';
|
|
3
|
-
import
|
|
3
|
+
import path3 from 'path';
|
|
4
4
|
import process2 from 'process';
|
|
5
5
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
6
6
|
import { createServer, mergeConfig, normalizePath, createLogger } from 'vite';
|
|
@@ -9,8 +9,8 @@ import { spawn } from 'child_process';
|
|
|
9
9
|
import { createRequire } from 'module';
|
|
10
10
|
|
|
11
11
|
// src/runtime/index.ts
|
|
12
|
-
var packageRoot =
|
|
13
|
-
var requireFromPackage = createRequire(
|
|
12
|
+
var packageRoot = path3.resolve(fileURLToPath(new URL("..", import.meta.url)));
|
|
13
|
+
var requireFromPackage = createRequire(path3.join(packageRoot, "package.json"));
|
|
14
14
|
var linkedPeerPackages = ["vite"];
|
|
15
15
|
var log = {
|
|
16
16
|
framework(requested, resolved) {
|
|
@@ -35,20 +35,17 @@ async function resolveFramework(spec, options) {
|
|
|
35
35
|
requested: "vanilla",
|
|
36
36
|
cacheStatus: "local",
|
|
37
37
|
aliases: [],
|
|
38
|
-
packageLinks: packageLinks(["
|
|
38
|
+
packageLinks: packageLinks(["unocss"]),
|
|
39
39
|
editorPackageLinks: []
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
const resolved = await resolveFrameworkPackages(spec.name, spec.version);
|
|
43
43
|
const cacheDir = frameworkCacheDir(spec.name, resolved.version);
|
|
44
|
-
const nodeModules =
|
|
44
|
+
const nodeModules = path3.join(cacheDir, "node_modules");
|
|
45
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
46
|
log.framework(`${spec.name}@${spec.version}`, `${spec.name}@${resolved.version}`);
|
|
50
47
|
const cacheStatus = options.forceInstall ? "miss" : installed ? "hit" : "miss";
|
|
51
|
-
if (
|
|
48
|
+
if (!installed) {
|
|
52
49
|
log.install(resolved.packages);
|
|
53
50
|
await installFrameworkCache(cacheDir, resolved.packages);
|
|
54
51
|
}
|
|
@@ -61,7 +58,7 @@ async function resolveFramework(spec, options) {
|
|
|
61
58
|
cacheDir,
|
|
62
59
|
aliases: [],
|
|
63
60
|
packageLinks: [
|
|
64
|
-
...packageLinks(["
|
|
61
|
+
...packageLinks(["unocss"]),
|
|
65
62
|
...packageLinks(nodeModules, frameworkRuntimePackages(spec.name))
|
|
66
63
|
],
|
|
67
64
|
editorPackageLinks: packageLinks(nodeModules, frameworkEditorPackages(spec.name))
|
|
@@ -260,33 +257,33 @@ function packageLinks(nodeModulesOrPackageNames, maybePackageNames) {
|
|
|
260
257
|
const packageNames = maybePackageNames ?? [];
|
|
261
258
|
return packageNames.map((packageName) => ({
|
|
262
259
|
name: packageName,
|
|
263
|
-
source:
|
|
260
|
+
source: path3.join(nodeModules, packageName)
|
|
264
261
|
}));
|
|
265
262
|
}
|
|
266
263
|
function resolveInstalledPackageDir(packageName) {
|
|
267
264
|
const packageJson = requireFromPackage.resolve(`${packageName}/package.json`);
|
|
268
|
-
return
|
|
265
|
+
return path3.dirname(packageJson);
|
|
269
266
|
}
|
|
270
267
|
function frameworkCacheDir(framework, version) {
|
|
271
|
-
const base = process.env.VITEPAD_CACHE_DIR || (process.env.XDG_CACHE_HOME ?
|
|
272
|
-
return
|
|
268
|
+
const base = process.env.VITEPAD_CACHE_DIR || (process.env.XDG_CACHE_HOME ? path3.join(process.env.XDG_CACHE_HOME, "vitepad") : path3.join(os.homedir(), ".cache", "vitepad"));
|
|
269
|
+
return path3.join(base, "frameworks", `${framework}-${sanitizeCacheKey(version)}`);
|
|
273
270
|
}
|
|
274
271
|
function sanitizeCacheKey(value) {
|
|
275
272
|
return value.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
276
273
|
}
|
|
277
274
|
async function isInstalled(cacheDir, packages) {
|
|
278
|
-
if (!await pathExists(
|
|
275
|
+
if (!await pathExists(path3.join(cacheDir, "node_modules"))) return false;
|
|
279
276
|
for (const pkg of packages) {
|
|
280
277
|
const { name } = splitPackageSpec(pkg);
|
|
281
|
-
if (!await pathExists(
|
|
278
|
+
if (!await pathExists(path3.join(cacheDir, "node_modules", name))) {
|
|
282
279
|
return false;
|
|
283
280
|
}
|
|
284
281
|
}
|
|
285
282
|
return true;
|
|
286
283
|
}
|
|
287
284
|
async function installFrameworkCache(cacheDir, packages) {
|
|
288
|
-
await
|
|
289
|
-
await
|
|
285
|
+
await fs3.mkdir(cacheDir, { recursive: true });
|
|
286
|
+
await fs3.writeFile(path3.join(cacheDir, "package.json"), JSON.stringify({
|
|
290
287
|
private: true,
|
|
291
288
|
type: "module",
|
|
292
289
|
dependencies: Object.fromEntries(packages.map((pkg) => packageToDependency(pkg)))
|
|
@@ -326,12 +323,12 @@ ${output.trim()}`));
|
|
|
326
323
|
});
|
|
327
324
|
}
|
|
328
325
|
async function linkPeerPackages(cacheDir) {
|
|
329
|
-
await
|
|
326
|
+
await fs3.mkdir(path3.join(cacheDir, "node_modules"), { recursive: true });
|
|
330
327
|
for (const packageName of linkedPeerPackages) {
|
|
331
328
|
try {
|
|
332
329
|
await linkPackage({
|
|
333
330
|
source: resolveInstalledPackageDir(packageName),
|
|
334
|
-
target:
|
|
331
|
+
target: path3.join(cacheDir, "node_modules", packageName)
|
|
335
332
|
});
|
|
336
333
|
} catch (error) {
|
|
337
334
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -341,16 +338,16 @@ ${message}`);
|
|
|
341
338
|
}
|
|
342
339
|
}
|
|
343
340
|
async function linkPackage(input) {
|
|
344
|
-
const source = await
|
|
345
|
-
const existing = await
|
|
341
|
+
const source = await fs3.realpath(input.source);
|
|
342
|
+
const existing = await fs3.lstat(input.target).catch(() => null);
|
|
346
343
|
if (existing) {
|
|
347
344
|
if (existing.isSymbolicLink()) {
|
|
348
|
-
const current = await
|
|
345
|
+
const current = await fs3.realpath(input.target).catch(() => null);
|
|
349
346
|
if (current === source) return;
|
|
350
347
|
}
|
|
351
|
-
await
|
|
348
|
+
await fs3.rm(input.target, { recursive: true, force: true });
|
|
352
349
|
}
|
|
353
|
-
await
|
|
350
|
+
await fs3.symlink(source, input.target, "dir");
|
|
354
351
|
}
|
|
355
352
|
function createProgress(label) {
|
|
356
353
|
const width = 18;
|
|
@@ -386,16 +383,37 @@ function splitPackageSpec(spec) {
|
|
|
386
383
|
return { name: spec.slice(0, at), version: spec.slice(at + 1) };
|
|
387
384
|
}
|
|
388
385
|
async function importCachePackage(cacheDir, packageName) {
|
|
389
|
-
const requireFromCache = createRequire(
|
|
386
|
+
const requireFromCache = createRequire(path3.join(cacheDir, "package.json"));
|
|
390
387
|
const resolved = requireFromCache.resolve(packageName);
|
|
391
388
|
return import(pathToFileURL(resolved).href);
|
|
392
389
|
}
|
|
393
390
|
async function pathExists(file) {
|
|
394
|
-
return
|
|
391
|
+
return fs3.access(file).then(() => true, () => false);
|
|
392
|
+
}
|
|
393
|
+
async function packageVersion() {
|
|
394
|
+
const packageJson = await readPackageJson();
|
|
395
|
+
if (typeof packageJson.version === "string") return packageJson.version;
|
|
396
|
+
throw new Error("Failed to read vitepad version from package.json.");
|
|
397
|
+
}
|
|
398
|
+
async function readPackageJson() {
|
|
399
|
+
let current = path3.dirname(fileURLToPath(import.meta.url));
|
|
400
|
+
while (true) {
|
|
401
|
+
const packageJsonPath = path3.join(current, "package.json");
|
|
402
|
+
const source = await fs3.readFile(packageJsonPath, "utf8").catch(() => null);
|
|
403
|
+
if (source) {
|
|
404
|
+
const packageJson = JSON.parse(source);
|
|
405
|
+
if (packageJson.name === "@buding0904/vitepad") return packageJson;
|
|
406
|
+
}
|
|
407
|
+
const parent = path3.dirname(current);
|
|
408
|
+
if (parent === current) {
|
|
409
|
+
throw new Error("Failed to locate vitepad package.json.");
|
|
410
|
+
}
|
|
411
|
+
current = parent;
|
|
412
|
+
}
|
|
395
413
|
}
|
|
396
414
|
|
|
397
415
|
// src/runtime/index.ts
|
|
398
|
-
var rootDir =
|
|
416
|
+
var rootDir = path3.resolve(fileURLToPath(new URL("..", import.meta.url)));
|
|
399
417
|
var supportedComponentExts = /* @__PURE__ */ new Set([".jsx", ".tsx", ".vue", ".svelte"]);
|
|
400
418
|
var supportedMainExts = /* @__PURE__ */ new Set([".js", ".mjs", ".cjs", ".ts", ".mts", ".cts"]);
|
|
401
419
|
var frameworkValues = /* @__PURE__ */ new Set(["auto", "react", "preact", "solid", "vue", "svelte", "vanilla"]);
|
|
@@ -405,17 +423,21 @@ async function run(argv) {
|
|
|
405
423
|
console.log(helpText());
|
|
406
424
|
return;
|
|
407
425
|
}
|
|
426
|
+
if (options.version) {
|
|
427
|
+
console.log(await packageVersion());
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
408
430
|
if (!options.entry) {
|
|
409
431
|
throw new Error(helpText("Missing entry file."));
|
|
410
432
|
}
|
|
411
|
-
const entry =
|
|
412
|
-
const stat = await
|
|
433
|
+
const entry = path3.resolve(process2.cwd(), options.entry);
|
|
434
|
+
const stat = await fs3.stat(entry).catch(() => null);
|
|
413
435
|
if (!stat?.isFile()) {
|
|
414
436
|
throw new Error(`Entry file does not exist: ${entry}`);
|
|
415
437
|
}
|
|
416
|
-
const extension =
|
|
438
|
+
const extension = path3.extname(entry).toLowerCase();
|
|
417
439
|
const mode = inferMode(extension);
|
|
418
|
-
const source = await
|
|
440
|
+
const source = await fs3.readFile(entry, "utf8");
|
|
419
441
|
const framework = inferFramework({
|
|
420
442
|
extension,
|
|
421
443
|
source,
|
|
@@ -423,15 +445,12 @@ async function run(argv) {
|
|
|
423
445
|
version: options.frameworkVersion
|
|
424
446
|
});
|
|
425
447
|
validateCombination({ mode, framework: framework.name, extension });
|
|
426
|
-
const resolvedFramework = await resolveFramework(framework, { forceInstall:
|
|
427
|
-
await setupEditorPackages(
|
|
428
|
-
const classTokens = await collectClassTokens(entry);
|
|
448
|
+
const resolvedFramework = await resolveFramework(framework, { forceInstall: false });
|
|
449
|
+
await setupEditorPackages(path3.dirname(entry), resolvedFramework.editorPackageLinks);
|
|
429
450
|
const workspace = await createWorkspace({
|
|
430
451
|
entry,
|
|
431
452
|
mode,
|
|
432
453
|
framework: resolvedFramework.name,
|
|
433
|
-
sourceDirs: uniquePaths([process2.cwd(), path2.dirname(entry)]),
|
|
434
|
-
classTokens,
|
|
435
454
|
packageLinks: resolvedFramework.packageLinks
|
|
436
455
|
});
|
|
437
456
|
const config = await loadUserConfig(options.config);
|
|
@@ -441,7 +460,8 @@ async function run(argv) {
|
|
|
441
460
|
server: {
|
|
442
461
|
host: options.host,
|
|
443
462
|
port: options.port,
|
|
444
|
-
|
|
463
|
+
strictPort: options.strictPort,
|
|
464
|
+
open: false,
|
|
445
465
|
watch: {
|
|
446
466
|
ignored: (file) => normalizePath(file).startsWith(`${normalizePath(workspace)}/`)
|
|
447
467
|
},
|
|
@@ -449,7 +469,7 @@ async function run(argv) {
|
|
|
449
469
|
allow: [
|
|
450
470
|
rootDir,
|
|
451
471
|
process2.cwd(),
|
|
452
|
-
|
|
472
|
+
path3.dirname(entry),
|
|
453
473
|
workspace,
|
|
454
474
|
...resolvedFramework.cacheDir ? [resolvedFramework.cacheDir] : []
|
|
455
475
|
]
|
|
@@ -461,13 +481,13 @@ async function run(argv) {
|
|
|
461
481
|
...resolvedFramework.aliases,
|
|
462
482
|
{
|
|
463
483
|
find: "@",
|
|
464
|
-
replacement:
|
|
484
|
+
replacement: path3.dirname(entry)
|
|
465
485
|
}
|
|
466
486
|
],
|
|
467
487
|
dedupe: frameworkDedupe(resolvedFramework.name)
|
|
468
488
|
},
|
|
469
489
|
optimizeDeps: {
|
|
470
|
-
entries: [
|
|
490
|
+
entries: [path3.join(workspace, "src/main.js")],
|
|
471
491
|
include: frameworkOptimizeDeps(resolvedFramework.name)
|
|
472
492
|
},
|
|
473
493
|
customLogger: createVitepadLogger()
|
|
@@ -489,22 +509,24 @@ function parseArgs(argv) {
|
|
|
489
509
|
const options = {
|
|
490
510
|
framework: "auto",
|
|
491
511
|
frameworkVersion: "latest",
|
|
492
|
-
forceInstall: false,
|
|
493
512
|
port: 8e3,
|
|
513
|
+
strictPort: false,
|
|
494
514
|
host: "0.0.0.0",
|
|
495
|
-
|
|
496
|
-
|
|
515
|
+
help: false,
|
|
516
|
+
version: false
|
|
497
517
|
};
|
|
498
518
|
for (let index = 0; index < argv.length; index += 1) {
|
|
499
519
|
const arg = argv[index];
|
|
500
520
|
if (arg === "-h" || arg === "--help") {
|
|
501
521
|
options.help = true;
|
|
522
|
+
} else if (arg === "-v" || arg === "--version") {
|
|
523
|
+
options.version = true;
|
|
502
524
|
} else if (arg === "--framework" || arg === "-f") {
|
|
503
525
|
Object.assign(options, parseFramework(readValue(argv, ++index, arg)));
|
|
504
526
|
} else if (arg.startsWith("--framework=")) {
|
|
505
527
|
Object.assign(options, parseFramework(arg.slice("--framework=".length)));
|
|
506
|
-
} else if (arg === "--
|
|
507
|
-
options.
|
|
528
|
+
} else if (arg === "--strictPort") {
|
|
529
|
+
options.strictPort = true;
|
|
508
530
|
} else if (arg === "--port" || arg === "-p") {
|
|
509
531
|
options.port = Number(readValue(argv, ++index, arg));
|
|
510
532
|
} else if (arg.startsWith("--port=")) {
|
|
@@ -513,10 +535,6 @@ function parseArgs(argv) {
|
|
|
513
535
|
options.host = readValue(argv, ++index, arg);
|
|
514
536
|
} else if (arg.startsWith("--host=")) {
|
|
515
537
|
options.host = arg.slice("--host=".length);
|
|
516
|
-
} else if (arg === "--no-open") {
|
|
517
|
-
options.open = false;
|
|
518
|
-
} else if (arg === "--open") {
|
|
519
|
-
options.open = "/";
|
|
520
538
|
} else if (arg === "--config" || arg === "-c") {
|
|
521
539
|
options.config = readValue(argv, ++index, arg);
|
|
522
540
|
} else if (arg.startsWith("--config=")) {
|
|
@@ -580,32 +598,29 @@ function validateCombination(input) {
|
|
|
580
598
|
}
|
|
581
599
|
}
|
|
582
600
|
async function createWorkspace(input) {
|
|
583
|
-
const workspace = await
|
|
584
|
-
const srcDir =
|
|
585
|
-
const nodeModulesDir =
|
|
586
|
-
await
|
|
587
|
-
await
|
|
601
|
+
const workspace = await fs3.mkdtemp(path3.join(await fs3.realpath(os.tmpdir()), "vitepad-"));
|
|
602
|
+
const srcDir = path3.join(workspace, "src");
|
|
603
|
+
const nodeModulesDir = path3.join(workspace, "node_modules");
|
|
604
|
+
await fs3.mkdir(srcDir, { recursive: true });
|
|
605
|
+
await fs3.mkdir(nodeModulesDir, { recursive: true });
|
|
588
606
|
await Promise.all([
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
fs2.writeFile(path2.join(srcDir, "main.js"), mainTemplate(input)),
|
|
607
|
+
fs3.writeFile(path3.join(workspace, "index.html"), htmlTemplate()),
|
|
608
|
+
fs3.writeFile(path3.join(srcDir, "main.js"), mainTemplate(input)),
|
|
592
609
|
...input.packageLinks.map((link) => linkWorkspacePackage(nodeModulesDir, link))
|
|
593
610
|
]);
|
|
594
611
|
return workspace;
|
|
595
612
|
}
|
|
596
613
|
async function linkWorkspacePackage(nodeModulesDir, link) {
|
|
597
|
-
const source = await
|
|
598
|
-
const target =
|
|
599
|
-
await
|
|
600
|
-
await
|
|
614
|
+
const source = await fs3.realpath(link.source);
|
|
615
|
+
const target = path3.join(nodeModulesDir, link.name);
|
|
616
|
+
await fs3.mkdir(path3.dirname(target), { recursive: true });
|
|
617
|
+
await fs3.symlink(source, target, "dir");
|
|
601
618
|
}
|
|
602
619
|
async function setupEditorPackages(projectDir, links) {
|
|
603
620
|
if (links.length === 0) return;
|
|
604
|
-
const nodeModulesDir = path2.join(projectDir, "node_modules");
|
|
605
|
-
await fs2.mkdir(nodeModulesDir, { recursive: true });
|
|
606
621
|
const linked = [];
|
|
607
622
|
for (const link of links) {
|
|
608
|
-
const result = await linkEditorPackage(
|
|
623
|
+
const result = await linkEditorPackage(projectDir, link);
|
|
609
624
|
if (result === "linked") {
|
|
610
625
|
linked.push(link.name);
|
|
611
626
|
}
|
|
@@ -614,81 +629,34 @@ async function setupEditorPackages(projectDir, links) {
|
|
|
614
629
|
console.log(`${pc2.cyan("vitepad")} ${pc2.green("editor")} linked ${linked.map((name) => pc2.cyan(name)).join(pc2.gray(", "))}`);
|
|
615
630
|
}
|
|
616
631
|
}
|
|
617
|
-
async function linkEditorPackage(
|
|
618
|
-
const source = await
|
|
619
|
-
const
|
|
620
|
-
|
|
621
|
-
const
|
|
632
|
+
async function linkEditorPackage(projectDir, link) {
|
|
633
|
+
const source = await fs3.realpath(link.source);
|
|
634
|
+
const existingResolvablePackage = await findResolvablePackage(projectDir, link.name);
|
|
635
|
+
if (existingResolvablePackage) return "skipped";
|
|
636
|
+
const nodeModulesDir = path3.join(projectDir, "node_modules");
|
|
637
|
+
const target = path3.join(nodeModulesDir, link.name);
|
|
638
|
+
await fs3.mkdir(path3.dirname(target), { recursive: true });
|
|
639
|
+
const existing = await fs3.lstat(target).catch(() => null);
|
|
622
640
|
if (existing) {
|
|
623
641
|
if (existing.isSymbolicLink()) {
|
|
624
|
-
const current = await
|
|
642
|
+
const current = await fs3.realpath(target).catch(() => null);
|
|
625
643
|
if (current === source) return "skipped";
|
|
626
644
|
}
|
|
627
645
|
return "skipped";
|
|
628
646
|
}
|
|
629
|
-
await
|
|
647
|
+
await fs3.symlink(source, target, "dir");
|
|
630
648
|
return "linked";
|
|
631
649
|
}
|
|
632
|
-
function
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
].join("\n");
|
|
642
|
-
}
|
|
643
|
-
async function collectClassTokens(entry) {
|
|
644
|
-
const visited = /* @__PURE__ */ new Set();
|
|
645
|
-
const tokens = /* @__PURE__ */ new Set();
|
|
646
|
-
await collectFileClassTokens(entry, visited, tokens);
|
|
647
|
-
return [...tokens].sort();
|
|
648
|
-
}
|
|
649
|
-
async function collectFileClassTokens(file, visited, tokens) {
|
|
650
|
-
const resolved = path2.resolve(file);
|
|
651
|
-
if (visited.has(resolved)) return;
|
|
652
|
-
visited.add(resolved);
|
|
653
|
-
const source = await fs2.readFile(resolved, "utf8").catch(() => "");
|
|
654
|
-
for (const match of source.matchAll(/\b(?:class|className)\s*=\s*(?:"([^"]+)"|'([^']+)'|{`([^`]+)`})/g)) {
|
|
655
|
-
const value = match[1] || match[2] || match[3] || "";
|
|
656
|
-
for (const token of value.split(/\s+/)) {
|
|
657
|
-
if (token && !/[${}]/.test(token)) tokens.add(token);
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
for (const specifier of localImportSpecifiers(source)) {
|
|
661
|
-
const imported = await resolveLocalImport(resolved, specifier);
|
|
662
|
-
if (imported) {
|
|
663
|
-
await collectFileClassTokens(imported, visited, tokens);
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
function localImportSpecifiers(source) {
|
|
668
|
-
const specifiers = /* @__PURE__ */ new Set();
|
|
669
|
-
for (const match of source.matchAll(/\bimport\s+(?:[^'"]+?\s+from\s+)?['"]([^'"]+)['"]/g)) {
|
|
670
|
-
if (isLocalSpecifier(match[1])) specifiers.add(match[1]);
|
|
671
|
-
}
|
|
672
|
-
for (const match of source.matchAll(/\bexport\s+[^'"]+?\s+from\s+['"]([^'"]+)['"]/g)) {
|
|
673
|
-
if (isLocalSpecifier(match[1])) specifiers.add(match[1]);
|
|
674
|
-
}
|
|
675
|
-
return [...specifiers];
|
|
676
|
-
}
|
|
677
|
-
function isLocalSpecifier(specifier) {
|
|
678
|
-
return specifier.startsWith("./") || specifier.startsWith("../");
|
|
679
|
-
}
|
|
680
|
-
async function resolveLocalImport(importer, specifier) {
|
|
681
|
-
const base = path2.resolve(path2.dirname(importer), specifier);
|
|
682
|
-
const candidates = [
|
|
683
|
-
base,
|
|
684
|
-
...[".ts", ".tsx", ".js", ".jsx", ".vue", ".svelte"].map((ext) => `${base}${ext}`),
|
|
685
|
-
...["index.ts", "index.tsx", "index.js", "index.jsx", "index.vue", "index.svelte"].map((file) => path2.join(base, file))
|
|
686
|
-
];
|
|
687
|
-
for (const candidate of candidates) {
|
|
688
|
-
const stat = await fs2.stat(candidate).catch(() => null);
|
|
689
|
-
if (stat?.isFile()) return candidate;
|
|
650
|
+
async function findResolvablePackage(fromDir, packageName) {
|
|
651
|
+
let current = path3.resolve(fromDir);
|
|
652
|
+
while (true) {
|
|
653
|
+
const candidate = path3.join(current, "node_modules", packageName);
|
|
654
|
+
const existing = await fs3.lstat(candidate).catch(() => null);
|
|
655
|
+
if (existing) return candidate;
|
|
656
|
+
const parent = path3.dirname(current);
|
|
657
|
+
if (parent === current) return null;
|
|
658
|
+
current = parent;
|
|
690
659
|
}
|
|
691
|
-
return void 0;
|
|
692
660
|
}
|
|
693
661
|
function htmlTemplate() {
|
|
694
662
|
return `<!doctype html>
|
|
@@ -709,13 +677,13 @@ function mainTemplate(input) {
|
|
|
709
677
|
const { entry, mode, framework } = input;
|
|
710
678
|
const importPath = `/@fs/${normalizePath(entry)}`;
|
|
711
679
|
if (mode === "main") {
|
|
712
|
-
return `import '
|
|
680
|
+
return `import 'virtual:uno.css'
|
|
713
681
|
import ${JSON.stringify(importPath)}
|
|
714
682
|
`;
|
|
715
683
|
}
|
|
716
684
|
switch (framework) {
|
|
717
685
|
case "react":
|
|
718
|
-
return `import '
|
|
686
|
+
return `import 'virtual:uno.css'
|
|
719
687
|
import React from 'react'
|
|
720
688
|
import { createRoot } from 'react-dom/client'
|
|
721
689
|
import App from ${JSON.stringify(importPath)}
|
|
@@ -723,28 +691,28 @@ import App from ${JSON.stringify(importPath)}
|
|
|
723
691
|
createRoot(document.getElementById('root')).render(React.createElement(App))
|
|
724
692
|
`;
|
|
725
693
|
case "preact":
|
|
726
|
-
return `import '
|
|
694
|
+
return `import 'virtual:uno.css'
|
|
727
695
|
import { h, render } from 'preact'
|
|
728
696
|
import App from ${JSON.stringify(importPath)}
|
|
729
697
|
|
|
730
698
|
render(h(App, null), document.getElementById('root'))
|
|
731
699
|
`;
|
|
732
700
|
case "solid":
|
|
733
|
-
return `import '
|
|
701
|
+
return `import 'virtual:uno.css'
|
|
734
702
|
import { render } from 'solid-js/web'
|
|
735
703
|
import App from ${JSON.stringify(importPath)}
|
|
736
704
|
|
|
737
705
|
render(() => App({}), document.getElementById('root'))
|
|
738
706
|
`;
|
|
739
707
|
case "vue":
|
|
740
|
-
return `import '
|
|
708
|
+
return `import 'virtual:uno.css'
|
|
741
709
|
import { createApp } from 'vue'
|
|
742
710
|
import App from ${JSON.stringify(importPath)}
|
|
743
711
|
|
|
744
712
|
createApp(App).mount('#root')
|
|
745
713
|
`;
|
|
746
714
|
case "svelte":
|
|
747
|
-
return `import '
|
|
715
|
+
return `import 'virtual:uno.css'
|
|
748
716
|
import { mount } from 'svelte'
|
|
749
717
|
import App from ${JSON.stringify(importPath)}
|
|
750
718
|
|
|
@@ -758,17 +726,18 @@ export default app
|
|
|
758
726
|
}
|
|
759
727
|
async function loadUserConfig(configFile) {
|
|
760
728
|
if (!configFile) return {};
|
|
761
|
-
const resolved =
|
|
729
|
+
const resolved = path3.resolve(process2.cwd(), configFile);
|
|
762
730
|
const configModule = await import(pathToFileURL(resolved).href);
|
|
763
731
|
return configModule.default ?? configModule;
|
|
764
732
|
}
|
|
765
733
|
async function loadPlugins(framework) {
|
|
766
|
-
const [{ default:
|
|
767
|
-
import('
|
|
734
|
+
const [{ default: unocss }, { presetUno }, frameworkPlugins] = await Promise.all([
|
|
735
|
+
import('unocss/vite'),
|
|
736
|
+
import('unocss'),
|
|
768
737
|
loadFrameworkPlugins(framework)
|
|
769
738
|
]);
|
|
770
739
|
const plugins = [];
|
|
771
|
-
appendPlugin(plugins,
|
|
740
|
+
appendPlugin(plugins, unocss({ presets: [presetUno()] }));
|
|
772
741
|
plugins.push(...frameworkPlugins);
|
|
773
742
|
return plugins;
|
|
774
743
|
}
|
|
@@ -821,10 +790,9 @@ Entries:
|
|
|
821
790
|
Options:
|
|
822
791
|
-f, --framework <name> auto, react, preact, solid, vue, svelte, vanilla
|
|
823
792
|
Version specs are supported, e.g. react@18, vue@3.4.
|
|
824
|
-
--force-install Reinstall the selected framework cache.
|
|
825
793
|
-p, --port <number> Dev server port. Default: 8000
|
|
794
|
+
--strictPort Exit if the configured port is already in use.
|
|
826
795
|
--host <host> Dev server host. Default: 0.0.0.0
|
|
827
|
-
--no-open Do not open the browser automatically.
|
|
828
796
|
-c, --config <file> Merge an extra Vite config file.
|
|
829
797
|
-h, --help Show help.
|
|
830
798
|
`;
|