@blinkk/root 1.0.0-rc.37 → 1.0.0-rc.39

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.
@@ -6,6 +6,7 @@ import {
6
6
  } from "./chunk-TZAHHHA4.js";
7
7
  import {
8
8
  bundleRootConfig,
9
+ copyDir,
9
10
  copyGlob,
10
11
  createViteServer,
11
12
  dirExists,
@@ -18,8 +19,9 @@ import {
18
19
  loadRootConfig,
19
20
  makeDir,
20
21
  rmDir,
21
- writeFile
22
- } from "./chunk-L2UBQKLP.js";
22
+ writeFile,
23
+ writeJson
24
+ } from "./chunk-ODXHLJOY.js";
23
25
  import {
24
26
  configureServerPlugins,
25
27
  getVitePlugins
@@ -672,8 +674,164 @@ function formatParams(params) {
672
674
  }).join("\n");
673
675
  }
674
676
 
677
+ // src/cli/create-package.ts
678
+ import { promises as fs3 } from "node:fs";
679
+ import path4 from "node:path";
680
+ import { build as esbuild } from "esbuild";
681
+ async function createPackage(rootProjectDir, options) {
682
+ const mode = options?.mode || "production";
683
+ process.env.NODE_ENV = mode;
684
+ const rootDir = path4.resolve(rootProjectDir || process.cwd());
685
+ const distDir = path4.join(rootDir, "dist");
686
+ const target = options?.target || await getDefaultTarget(rootDir);
687
+ const outDir = path4.resolve(options?.out || target || "out");
688
+ await build(rootProjectDir, { ssrOnly: true, mode });
689
+ await rmDir(outDir);
690
+ await makeDir(outDir);
691
+ await copyDir(distDir, path4.resolve(outDir, "dist"));
692
+ const collectionsDir = path4.resolve(rootDir, "collections");
693
+ if (await dirExists(collectionsDir)) {
694
+ await copyDir(collectionsDir, path4.join(outDir, "collections"));
695
+ }
696
+ const packageJson = await generatePackageJson(rootDir);
697
+ if (target === "appengine") {
698
+ await onAppEngine({ rootDir, packageJson, outDir });
699
+ } else if (target === "firebase") {
700
+ await onFirebase({ rootDir, packageJson, outDir });
701
+ }
702
+ await writeJson(path4.resolve(outDir, "package.json"), packageJson);
703
+ console.log("done!");
704
+ console.log(`saved package to ${outDir}`);
705
+ }
706
+ async function getDefaultTarget(rootDir) {
707
+ const firebaseConfigPath = path4.resolve(rootDir, "firebase.json");
708
+ if (await fileExists(firebaseConfigPath)) {
709
+ return "firebase";
710
+ }
711
+ const appEngineConfigPath = path4.resolve(rootDir, "app.yaml");
712
+ if (await fileExists(appEngineConfigPath)) {
713
+ return "appengine";
714
+ }
715
+ return null;
716
+ }
717
+ async function generatePackageJson(rootDir) {
718
+ const packageJson = await loadJson(
719
+ path4.resolve(rootDir, "package.json")
720
+ );
721
+ if (packageJson.peerDependencies) {
722
+ await updatePeerDepsFromWorkspace(rootDir, packageJson);
723
+ }
724
+ return packageJson;
725
+ }
726
+ async function updatePeerDepsFromWorkspace(rootDir, packageJson) {
727
+ const requiredPeerDeps = getRequiredPeerDeps(packageJson);
728
+ if (requiredPeerDeps.length === 0) {
729
+ return;
730
+ }
731
+ const workspaceRoot = await findWorkspaceRoot(rootDir);
732
+ if (!workspaceRoot) {
733
+ return;
734
+ }
735
+ const workspacePackageJsonPath = path4.resolve(workspaceRoot, "package.json");
736
+ const workspacePackageJson = await loadJson(
737
+ workspacePackageJsonPath
738
+ );
739
+ const workspaceDeps = workspacePackageJson.dependencies || {};
740
+ function setDep(name, value) {
741
+ if (!packageJson.dependencies) {
742
+ packageJson.dependencies = {};
743
+ }
744
+ packageJson.dependencies[name] = value;
745
+ }
746
+ for (const peerDep of requiredPeerDeps) {
747
+ const workspaceDepVersion = workspaceDeps[peerDep];
748
+ if (workspaceDepVersion) {
749
+ setDep(peerDep, workspaceDepVersion);
750
+ } else {
751
+ console.warn(
752
+ `could not find peer dep "${peerDep}" in workspace "${workspacePackageJsonPath}"`
753
+ );
754
+ }
755
+ }
756
+ }
757
+ async function findWorkspaceRoot(rootDir) {
758
+ const parentDir = path4.dirname(rootDir);
759
+ if (parentDir === "/") {
760
+ return null;
761
+ }
762
+ const pnpmWorkspaceFile = path4.resolve(parentDir, "pnpm-workspace.yaml");
763
+ if (await fileExists(pnpmWorkspaceFile)) {
764
+ return parentDir;
765
+ }
766
+ const packageJsonPath = path4.resolve(parentDir, "package.json");
767
+ if (await fileExists(packageJsonPath)) {
768
+ const parentPackageJson = await loadJson(packageJsonPath);
769
+ if (parentPackageJson && parentPackageJson.workspaces) {
770
+ return parentDir;
771
+ }
772
+ }
773
+ return findWorkspaceRoot(parentDir);
774
+ }
775
+ function getRequiredPeerDeps(packageJson) {
776
+ const requiredPeerDeps = [];
777
+ const peerDeps = packageJson.peerDependencies || {};
778
+ const peerDepsMeta = packageJson.peerDependenciesMeta || {};
779
+ for (const key in peerDeps) {
780
+ const meta = peerDepsMeta[key];
781
+ if (!meta?.optional) {
782
+ requiredPeerDeps.push(key);
783
+ }
784
+ }
785
+ return requiredPeerDeps;
786
+ }
787
+ async function onAppEngine(options) {
788
+ const { rootDir, outDir } = options;
789
+ const configPath = path4.resolve(rootDir, "app.yaml");
790
+ if (await fileExists(configPath)) {
791
+ await fs3.copyFile(configPath, path4.resolve(outDir, "app.yaml"));
792
+ }
793
+ }
794
+ async function onFirebase(options) {
795
+ const { rootDir, outDir } = options;
796
+ const outBasename = path4.basename(outDir);
797
+ if (outBasename === "functions") {
798
+ const indexTsFile = path4.resolve(rootDir, "index.ts");
799
+ if (await fileExists(indexTsFile)) {
800
+ await bundleTsFile(indexTsFile, path4.resolve(outDir, "index.js"));
801
+ }
802
+ }
803
+ }
804
+ async function bundleTsFile(srcPath, outPath) {
805
+ await esbuild({
806
+ entryPoints: [srcPath],
807
+ bundle: true,
808
+ minify: true,
809
+ platform: "node",
810
+ outfile: outPath,
811
+ sourcemap: "inline",
812
+ metafile: true,
813
+ format: "esm",
814
+ plugins: [
815
+ {
816
+ name: "externalize-deps",
817
+ setup(build2) {
818
+ build2.onResolve({ filter: /.*/ }, (args) => {
819
+ const id = args.path;
820
+ if (id[0] !== "." && !path4.isAbsolute(id)) {
821
+ return {
822
+ external: true
823
+ };
824
+ }
825
+ return null;
826
+ });
827
+ }
828
+ }
829
+ ]
830
+ });
831
+ }
832
+
675
833
  // src/cli/dev.ts
676
- import path5 from "node:path";
834
+ import path6 from "node:path";
677
835
  import { fileURLToPath as fileURLToPath2 } from "node:url";
678
836
  import cookieParser from "cookie-parser";
679
837
  import { default as express } from "express";
@@ -750,7 +908,7 @@ function replaceParams(urlPathFormat, params) {
750
908
  }
751
909
 
752
910
  // src/render/asset-map/dev-asset-map.ts
753
- import path4 from "node:path";
911
+ import path5 from "node:path";
754
912
  import { searchForWorkspaceRoot as searchForWorkspaceRoot2 } from "vite";
755
913
  var DevServerAssetMap = class {
756
914
  constructor(rootConfig, moduleGraph) {
@@ -758,7 +916,7 @@ var DevServerAssetMap = class {
758
916
  this.moduleGraph = moduleGraph;
759
917
  }
760
918
  async get(src) {
761
- const file = path4.resolve(this.rootConfig.rootDir, src);
919
+ const file = path5.resolve(this.rootConfig.rootDir, src);
762
920
  const viteModules = this.moduleGraph.getModulesByFile(file);
763
921
  if (viteModules && viteModules.size > 0) {
764
922
  const [viteModule] = viteModules;
@@ -790,7 +948,7 @@ var DevServerAssetMap = class {
790
948
  return null;
791
949
  }
792
950
  filePathToSrc(file) {
793
- return path4.relative(this.rootConfig.rootDir, file);
951
+ return path5.relative(this.rootConfig.rootDir, file);
794
952
  }
795
953
  };
796
954
  var DevServerAsset = class _DevServerAsset {
@@ -827,7 +985,7 @@ var DevServerAsset = class _DevServerAsset {
827
985
  return;
828
986
  }
829
987
  visited.add(asset.moduleId);
830
- const parts = path4.parse(asset.assetUrl);
988
+ const parts = path5.parse(asset.assetUrl);
831
989
  if ([".js", ".jsx", ".ts", ".tsx"].includes(parts.ext) && asset.moduleId.includes("/elements/")) {
832
990
  urls.add(asset.assetUrl);
833
991
  }
@@ -854,7 +1012,7 @@ var DevServerAsset = class _DevServerAsset {
854
1012
  }
855
1013
  visited.add(asset.assetUrl);
856
1014
  if (asset.src.endsWith(".scss")) {
857
- const parts = path4.parse(asset.src);
1015
+ const parts = path5.parse(asset.src);
858
1016
  if (!parts.name.startsWith("_")) {
859
1017
  urls.add(asset.assetUrl);
860
1018
  }
@@ -921,10 +1079,10 @@ function randString(len) {
921
1079
  }
922
1080
 
923
1081
  // src/cli/dev.ts
924
- var __dirname2 = path5.dirname(fileURLToPath2(import.meta.url));
1082
+ var __dirname2 = path6.dirname(fileURLToPath2(import.meta.url));
925
1083
  async function dev(rootProjectDir, options) {
926
1084
  process.env.NODE_ENV = "development";
927
- const rootDir = path5.resolve(rootProjectDir || process.cwd());
1085
+ const rootDir = path6.resolve(rootProjectDir || process.cwd());
928
1086
  const defaultPort = parseInt(process.env.PORT || "4007");
929
1087
  const host = options?.host || "localhost";
930
1088
  const port = await findOpenPort(defaultPort, defaultPort + 10);
@@ -942,7 +1100,7 @@ async function dev(rootProjectDir, options) {
942
1100
  server.listen(port, host);
943
1101
  }
944
1102
  async function createDevServer(options) {
945
- const rootDir = path5.resolve(options?.rootDir || process.cwd());
1103
+ const rootDir = path6.resolve(options?.rootDir || process.cwd());
946
1104
  const rootConfig = await loadRootConfig(rootDir, { command: "dev" });
947
1105
  const port = options?.port;
948
1106
  const server = express();
@@ -974,7 +1132,7 @@ async function createDevServer(options) {
974
1132
  if (rootConfig.server?.headers) {
975
1133
  server.use(headersMiddleware({ rootConfig }));
976
1134
  }
977
- const publicDir = path5.join(rootDir, "public");
1135
+ const publicDir = path6.join(rootDir, "public");
978
1136
  if (await dirExists(publicDir)) {
979
1137
  server.use(rootPublicDirMiddleware({ publicDir, viteServer }));
980
1138
  }
@@ -996,10 +1154,10 @@ async function createViteMiddleware(options) {
996
1154
  return sourceFile.relPath;
997
1155
  });
998
1156
  const bundleScripts = [];
999
- if (await isDirectory(path5.join(rootDir, "bundles"))) {
1157
+ if (await isDirectory(path6.join(rootDir, "bundles"))) {
1000
1158
  const bundleFiles = await glob3("bundles/*", { cwd: rootDir });
1001
1159
  bundleFiles.forEach((file) => {
1002
- const parts = path5.parse(file);
1160
+ const parts = path6.parse(file);
1003
1161
  if (isJsFile(parts.base)) {
1004
1162
  bundleScripts.push(file);
1005
1163
  }
@@ -1011,7 +1169,7 @@ async function createViteMiddleware(options) {
1011
1169
  optimizeDeps
1012
1170
  });
1013
1171
  function isInElementsDir(changedFilePath) {
1014
- const filePath = path5.resolve(changedFilePath);
1172
+ const filePath = path6.resolve(changedFilePath);
1015
1173
  const elementsDirs = getElementsDirs(rootConfig);
1016
1174
  return elementsDirs.some((dirPath) => filePath.startsWith(dirPath));
1017
1175
  }
@@ -1030,7 +1188,7 @@ async function createViteMiddleware(options) {
1030
1188
  const viteMiddleware = async (req, res, next) => {
1031
1189
  try {
1032
1190
  req.viteServer = viteServer;
1033
- const renderModulePath = path5.resolve(__dirname2, "./render.js");
1191
+ const renderModulePath = path6.resolve(__dirname2, "./render.js");
1034
1192
  const render = await viteServer.ssrLoadModule(
1035
1193
  renderModulePath
1036
1194
  );
@@ -1054,7 +1212,7 @@ function rootPublicDirMiddleware(options) {
1054
1212
  handler = sirv(publicDir, sirvOptions);
1055
1213
  }, 1e3);
1056
1214
  function isInPublicDir(changedFilePath) {
1057
- const filePath = path5.resolve(changedFilePath);
1215
+ const filePath = path6.resolve(changedFilePath);
1058
1216
  return filePath.startsWith(publicDir);
1059
1217
  }
1060
1218
  const watcher = options.viteServer.watcher;
@@ -1084,7 +1242,7 @@ function rootDevServer404Middleware() {
1084
1242
  console.error(`\u2753 404 ${req.originalUrl}`);
1085
1243
  if (req.renderer) {
1086
1244
  const url = req.path;
1087
- const ext = path5.extname(url);
1245
+ const ext = path6.extname(url);
1088
1246
  if (!ext) {
1089
1247
  const renderer = req.renderer;
1090
1248
  const data = await renderer.renderDevServer404(req);
@@ -1102,7 +1260,7 @@ function rootDevServer500Middleware() {
1102
1260
  console.error(String(err.stack || err));
1103
1261
  if (req.renderer) {
1104
1262
  const url = req.path;
1105
- const ext = path5.extname(url);
1263
+ const ext = path6.extname(url);
1106
1264
  if (!ext) {
1107
1265
  const renderer = req.renderer;
1108
1266
  const data = await renderer.renderDevServer500(req, err);
@@ -1127,7 +1285,7 @@ function debounce(fn, timeout) {
1127
1285
  }
1128
1286
 
1129
1287
  // src/cli/preview.ts
1130
- import path6 from "node:path";
1288
+ import path7 from "node:path";
1131
1289
  import compression from "compression";
1132
1290
  import cookieParser2 from "cookie-parser";
1133
1291
  import { default as express2 } from "express";
@@ -1135,7 +1293,7 @@ import { dim as dim3 } from "kleur/colors";
1135
1293
  import sirv2 from "sirv";
1136
1294
  async function preview(rootProjectDir, options) {
1137
1295
  process.env.NODE_ENV = "development";
1138
- const rootDir = path6.resolve(rootProjectDir || process.cwd());
1296
+ const rootDir = path7.resolve(rootProjectDir || process.cwd());
1139
1297
  const server = await createPreviewServer({ rootDir });
1140
1298
  const port = parseInt(process.env.PORT || "4007");
1141
1299
  const host = options?.host || "localhost";
@@ -1149,7 +1307,7 @@ async function preview(rootProjectDir, options) {
1149
1307
  async function createPreviewServer(options) {
1150
1308
  const rootDir = options.rootDir;
1151
1309
  const rootConfig = await loadBundledConfig(rootDir, { command: "preview" });
1152
- const distDir = path6.join(rootDir, "dist");
1310
+ const distDir = path7.join(rootDir, "dist");
1153
1311
  const server = express2();
1154
1312
  server.disable("x-powered-by");
1155
1313
  server.use(compression());
@@ -1175,7 +1333,7 @@ async function createPreviewServer(options) {
1175
1333
  if (rootConfig.server?.headers) {
1176
1334
  server.use(headersMiddleware({ rootConfig }));
1177
1335
  }
1178
- const publicDir = path6.join(distDir, "html");
1336
+ const publicDir = path7.join(distDir, "html");
1179
1337
  server.use(sirv2(publicDir, { dev: false }));
1180
1338
  server.use(trailingSlashMiddleware({ rootConfig }));
1181
1339
  server.use(rootPreviewServerMiddleware());
@@ -1189,14 +1347,14 @@ async function createPreviewServer(options) {
1189
1347
  }
1190
1348
  async function rootPreviewRendererMiddleware(options) {
1191
1349
  const { distDir, rootConfig } = options;
1192
- const render = await import(path6.join(distDir, "server/render.js"));
1193
- const manifestPath = path6.join(distDir, ".root/manifest.json");
1350
+ const render = await import(path7.join(distDir, "server/render.js"));
1351
+ const manifestPath = path7.join(distDir, ".root/manifest.json");
1194
1352
  if (!await fileExists(manifestPath)) {
1195
1353
  throw new Error(
1196
1354
  `could not find ${manifestPath}. run \`root build\` before \`root preview\`.`
1197
1355
  );
1198
1356
  }
1199
- const elementGraphJsonPath = path6.join(distDir, ".root/elements.json");
1357
+ const elementGraphJsonPath = path7.join(distDir, ".root/elements.json");
1200
1358
  if (!await fileExists(elementGraphJsonPath)) {
1201
1359
  throw new Error(
1202
1360
  `could not find ${elementGraphJsonPath}. run \`root build\` before \`root preview\`.`
@@ -1236,7 +1394,7 @@ function rootPreviewServer404Middleware() {
1236
1394
  console.error(`\u2753 404 ${req.originalUrl}`);
1237
1395
  if (req.renderer) {
1238
1396
  const url = req.path;
1239
- const ext = path6.extname(url);
1397
+ const ext = path7.extname(url);
1240
1398
  if (!ext) {
1241
1399
  const renderer = req.renderer;
1242
1400
  const data = await renderer.render404({ currentPath: url });
@@ -1254,7 +1412,7 @@ function rootPreviewServer500Middleware() {
1254
1412
  console.error(String(err.stack || err));
1255
1413
  if (req.renderer) {
1256
1414
  const url = req.path;
1257
- const ext = path6.extname(url);
1415
+ const ext = path7.extname(url);
1258
1416
  if (!ext) {
1259
1417
  const renderer = req.renderer;
1260
1418
  const data = await renderer.renderDevServer500(req, err);
@@ -1268,7 +1426,7 @@ function rootPreviewServer500Middleware() {
1268
1426
  }
1269
1427
 
1270
1428
  // src/cli/start.ts
1271
- import path7 from "node:path";
1429
+ import path8 from "node:path";
1272
1430
  import compression2 from "compression";
1273
1431
  import cookieParser3 from "cookie-parser";
1274
1432
  import { default as express3 } from "express";
@@ -1276,7 +1434,7 @@ import { dim as dim4 } from "kleur/colors";
1276
1434
  import sirv3 from "sirv";
1277
1435
  async function start(rootProjectDir, options) {
1278
1436
  process.env.NODE_ENV = "production";
1279
- const rootDir = path7.resolve(rootProjectDir || process.cwd());
1437
+ const rootDir = path8.resolve(rootProjectDir || process.cwd());
1280
1438
  const server = await createProdServer({ rootDir });
1281
1439
  const port = parseInt(process.env.PORT || "4007");
1282
1440
  const host = options?.host || "localhost";
@@ -1290,7 +1448,7 @@ async function start(rootProjectDir, options) {
1290
1448
  async function createProdServer(options) {
1291
1449
  const rootDir = options.rootDir;
1292
1450
  const rootConfig = await loadBundledConfig(rootDir, { command: "start" });
1293
- const distDir = path7.join(rootDir, "dist");
1451
+ const distDir = path8.join(rootDir, "dist");
1294
1452
  const server = express3();
1295
1453
  server.disable("x-powered-by");
1296
1454
  server.use(compression2());
@@ -1316,7 +1474,7 @@ async function createProdServer(options) {
1316
1474
  if (rootConfig.server?.headers) {
1317
1475
  server.use(headersMiddleware({ rootConfig }));
1318
1476
  }
1319
- const publicDir = path7.join(distDir, "html");
1477
+ const publicDir = path8.join(distDir, "html");
1320
1478
  server.use(sirv3(publicDir, { dev: false }));
1321
1479
  server.use(trailingSlashMiddleware({ rootConfig }));
1322
1480
  server.use(rootProdServerMiddleware());
@@ -1330,14 +1488,14 @@ async function createProdServer(options) {
1330
1488
  }
1331
1489
  async function rootProdRendererMiddleware(options) {
1332
1490
  const { distDir, rootConfig } = options;
1333
- const render = await import(path7.join(distDir, "server/render.js"));
1334
- const manifestPath = path7.join(distDir, ".root/manifest.json");
1491
+ const render = await import(path8.join(distDir, "server/render.js"));
1492
+ const manifestPath = path8.join(distDir, ".root/manifest.json");
1335
1493
  if (!await fileExists(manifestPath)) {
1336
1494
  throw new Error(
1337
1495
  `could not find ${manifestPath}. run \`root build\` before \`root start\`.`
1338
1496
  );
1339
1497
  }
1340
- const elementGraphJsonPath = path7.join(distDir, ".root/elements.json");
1498
+ const elementGraphJsonPath = path8.join(distDir, ".root/elements.json");
1341
1499
  if (!await fileExists(elementGraphJsonPath)) {
1342
1500
  throw new Error(
1343
1501
  `could not find ${elementGraphJsonPath}. run \`root build\` before \`root start\`.`
@@ -1377,7 +1535,7 @@ function rootProdServer404Middleware() {
1377
1535
  console.error(`\u2753 404 ${req.originalUrl}`);
1378
1536
  if (req.renderer) {
1379
1537
  const url = req.path;
1380
- const ext = path7.extname(url);
1538
+ const ext = path8.extname(url);
1381
1539
  if (!ext) {
1382
1540
  const renderer = req.renderer;
1383
1541
  const data = await renderer.render404({ currentPath: url });
@@ -1395,7 +1553,7 @@ function rootProdServer500Middleware() {
1395
1553
  console.error(String(err.stack || err));
1396
1554
  if (req.renderer) {
1397
1555
  const url = req.path;
1398
- const ext = path7.extname(url);
1556
+ const ext = path8.extname(url);
1399
1557
  if (!ext) {
1400
1558
  const renderer = req.renderer;
1401
1559
  const data = await renderer.renderError(err);
@@ -1435,6 +1593,9 @@ var CliRunner = class {
1435
1593
  "number of files to build concurrently",
1436
1594
  "10"
1437
1595
  ).action(build);
1596
+ program.command("create-package [path]").alias("package").description(
1597
+ "creates a standalone npm package for deployment to various hosting services"
1598
+ ).option("--target <target>", "hosting target, i.e. appengine or firebase").option("--out <outdir>", "output dir").option("--mode <mode>", "deployment mode, i.e. production or preview").action(createPackage);
1438
1599
  program.command("dev [path]").description("starts the server in development mode").option(
1439
1600
  "--host <host>",
1440
1601
  "network address the server should listen on, e.g. 127.0.0.1"
@@ -1453,6 +1614,7 @@ var CliRunner = class {
1453
1614
 
1454
1615
  export {
1455
1616
  build,
1617
+ createPackage,
1456
1618
  dev,
1457
1619
  createDevServer,
1458
1620
  preview,
@@ -1461,4 +1623,4 @@ export {
1461
1623
  createProdServer,
1462
1624
  CliRunner
1463
1625
  };
1464
- //# sourceMappingURL=chunk-VDORN4UU.js.map
1626
+ //# sourceMappingURL=chunk-HRGMPZCK.js.map