@b9g/shovel 0.2.0-beta.1 → 0.2.0-beta.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/bin/cli.js +79 -25
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -24,10 +24,61 @@ import * as Platform from "@b9g/platform";
|
|
|
24
24
|
// src/esbuild/watcher.ts
|
|
25
25
|
import * as ESBuild from "esbuild";
|
|
26
26
|
import { watch } from "fs";
|
|
27
|
-
import { resolve, dirname, join } from "path";
|
|
27
|
+
import { resolve, dirname as dirname2, join } from "path";
|
|
28
28
|
import { readFileSync } from "fs";
|
|
29
29
|
import { mkdir } from "fs/promises";
|
|
30
30
|
import { assetsPlugin } from "@b9g/assets/plugin";
|
|
31
|
+
|
|
32
|
+
// src/esbuild/import-meta-plugin.ts
|
|
33
|
+
import { readFile } from "fs/promises";
|
|
34
|
+
import { dirname } from "path";
|
|
35
|
+
import { pathToFileURL } from "url";
|
|
36
|
+
function importMetaPlugin() {
|
|
37
|
+
return {
|
|
38
|
+
name: "import-meta-transform",
|
|
39
|
+
setup(build3) {
|
|
40
|
+
build3.onLoad({ filter: /\.[jt]sx?$/, namespace: "file" }, async (args) => {
|
|
41
|
+
if (args.path.includes("node_modules")) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const contents = await readFile(args.path, "utf8");
|
|
45
|
+
if (!contents.includes("import.meta.url") && !contents.includes("import.meta.dirname") && !contents.includes("import.meta.filename")) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
const fileUrl = pathToFileURL(args.path).href;
|
|
49
|
+
const fileDirname = dirname(args.path);
|
|
50
|
+
const fileFilename = args.path;
|
|
51
|
+
let transformed = contents;
|
|
52
|
+
transformed = transformed.replace(
|
|
53
|
+
/\bimport\.meta\.url\b/g,
|
|
54
|
+
JSON.stringify(fileUrl)
|
|
55
|
+
);
|
|
56
|
+
transformed = transformed.replace(
|
|
57
|
+
/\bimport\.meta\.dirname\b/g,
|
|
58
|
+
JSON.stringify(fileDirname)
|
|
59
|
+
);
|
|
60
|
+
transformed = transformed.replace(
|
|
61
|
+
/\bimport\.meta\.filename\b/g,
|
|
62
|
+
JSON.stringify(fileFilename)
|
|
63
|
+
);
|
|
64
|
+
const ext = args.path.split(".").pop();
|
|
65
|
+
let loader = "js";
|
|
66
|
+
if (ext === "ts")
|
|
67
|
+
loader = "ts";
|
|
68
|
+
else if (ext === "tsx")
|
|
69
|
+
loader = "tsx";
|
|
70
|
+
else if (ext === "jsx")
|
|
71
|
+
loader = "jsx";
|
|
72
|
+
return {
|
|
73
|
+
contents: transformed,
|
|
74
|
+
loader
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/esbuild/watcher.ts
|
|
31
82
|
import { getLogger } from "@logtape/logtape";
|
|
32
83
|
var logger = getLogger(["watcher"]);
|
|
33
84
|
var Watcher = class {
|
|
@@ -44,7 +95,7 @@ var Watcher = class {
|
|
|
44
95
|
async start() {
|
|
45
96
|
const entryPath = resolve(this.#options.entrypoint);
|
|
46
97
|
await this.#build();
|
|
47
|
-
const watchDir =
|
|
98
|
+
const watchDir = dirname2(entryPath);
|
|
48
99
|
logger.info("Watching for changes", { watchDir });
|
|
49
100
|
this.#watcher = watch(
|
|
50
101
|
watchDir,
|
|
@@ -88,7 +139,7 @@ var Watcher = class {
|
|
|
88
139
|
const version = Date.now();
|
|
89
140
|
const initialCwd = process.cwd();
|
|
90
141
|
let workspaceRoot = initialCwd;
|
|
91
|
-
while (workspaceRoot !==
|
|
142
|
+
while (workspaceRoot !== dirname2(workspaceRoot)) {
|
|
92
143
|
try {
|
|
93
144
|
const packageJSON = JSON.parse(
|
|
94
145
|
readFileSync(resolve(workspaceRoot, "package.json"), "utf8")
|
|
@@ -98,9 +149,9 @@ var Watcher = class {
|
|
|
98
149
|
}
|
|
99
150
|
} catch {
|
|
100
151
|
}
|
|
101
|
-
workspaceRoot =
|
|
152
|
+
workspaceRoot = dirname2(workspaceRoot);
|
|
102
153
|
}
|
|
103
|
-
if (workspaceRoot ===
|
|
154
|
+
if (workspaceRoot === dirname2(workspaceRoot)) {
|
|
104
155
|
workspaceRoot = initialCwd;
|
|
105
156
|
}
|
|
106
157
|
logger.info("Building", { entryPath });
|
|
@@ -117,6 +168,7 @@ var Watcher = class {
|
|
|
117
168
|
packages: "external",
|
|
118
169
|
absWorkingDir: workspaceRoot,
|
|
119
170
|
plugins: [
|
|
171
|
+
importMetaPlugin(),
|
|
120
172
|
assetsPlugin({
|
|
121
173
|
outputDir: `${outputDir}/assets`,
|
|
122
174
|
manifest: `${outputDir}/server/asset-manifest.json`
|
|
@@ -303,8 +355,8 @@ async function infoCommand() {
|
|
|
303
355
|
|
|
304
356
|
// src/commands/build.ts
|
|
305
357
|
import * as ESBuild2 from "esbuild";
|
|
306
|
-
import { resolve as resolve2, join as join2, dirname as
|
|
307
|
-
import { mkdir as mkdir2, readFile, writeFile } from "fs/promises";
|
|
358
|
+
import { resolve as resolve2, join as join2, dirname as dirname3 } from "path";
|
|
359
|
+
import { mkdir as mkdir2, readFile as readFile2, writeFile } from "fs/promises";
|
|
308
360
|
import { fileURLToPath } from "url";
|
|
309
361
|
import { assetsPlugin as assetsPlugin2 } from "@b9g/assets/plugin";
|
|
310
362
|
import { configure as configure2, getConsoleSink as getConsoleSink2, getLogger as getLogger5 } from "@logtape/logtape";
|
|
@@ -386,7 +438,7 @@ async function initializeBuild({
|
|
|
386
438
|
const entryPath = resolve2(entrypoint);
|
|
387
439
|
const outputDir = resolve2(outDir);
|
|
388
440
|
try {
|
|
389
|
-
const stats = await
|
|
441
|
+
const stats = await readFile2(entryPath, "utf8");
|
|
390
442
|
if (stats.length === 0) {
|
|
391
443
|
logger5.warn("Entry point is empty", { entryPath });
|
|
392
444
|
}
|
|
@@ -429,37 +481,37 @@ async function initializeBuild({
|
|
|
429
481
|
}
|
|
430
482
|
async function findWorkspaceRoot() {
|
|
431
483
|
let workspaceRoot = process.cwd();
|
|
432
|
-
while (workspaceRoot !==
|
|
484
|
+
while (workspaceRoot !== dirname3(workspaceRoot)) {
|
|
433
485
|
try {
|
|
434
486
|
const packageJSON = JSON.parse(
|
|
435
|
-
await
|
|
487
|
+
await readFile2(resolve2(workspaceRoot, "package.json"), "utf8")
|
|
436
488
|
);
|
|
437
489
|
if (packageJSON.workspaces) {
|
|
438
490
|
return workspaceRoot;
|
|
439
491
|
}
|
|
440
492
|
} catch {
|
|
441
493
|
}
|
|
442
|
-
workspaceRoot =
|
|
494
|
+
workspaceRoot = dirname3(workspaceRoot);
|
|
443
495
|
}
|
|
444
496
|
return workspaceRoot;
|
|
445
497
|
}
|
|
446
498
|
async function findShovelPackageRoot() {
|
|
447
|
-
let currentDir =
|
|
499
|
+
let currentDir = dirname3(fileURLToPath(import.meta.url));
|
|
448
500
|
let packageRoot = currentDir;
|
|
449
|
-
while (packageRoot !==
|
|
501
|
+
while (packageRoot !== dirname3(packageRoot)) {
|
|
450
502
|
try {
|
|
451
503
|
const packageJSONPath = join2(packageRoot, "package.json");
|
|
452
|
-
const content = await
|
|
504
|
+
const content = await readFile2(packageJSONPath, "utf8");
|
|
453
505
|
const pkg2 = JSON.parse(content);
|
|
454
506
|
if (pkg2.name === "@b9g/shovel" || pkg2.name === "shovel") {
|
|
455
507
|
if (packageRoot.endsWith("/dist") || packageRoot.endsWith("\\dist")) {
|
|
456
|
-
return
|
|
508
|
+
return dirname3(packageRoot);
|
|
457
509
|
}
|
|
458
510
|
return packageRoot;
|
|
459
511
|
}
|
|
460
512
|
} catch {
|
|
461
513
|
}
|
|
462
|
-
packageRoot =
|
|
514
|
+
packageRoot = dirname3(packageRoot);
|
|
463
515
|
}
|
|
464
516
|
return currentDir;
|
|
465
517
|
}
|
|
@@ -488,10 +540,11 @@ async function createBuildConfig({
|
|
|
488
540
|
target: BUILD_DEFAULTS.target,
|
|
489
541
|
platform: "node",
|
|
490
542
|
outfile: join2(serverDir, "server.js"),
|
|
491
|
-
absWorkingDir: workspaceRoot ||
|
|
543
|
+
absWorkingDir: workspaceRoot || dirname3(entryPath),
|
|
492
544
|
mainFields: ["module", "main"],
|
|
493
545
|
conditions: ["import", "module"],
|
|
494
546
|
plugins: [
|
|
547
|
+
importMetaPlugin(),
|
|
495
548
|
assetsPlugin2({
|
|
496
549
|
outputDir: assetsDir,
|
|
497
550
|
manifest: join2(serverDir, "asset-manifest.json")
|
|
@@ -555,10 +608,11 @@ async function createBuildConfig({
|
|
|
555
608
|
serverDir,
|
|
556
609
|
isCloudflare ? "server.js" : BUILD_DEFAULTS.outputFile
|
|
557
610
|
),
|
|
558
|
-
absWorkingDir: workspaceRoot ||
|
|
611
|
+
absWorkingDir: workspaceRoot || dirname3(entryPath),
|
|
559
612
|
mainFields: ["module", "main"],
|
|
560
613
|
conditions: ["import", "module"],
|
|
561
614
|
plugins: isCloudflare ? [
|
|
615
|
+
importMetaPlugin(),
|
|
562
616
|
assetsPlugin2({
|
|
563
617
|
outputDir: assetsDir,
|
|
564
618
|
manifest: join2(serverDir, "asset-manifest.json")
|
|
@@ -600,20 +654,20 @@ import "${userEntryPath}";
|
|
|
600
654
|
return await createWorkerEntry(userEntryPath, workerCount, platform);
|
|
601
655
|
}
|
|
602
656
|
async function createWorkerEntry(userEntryPath, workerCount, platform) {
|
|
603
|
-
let currentDir =
|
|
657
|
+
let currentDir = dirname3(fileURLToPath(import.meta.url));
|
|
604
658
|
let packageRoot = currentDir;
|
|
605
|
-
while (packageRoot !==
|
|
659
|
+
while (packageRoot !== dirname3(packageRoot)) {
|
|
606
660
|
try {
|
|
607
661
|
const packageJSONPath = join2(packageRoot, "package.json");
|
|
608
|
-
await
|
|
662
|
+
await readFile2(packageJSONPath, "utf8");
|
|
609
663
|
break;
|
|
610
664
|
} catch {
|
|
611
|
-
packageRoot =
|
|
665
|
+
packageRoot = dirname3(packageRoot);
|
|
612
666
|
}
|
|
613
667
|
}
|
|
614
668
|
let templatePath = join2(packageRoot, "src/worker-entry.ts");
|
|
615
669
|
try {
|
|
616
|
-
await
|
|
670
|
+
await readFile2(templatePath, "utf8");
|
|
617
671
|
} catch {
|
|
618
672
|
templatePath = join2(packageRoot, "src/worker-entry.js");
|
|
619
673
|
}
|
|
@@ -642,10 +696,10 @@ async function logBundleAnalysis(metafile) {
|
|
|
642
696
|
}
|
|
643
697
|
}
|
|
644
698
|
async function generatePackageJSON({ serverDir, platform, verbose, entryPath }) {
|
|
645
|
-
const entryDir =
|
|
699
|
+
const entryDir = dirname3(entryPath);
|
|
646
700
|
const sourcePackageJsonPath = resolve2(entryDir, "package.json");
|
|
647
701
|
try {
|
|
648
|
-
const packageJSONContent = await
|
|
702
|
+
const packageJSONContent = await readFile2(sourcePackageJsonPath, "utf8");
|
|
649
703
|
try {
|
|
650
704
|
JSON.parse(packageJSONContent);
|
|
651
705
|
} catch (parseError) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@b9g/shovel",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.2",
|
|
4
4
|
"description": "ServiceWorker-first universal deployment platform. Write ServiceWorker apps once, deploy anywhere (Node/Bun/Cloudflare). Registry-based multi-app orchestration.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"bin": {
|