@varlock/cloudflare-integration 0.0.1 → 0.1.0

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 CHANGED
@@ -4,7 +4,7 @@ This package helps you integrate [varlock](https://varlock.dev) into a [Cloudfla
4
4
 
5
5
  It provides:
6
6
 
7
- - a Vite plugin (`varlockCloudflareVitePlugin`) that wraps `@cloudflare/vite-plugin` with automatic env var injection into miniflare bindings and Cloudflare's secret bindings at runtime
7
+ - a Vite plugin (`varlockCloudflareVitePlugin`) that wraps `@cloudflare/vite-plugin` with automatic env var injection into miniflare bindings and Cloudflare's secret bindings at runtime, and auto-detects SvelteKit (projects using `@sveltejs/adapter-cloudflare`) to apply the right injection strategy without extra config
8
8
  - a `varlock-wrangler` CLI binary — a drop-in replacement for `wrangler` that injects env via named pipe in dev, uploads vars/secrets on deploy, and generates correct types
9
9
  - a standalone init module (`@varlock/cloudflare-integration/init`) for non-Vite workers
10
10
  - validation of your env vars against your `.env.schema`
package/dist/index.d.ts CHANGED
@@ -1,8 +1,14 @@
1
1
  import { PluginConfig } from '@cloudflare/vite-plugin';
2
2
 
3
3
  /**
4
- * Varlock Cloudflare Vite plugin — wraps the Cloudflare Workers Vite plugin
5
- * with automatic env var injection.
4
+ * Varlock Cloudflare Vite plugin — wraps `@cloudflare/vite-plugin` with
5
+ * automatic env var injection.
6
+ *
7
+ * For SvelteKit projects deploying via `@sveltejs/adapter-cloudflare`, use
8
+ * `varlockSvelteKitCloudflarePlugin` from
9
+ * `@varlock/cloudflare-integration/sveltekit` instead — it skips
10
+ * `@cloudflare/vite-plugin` (which doesn't support SvelteKit) and uses an
11
+ * SSR-entry-based env loader.
6
12
  *
7
13
  * @example
8
14
  * ```ts
package/dist/index.js CHANGED
@@ -1407,18 +1407,29 @@ function resetStaticReplacements() {
1407
1407
  });
1408
1408
  }
1409
1409
  var loadCount = 0;
1410
- function reloadConfig() {
1411
- debug("loading config - count =", ++loadCount);
1410
+ function reloadConfig(cwd) {
1411
+ debug("loading config - count =", ++loadCount, cwd ? `(cwd: ${cwd})` : "");
1412
1412
  try {
1413
- const execResult = execSyncVarlock("load --format json-full", {
1413
+ const execResult = execSyncVarlock("load --format json-full --compact", {
1414
1414
  env: originalProcessEnv,
1415
- showLogsOnError: true
1415
+ ...cwd && { cwd }
1416
1416
  });
1417
1417
  process.env.__VARLOCK_ENV = execResult;
1418
1418
  varlockLoadedEnv = JSON.parse(process.env.__VARLOCK_ENV);
1419
1419
  configIsValid = true;
1420
1420
  } catch (err) {
1421
+ const errAny = err;
1422
+ const stdout = errAny?.stdout?.toString();
1423
+ if (stdout) {
1424
+ try {
1425
+ varlockLoadedEnv = JSON.parse(stdout);
1426
+ } catch {
1427
+ }
1428
+ }
1429
+ if (errAny?.stderr) console.error(errAny.stderr.toString());
1421
1430
  configIsValid = false;
1431
+ resetStaticReplacements();
1432
+ return;
1422
1433
  }
1423
1434
  initVarlockEnv();
1424
1435
  patchGlobalConsole();
@@ -1449,7 +1460,11 @@ See https://varlock.dev/integrations/vite/ for more details.
1449
1460
  `);
1450
1461
  }
1451
1462
  isDevCommand = env.command === "serve";
1452
- if (isFirstLoad) {
1463
+ const projectRoot = config.root ? path.resolve(config.root) : void 0;
1464
+ const rootDiffersFromCwd = !!(projectRoot && path.relative(projectRoot, process.cwd()) !== "");
1465
+ if (rootDiffersFromCwd) {
1466
+ reloadConfig(projectRoot);
1467
+ } else if (isFirstLoad) {
1453
1468
  isFirstLoad = false;
1454
1469
  } else if (isDevCommand) {
1455
1470
  reloadConfig();
@@ -1459,6 +1474,16 @@ See https://varlock.dev/integrations/vite/ for more details.
1459
1474
  config.clearScreen = false;
1460
1475
  } else {
1461
1476
  console.log("\u{1F4A5} Varlock config validation failed \u{1F4A5}");
1477
+ if (varlockLoadedEnv?.errors?.root) {
1478
+ for (const msg of varlockLoadedEnv.errors.root) {
1479
+ console.log(` - ${msg}`);
1480
+ }
1481
+ }
1482
+ if (varlockLoadedEnv?.errors?.configItems) {
1483
+ for (const [key, msg] of Object.entries(varlockLoadedEnv.errors.configItems)) {
1484
+ console.log(` - ${key}: ${msg}`);
1485
+ }
1486
+ }
1462
1487
  process.exit(1);
1463
1488
  }
1464
1489
  }
@@ -1466,6 +1491,7 @@ See https://varlock.dev/integrations/vite/ for more details.
1466
1491
  // hook to observe/modify config after it is resolved
1467
1492
  configResolved(config) {
1468
1493
  debug("vite plugin - configResolved fn called");
1494
+ if (!varlockLoadedEnv) return;
1469
1495
  for (const varlockSource of varlockLoadedEnv.sources) {
1470
1496
  if (!varlockSource.enabled) continue;
1471
1497
  if (varlockLoadedEnv.basePath && varlockSource.path) {
@@ -1597,7 +1623,53 @@ See https://varlock.dev/integrations/vite/ for more details.
1597
1623
  }
1598
1624
  };
1599
1625
  }
1626
+
1627
+ // src/shared-ssr-entry-code.ts
1628
+ var CLOUDFLARE_SSR_ENTRY_CODE = `
1629
+ if (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') {
1630
+ const { env: __cfEnv } = await import('cloudflare:workers');
1631
+ let __varlockEnvJson;
1632
+ if (__cfEnv?.__VARLOCK_ENV) {
1633
+ __varlockEnvJson = __cfEnv.__VARLOCK_ENV;
1634
+ } else if (__cfEnv?.__VARLOCK_ENV_CHUNKS) {
1635
+ const n = parseInt(__cfEnv.__VARLOCK_ENV_CHUNKS, 10);
1636
+ if (!Number.isFinite(n) || n < 1 || n > 1000) {
1637
+ throw new Error("[varlock] invalid __VARLOCK_ENV_CHUNKS: " + __cfEnv.__VARLOCK_ENV_CHUNKS);
1638
+ }
1639
+ const parts = [];
1640
+ for (let i = 0; i < n; i++) {
1641
+ const chunk = __cfEnv["__VARLOCK_ENV_" + i];
1642
+ if (chunk == null) throw new Error("[varlock] missing chunk __VARLOCK_ENV_" + i);
1643
+ parts.push(chunk);
1644
+ }
1645
+ __varlockEnvJson = parts.join("");
1646
+ }
1647
+ if (__varlockEnvJson) {
1648
+ try {
1649
+ globalThis.__varlockLoadedEnv = JSON.parse(__varlockEnvJson);
1650
+ } catch (e) {
1651
+ throw new Error("[varlock] failed to parse __VARLOCK_ENV: " + e.message);
1652
+ }
1653
+ }
1654
+ }
1655
+ `;
1656
+
1657
+ // src/index.ts
1658
+ var CLOUDFLARE_PLUGIN_NAME = "vite-plugin-cloudflare";
1600
1659
  function varlockCloudflareVitePlugin(cloudflareOptions) {
1660
+ const conflictGuard = {
1661
+ name: "varlock-cloudflare-conflict-guard",
1662
+ configResolved(config) {
1663
+ const cfPluginCount = config.plugins.filter(
1664
+ (p) => typeof p?.name === "string" && p.name === CLOUDFLARE_PLUGIN_NAME
1665
+ ).length;
1666
+ if (cfPluginCount > 1) {
1667
+ throw new Error(
1668
+ "[varlock] `@cloudflare/vite-plugin` is already present in your Vite plugins. Remove it \u2014 `varlockCloudflareVitePlugin` injects (and configures) it for you."
1669
+ );
1670
+ }
1671
+ }
1672
+ };
1601
1673
  let isDevMode = false;
1602
1674
  const modeDetector = {
1603
1675
  name: "varlock-cloudflare-mode",
@@ -1640,45 +1712,14 @@ function varlockCloudflareVitePlugin(cloudflareOptions) {
1640
1712
  };
1641
1713
  const varlockPlugin = varlockVitePlugin({
1642
1714
  ssrEntryModuleIds: ["\0virtual:cloudflare/worker-entry"],
1643
- ssrEntryCode: [
1644
- // read the resolved env from Cloudflare's secret bindings at runtime
1645
- // the __VARLOCK_ENV secret is uploaded via `varlock-wrangler deploy`
1646
- // it may be a single binding or split into chunks if >5KB
1647
- `
1648
- import { env as __cfEnv } from 'cloudflare:workers';
1649
- {
1650
- let __varlockEnvJson;
1651
- if (__cfEnv?.__VARLOCK_ENV) {
1652
- __varlockEnvJson = __cfEnv.__VARLOCK_ENV;
1653
- } else if (__cfEnv?.__VARLOCK_ENV_CHUNKS) {
1654
- const n = parseInt(__cfEnv.__VARLOCK_ENV_CHUNKS, 10);
1655
- if (!Number.isFinite(n) || n < 1 || n > 1000) {
1656
- throw new Error("[varlock] invalid __VARLOCK_ENV_CHUNKS: " + __cfEnv.__VARLOCK_ENV_CHUNKS);
1657
- }
1658
- const parts = [];
1659
- for (let i = 0; i < n; i++) {
1660
- const chunk = __cfEnv["__VARLOCK_ENV_" + i];
1661
- if (chunk == null) throw new Error("[varlock] missing chunk __VARLOCK_ENV_" + i);
1662
- parts.push(chunk);
1663
- }
1664
- __varlockEnvJson = parts.join("");
1665
- }
1666
- if (__varlockEnvJson) {
1667
- try {
1668
- globalThis.__varlockLoadedEnv = JSON.parse(__varlockEnvJson);
1669
- } catch (e) {
1670
- throw new Error("[varlock] failed to parse __VARLOCK_ENV: " + e.message);
1671
- }
1672
- }
1673
- }
1674
- `
1675
- ]
1715
+ ssrEntryCode: [CLOUDFLARE_SSR_ENTRY_CODE]
1676
1716
  });
1677
1717
  const cloudflarePlugin = cloudflare({
1678
1718
  ...cloudflareOptions,
1679
1719
  config: mergedConfig
1680
1720
  });
1681
1721
  return [
1722
+ conflictGuard,
1682
1723
  modeDetector,
1683
1724
  varlockPlugin,
1684
1725
  // cloudflare() may return a single plugin or an array