@varlock/cloudflare-integration 1.1.2 → 1.1.4

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
@@ -1381,14 +1381,105 @@ function createReplacerTransformFn(opts) {
1381
1381
  return magicString;
1382
1382
  };
1383
1383
  }
1384
+ var ANSI_COLORS = {
1385
+ 30: "#45475a",
1386
+ 31: "#f38ba8",
1387
+ 32: "#a6e3a1",
1388
+ 33: "#f9e2af",
1389
+ 34: "#89b4fa",
1390
+ 35: "#f5c2e7",
1391
+ 36: "#94e2d5",
1392
+ 37: "#cdd6f4",
1393
+ 90: "#585b70",
1394
+ 91: "#f38ba8",
1395
+ 92: "#a6e3a1",
1396
+ 93: "#f9e2af",
1397
+ 94: "#89b4fa",
1398
+ 95: "#f5c2e7",
1399
+ 96: "#94e2d5",
1400
+ 97: "#cdd6f4"
1401
+ };
1402
+ var ANSI_RE = new RegExp(`${String.fromCharCode(27)}\\[([0-9;]*)m`, "g");
1403
+ function ansiToHtml(str) {
1404
+ let bold = false;
1405
+ let dim = false;
1406
+ let italic = false;
1407
+ let underline = false;
1408
+ let strikethrough = false;
1409
+ let color;
1410
+ let isOpen = false;
1411
+ function buildSpan() {
1412
+ const styles = [];
1413
+ if (bold) styles.push("font-weight:bold");
1414
+ if (dim) styles.push("opacity:0.6");
1415
+ if (italic) styles.push("font-style:italic");
1416
+ if (underline) styles.push("text-decoration:underline");
1417
+ if (strikethrough) styles.push("text-decoration:line-through");
1418
+ if (color) styles.push(`color:${color}`);
1419
+ if (!styles.length) return "";
1420
+ isOpen = true;
1421
+ return `<span style="${styles.join(";")}">`;
1422
+ }
1423
+ function closeIfOpen() {
1424
+ if (!isOpen) return "";
1425
+ isOpen = false;
1426
+ return "</span>";
1427
+ }
1428
+ let html = str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1429
+ html = html.replace(ANSI_RE, (_, codes) => {
1430
+ const parts = codes.split(";").filter(Boolean).map(Number);
1431
+ if (parts.length === 0 || parts.includes(0)) {
1432
+ bold = false;
1433
+ dim = false;
1434
+ italic = false;
1435
+ underline = false;
1436
+ strikethrough = false;
1437
+ color = void 0;
1438
+ return closeIfOpen();
1439
+ }
1440
+ const close = closeIfOpen();
1441
+ for (const code of parts) {
1442
+ if (code === 1) bold = true;
1443
+ else if (code === 2) dim = true;
1444
+ else if (code === 3) italic = true;
1445
+ else if (code === 4) underline = true;
1446
+ else if (code === 9) strikethrough = true;
1447
+ else if (code === 22) {
1448
+ bold = false;
1449
+ dim = false;
1450
+ } else if (code === 23) italic = false;
1451
+ else if (code === 24) underline = false;
1452
+ else if (code === 29) strikethrough = false;
1453
+ else if (code === 39) color = void 0;
1454
+ else if (ANSI_COLORS[code]) color = ANSI_COLORS[code];
1455
+ }
1456
+ return close + buildSpan();
1457
+ });
1458
+ html += closeIfOpen();
1459
+ return html;
1460
+ }
1461
+ function buildErrorPageHtml(ansiError) {
1462
+ const errorContent = ansiError ? ansiToHtml(ansiError) : "Config is invalid \u2014 check your terminal for details.";
1463
+ return `<!DOCTYPE html>
1464
+ <html><head><title>varlock - config error</title></head>
1465
+ <body style="font-family: monospace; background: #1e1e2e; color: #cdd6f4; margin: 0; padding: 2rem;">
1466
+ <div style="margin-bottom: 1.5rem;">
1467
+ <h2 style="color: #f38ba8; margin: 0 0 0.5rem 0;">\u{1F512} Varlock \u2014 env config validation failed</h2>
1468
+ <p style="color: #6c7086; margin: 0;">Your environment variables are loaded and validated by <a href="https://varlock.dev" style="color: #89b4fa;">varlock</a>. Fix the error(s) below and save to reload.</p>
1469
+ </div>
1470
+ <pre style="white-space: pre-wrap; word-wrap: break-word; line-height: 1.5; background: #181825; padding: 1rem; border-radius: 8px;">${errorContent}</pre>
1471
+ </body></html>`;
1472
+ }
1384
1473
  globalThis.__varlockThrowOnMissingKeys = true;
1385
1474
  var originalProcessEnv = { ...process.env };
1386
1475
  var debug = createDebug("varlock:vite-integration");
1387
- var isFirstLoad = !process.__VARLOCK_ENV;
1388
- debug("varlock vite plugin loaded. first load = ", isFirstLoad);
1476
+ debug("varlock vite plugin loaded");
1389
1477
  var isDevCommand;
1390
1478
  var configIsValid = true;
1391
1479
  var varlockLoadedEnv;
1480
+ var varlockLastError;
1481
+ var lastErrorAt = 0;
1482
+ var configHookCalled = false;
1392
1483
  var staticReplacements = {};
1393
1484
  var replacerFn;
1394
1485
  function resetStaticReplacements() {
@@ -1416,16 +1507,28 @@ function reloadConfig(cwd) {
1416
1507
  });
1417
1508
  process.env.__VARLOCK_ENV = stdout;
1418
1509
  varlockLoadedEnv = JSON.parse(stdout);
1510
+ varlockLastError = void 0;
1511
+ lastErrorAt = 0;
1419
1512
  configIsValid = true;
1420
1513
  } catch (err) {
1421
1514
  if (err instanceof VarlockExecError) {
1422
1515
  if (err.stdout) {
1423
1516
  try {
1424
1517
  varlockLoadedEnv = JSON.parse(err.stdout);
1518
+ process.env.__VARLOCK_ENV = err.stdout;
1425
1519
  } catch {
1426
1520
  }
1427
1521
  }
1428
- if (err.stderr) console.error(err.stderr);
1522
+ if (err.stderr) {
1523
+ const now = Date.now();
1524
+ const isDuplicate = err.stderr === varlockLastError && now - lastErrorAt < 5e3;
1525
+ varlockLastError = err.stderr;
1526
+ lastErrorAt = now;
1527
+ if (!isDuplicate) {
1528
+ console.error(err.stderr);
1529
+ console.error("\n[varlock] \u26A0\uFE0F config is invalid \u2014 fix the error(s) above to continue\n");
1530
+ }
1531
+ }
1429
1532
  }
1430
1533
  configIsValid = false;
1431
1534
  resetStaticReplacements();
@@ -1480,7 +1583,7 @@ function varlockVitePlugin(vitePluginOptions) {
1480
1583
  },
1481
1584
  // hook to modify config before it is resolved
1482
1585
  async config(config, env) {
1483
- debug("vite plugin - config fn called");
1586
+ debug("vite plugin - config fn called, loadCount =", loadCount, "command =", env.command);
1484
1587
  if (config.envDir) {
1485
1588
  console.warn(`
1486
1589
  [varlock] \u26A0\uFE0F The \`envDir\` Vite option is not supported by varlock.
@@ -1500,8 +1603,8 @@ See https://varlock.dev/integrations/vite/ for more details.
1500
1603
  const rootDiffersFromCwd = !!(projectRoot && path.relative(projectRoot, process.cwd()) !== "");
1501
1604
  if (rootDiffersFromCwd) {
1502
1605
  reloadConfig(projectRoot);
1503
- } else if (isFirstLoad) {
1504
- isFirstLoad = false;
1606
+ } else if (!configHookCalled) {
1607
+ configHookCalled = true;
1505
1608
  } else if (isDevCommand) {
1506
1609
  reloadConfig();
1507
1610
  }
@@ -1509,17 +1612,7 @@ See https://varlock.dev/integrations/vite/ for more details.
1509
1612
  if (isDevCommand) {
1510
1613
  config.clearScreen = false;
1511
1614
  } else {
1512
- console.log("\u{1F4A5} Varlock config validation failed \u{1F4A5}");
1513
- if (varlockLoadedEnv?.errors?.root) {
1514
- for (const msg of varlockLoadedEnv.errors.root) {
1515
- console.log(` - ${msg}`);
1516
- }
1517
- }
1518
- if (varlockLoadedEnv?.errors?.configItems) {
1519
- for (const [key, msg] of Object.entries(varlockLoadedEnv.errors.configItems)) {
1520
- console.log(` - ${key}: ${msg}`);
1521
- }
1522
- }
1615
+ console.error("\n[varlock] config is invalid \u2014 cannot proceed with build\n");
1523
1616
  process.exit(1);
1524
1617
  }
1525
1618
  }
@@ -1538,19 +1631,13 @@ See https://varlock.dev/integrations/vite/ for more details.
1538
1631
  // hook to configure vite dev server
1539
1632
  async configureServer(server) {
1540
1633
  debug("vite plugin - configureServer fn called");
1541
- if (!configIsValid) {
1542
- server.middlewares.use((req, res, next) => {
1543
- server.hot.send({
1544
- type: "error",
1545
- err: {
1546
- plugin: "varlock",
1547
- message: "Your config is currently invalid - check your terminal for more details",
1548
- stack: ""
1549
- }
1550
- });
1551
- return next();
1552
- });
1553
- }
1634
+ server.middlewares.use((req, res, next) => {
1635
+ if (configIsValid) return next();
1636
+ if (req.url?.startsWith("/@")) return next();
1637
+ res.statusCode = 500;
1638
+ res.setHeader("Content-Type", "text/html; charset=utf-8");
1639
+ res.end(buildErrorPageHtml(varlockLastError));
1640
+ });
1554
1641
  },
1555
1642
  transform(code, id, options) {
1556
1643
  let magicString = replacerFn(this, code, id);
@@ -1604,19 +1691,9 @@ See https://varlock.dev/integrations/vite/ for more details.
1604
1691
  // this enables replacing %ENV.xxx% constants in html entry-point files
1605
1692
  // see https://vite.dev/guide/env-and-mode.html#html-constant-replacement
1606
1693
  transformIndexHtml(html) {
1694
+ debug("transformIndexHtml called, configIsValid =", configIsValid);
1607
1695
  if (!configIsValid) {
1608
- return `
1609
- <html>
1610
- <head>
1611
- <script type="module" src="/@vite/client"></script>
1612
- <title>Invalid config</title>
1613
- </head>
1614
- <body>
1615
- <h2>Your varlock config is currently invalid!</h2>
1616
- <p>Check your terminal for more details</p>
1617
- </body>
1618
- </html>
1619
- `;
1696
+ return buildErrorPageHtml(varlockLastError);
1620
1697
  }
1621
1698
  const replacedHtml = html.replace(
1622
1699
  // look for "%ENV.xxx%"