@varlock/cloudflare-integration 1.0.0 → 1.1.1

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/dist/sveltekit.js CHANGED
@@ -4,7 +4,7 @@ import { patchGlobalConsole } from 'varlock/patch-console';
4
4
  import { patchGlobalServerResponse } from 'varlock/patch-server-response';
5
5
  import { patchGlobalResponse } from 'varlock/patch-response';
6
6
  import { createDebug } from 'varlock';
7
- import { execSyncVarlock } from 'varlock/exec-sync-varlock';
7
+ import { execSyncVarlock, VarlockExecError } from 'varlock/exec-sync-varlock';
8
8
 
9
9
  // ../vite/dist/index.js
10
10
  var __create = Object.create;
@@ -33,7 +33,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
33
33
  mod
34
34
  ));
35
35
  var require_ast_matcher = __commonJS({
36
- "../../../node_modules/.bun/ast-matcher@1.2.0/node_modules/ast-matcher/index.js"(exports$1, module) {
36
+ "../../../node_modules/.bun/ast-matcher@1.2.0/node_modules/ast-matcher/index.js"(exports, module) {
37
37
  var STOP = false;
38
38
  var SKIP_BRANCH = 1;
39
39
  var IGNORED_KEYS = ["start", "end", "loc", "location", "locations", "line", "column", "range", "ranges", "raw", "extra"];
@@ -1409,23 +1409,24 @@ var loadCount = 0;
1409
1409
  function reloadConfig(cwd) {
1410
1410
  debug("loading config - count =", ++loadCount, cwd ? `(cwd: ${cwd})` : "");
1411
1411
  try {
1412
- const execResult = execSyncVarlock("load --format json-full --compact", {
1412
+ const { stdout } = execSyncVarlock("load --format json-full --compact", {
1413
+ fullResult: true,
1413
1414
  env: originalProcessEnv,
1414
1415
  ...cwd && { cwd }
1415
1416
  });
1416
- process.env.__VARLOCK_ENV = execResult;
1417
- varlockLoadedEnv = JSON.parse(process.env.__VARLOCK_ENV);
1417
+ process.env.__VARLOCK_ENV = stdout;
1418
+ varlockLoadedEnv = JSON.parse(stdout);
1418
1419
  configIsValid = true;
1419
1420
  } catch (err) {
1420
- const errAny = err;
1421
- const stdout = errAny?.stdout?.toString();
1422
- if (stdout) {
1423
- try {
1424
- varlockLoadedEnv = JSON.parse(stdout);
1425
- } catch {
1421
+ if (err instanceof VarlockExecError) {
1422
+ if (err.stdout) {
1423
+ try {
1424
+ varlockLoadedEnv = JSON.parse(err.stdout);
1425
+ } catch {
1426
+ }
1426
1427
  }
1428
+ if (err.stderr) console.error(err.stderr);
1427
1429
  }
1428
- if (errAny?.stderr) console.error(errAny.stderr.toString());
1429
1430
  configIsValid = false;
1430
1431
  resetStaticReplacements();
1431
1432
  return;
@@ -1437,10 +1438,46 @@ function reloadConfig(cwd) {
1437
1438
  resetStaticReplacements();
1438
1439
  }
1439
1440
  reloadConfig();
1441
+ var VARLOCK_INIT_MODULE_ID = "\0varlock-ssr-init";
1440
1442
  function varlockVitePlugin(vitePluginOptions) {
1443
+ function buildInitModuleCode() {
1444
+ const ssrInjectMode = vitePluginOptions?.ssrInjectMode ?? "init-only";
1445
+ const lines = [
1446
+ "// Virtual module generated by @varlock/vite-integration",
1447
+ "// Runs before any user code to ensure ENV is available at module top-level",
1448
+ "globalThis.__varlockThrowOnMissingKeys = true;"
1449
+ ];
1450
+ if (ssrInjectMode === "auto-load") {
1451
+ lines.push("import 'varlock/auto-load';");
1452
+ } else {
1453
+ if (ssrInjectMode === "resolved-env") {
1454
+ lines.push(`globalThis.__varlockLoadedEnv = ${JSON.stringify(varlockLoadedEnv)};`);
1455
+ }
1456
+ if (vitePluginOptions?.ssrEntryCode?.length) {
1457
+ lines.push(...vitePluginOptions.ssrEntryCode);
1458
+ }
1459
+ lines.push(
1460
+ "import { initVarlockEnv } from 'varlock/env';",
1461
+ "import { patchGlobalConsole } from 'varlock/patch-console';",
1462
+ "import { patchGlobalResponse } from 'varlock/patch-response';"
1463
+ );
1464
+ lines.push(
1465
+ "initVarlockEnv();",
1466
+ "patchGlobalConsole();"
1467
+ );
1468
+ lines.push("patchGlobalResponse();");
1469
+ }
1470
+ return lines.join("\n");
1471
+ }
1441
1472
  return {
1442
1473
  name: "inject-varlock-config",
1443
1474
  enforce: "post",
1475
+ resolveId(id) {
1476
+ if (id === VARLOCK_INIT_MODULE_ID) return id;
1477
+ },
1478
+ load(id) {
1479
+ if (id === VARLOCK_INIT_MODULE_ID) return buildInitModuleCode();
1480
+ },
1444
1481
  // hook to modify config before it is resolved
1445
1482
  async config(config, env) {
1446
1483
  debug("vite plugin - config fn called");
@@ -1520,51 +1557,27 @@ See https://varlock.dev/integrations/vite/ for more details.
1520
1557
  const fileExt = id.split("?")[0].split("#")[0].split(".").pop() || "";
1521
1558
  let isEntry = false;
1522
1559
  if (SUPPORTED_FILES.includes(fileExt)) {
1523
- const moduleIds = Array.from(this.getModuleIds());
1524
- if (moduleIds[0] === id) isEntry = true;
1560
+ try {
1561
+ const moduleInfo = this.getModuleInfo(id);
1562
+ if (moduleInfo?.isEntry) isEntry = true;
1563
+ } catch {
1564
+ }
1565
+ if (!isEntry) {
1566
+ const moduleIds = Array.from(this.getModuleIds());
1567
+ if (moduleIds[0] === id) isEntry = true;
1568
+ }
1525
1569
  }
1526
1570
  if (vitePluginOptions?.ssrEntryModuleIds?.includes(id)) isEntry = true;
1527
1571
  if (isEntry) {
1528
1572
  debug(`detected entry: ${id}`);
1529
- let isDevEnv = false;
1530
- if (this.environment) {
1531
- isDevEnv = this.environment.mode === "dev";
1532
- } else {
1533
- isDevEnv = isDevCommand;
1534
- }
1535
- const injectCode = [
1536
- "// INJECTED BY @varlock/vite-integration ----",
1537
- "globalThis.__varlockThrowOnMissingKeys = true;"
1538
- ];
1573
+ const injectCode = ["// INJECTED BY @varlock/vite-integration ----"];
1539
1574
  if (options?.ssr) {
1540
- const ssrInjectMode = vitePluginOptions?.ssrInjectMode ?? "init-only";
1541
- debug("ssrInjectMode =", ssrInjectMode, "isDev =", isDevEnv);
1542
- if (ssrInjectMode === "auto-load") {
1543
- injectCode.push(
1544
- "import 'varlock/auto-load';"
1545
- );
1546
- } else {
1547
- if (ssrInjectMode === "resolved-env") {
1548
- injectCode.push(`globalThis.__varlockLoadedEnv = ${JSON.stringify(varlockLoadedEnv)};`);
1549
- }
1550
- if (vitePluginOptions?.ssrEntryCode?.length) {
1551
- injectCode.push(...vitePluginOptions.ssrEntryCode);
1552
- }
1553
- injectCode.push(
1554
- "import { initVarlockEnv } from 'varlock/env';",
1555
- "import { patchGlobalConsole } from 'varlock/patch-console';",
1556
- "import { patchGlobalResponse } from 'varlock/patch-response';"
1557
- );
1558
- injectCode.push(
1559
- "initVarlockEnv();",
1560
- "patchGlobalConsole();"
1561
- );
1562
- injectCode.push("patchGlobalResponse();");
1563
- }
1575
+ injectCode.push(`import '${VARLOCK_INIT_MODULE_ID}';`);
1564
1576
  } else {
1577
+ injectCode.push("globalThis.__varlockThrowOnMissingKeys = true;");
1578
+ const isDevEnv = this.environment ? this.environment.mode === "dev" : isDevCommand;
1565
1579
  if (isDevEnv) {
1566
1580
  injectCode.push(
1567
- "// NOTE - __varlockValidKeys is only injected during development",
1568
1581
  `globalThis.__varlockValidKeys = ${JSON.stringify(Object.keys(varlockLoadedEnv?.config || {}))};`
1569
1582
  );
1570
1583
  }