mokup 0.2.1 → 0.2.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/dist/vite.cjs CHANGED
@@ -1,15 +1,15 @@
1
1
  'use strict';
2
2
 
3
+ const node_fs = require('node:fs');
3
4
  const node_process = require('node:process');
5
+ const node_url = require('node:url');
4
6
  const chokidar = require('chokidar');
5
7
  const node_buffer = require('node:buffer');
6
8
  const hono = require('hono');
7
9
  const patternRouter = require('hono/router/pattern-router');
8
10
  const pathe = require('pathe');
9
- const node_fs = require('node:fs');
10
11
  const node_module = require('node:module');
11
12
  const runtime = require('@mokup/runtime');
12
- const node_url = require('node:url');
13
13
  const esbuild = require('esbuild');
14
14
  const jsoncParser = require('jsonc-parser');
15
15
 
@@ -595,6 +595,7 @@ function createPlaygroundMiddleware(params) {
595
595
  const routes = params.getRoutes();
596
596
  sendJson(res, {
597
597
  basePath: matchedPath,
598
+ root: baseRoot,
598
599
  count: routes.length,
599
600
  groups: groups.map((group) => ({ key: group.key, label: group.label })),
600
601
  routes: routes.map((route) => toPlaygroundRoute(route, baseRoot, groups))
@@ -1488,6 +1489,19 @@ function resolveSwRuntimeImportPath(base) {
1488
1489
  const normalizedBase = normalizeBase(base);
1489
1490
  return `${normalizedBase}@id/mokup/runtime`;
1490
1491
  }
1492
+ const swModuleCandidates = [
1493
+ new URL("../sw.ts", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('vite.cjs', document.baseURI).href))),
1494
+ new URL("../sw.js", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('vite.cjs', document.baseURI).href)))
1495
+ ];
1496
+ const localSwModulePath = (() => {
1497
+ for (const candidate of swModuleCandidates) {
1498
+ const filePath = node_url.fileURLToPath(candidate);
1499
+ if (node_fs.existsSync(filePath)) {
1500
+ return filePath;
1501
+ }
1502
+ }
1503
+ return node_url.fileURLToPath(swModuleCandidates[0] ?? new URL("../sw.ts", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('vite.cjs', document.baseURI).href))));
1504
+ })();
1491
1505
  function hasMiddlewareStack(middlewares) {
1492
1506
  const candidate = middlewares;
1493
1507
  return Array.isArray(candidate.stack);
@@ -1503,6 +1517,7 @@ function createMokupPlugin(options = {}) {
1503
1517
  let root = node_process.cwd();
1504
1518
  let base = "/";
1505
1519
  let command = "serve";
1520
+ let assetsDir = "assets";
1506
1521
  let routes = [];
1507
1522
  let serverRoutes = [];
1508
1523
  let swRoutes = [];
@@ -1535,8 +1550,38 @@ function createMokupPlugin(options = {}) {
1535
1550
  const hasSwRoutes = () => !!swConfig && swRoutes.length > 0;
1536
1551
  const resolveSwRequestPath = (path) => resolveRegisterPath(base, path);
1537
1552
  const resolveSwRegisterScope = (scope) => resolveRegisterScope(base, scope);
1553
+ const resolveHtmlAssetPath = (fileName) => {
1554
+ const normalizedFileName = fileName.startsWith("/") ? fileName.slice(1) : fileName;
1555
+ if (base && base.startsWith(".")) {
1556
+ return normalizedFileName;
1557
+ }
1558
+ const normalizedBase = normalizeBase(base);
1559
+ return `${normalizedBase}${normalizedFileName}`;
1560
+ };
1561
+ const resolveAssetsFileName = (fileName) => {
1562
+ const trimmed = assetsDir.replace(/^\/+|\/+$/g, "");
1563
+ if (!trimmed) {
1564
+ return fileName;
1565
+ }
1566
+ return `${trimmed}/${fileName}`;
1567
+ };
1538
1568
  const swVirtualId = "virtual:mokup-sw";
1539
1569
  const resolvedSwVirtualId = `\0${swVirtualId}`;
1570
+ const swLifecycleVirtualId = "virtual:mokup-sw-lifecycle";
1571
+ const resolvedSwLifecycleVirtualId = `\0${swLifecycleVirtualId}`;
1572
+ let swLifecycleFileName = null;
1573
+ let swLifecycleScript = null;
1574
+ async function resolveSwModuleImport(context) {
1575
+ const resolved = await context.resolve("mokup/sw");
1576
+ if (resolved?.id) {
1577
+ return resolved.id;
1578
+ }
1579
+ const fallbackResolved = await context.resolve(localSwModulePath);
1580
+ if (fallbackResolved?.id) {
1581
+ return fallbackResolved.id;
1582
+ }
1583
+ return localSwModulePath;
1584
+ }
1540
1585
  function buildSwLifecycleScript(importPath = "mokup/sw") {
1541
1586
  const shouldUnregister = unregisterConfig.unregister === true || !hasSwEntries;
1542
1587
  if (shouldUnregister) {
@@ -1631,11 +1676,24 @@ function createMokupPlugin(options = {}) {
1631
1676
  if (id === swVirtualId) {
1632
1677
  return resolvedSwVirtualId;
1633
1678
  }
1679
+ if (id === swLifecycleVirtualId) {
1680
+ return resolvedSwLifecycleVirtualId;
1681
+ }
1634
1682
  return null;
1635
1683
  },
1636
1684
  async load(id) {
1637
1685
  if (id !== resolvedSwVirtualId) {
1638
- return null;
1686
+ if (id !== resolvedSwLifecycleVirtualId) {
1687
+ return null;
1688
+ }
1689
+ if (!swLifecycleScript) {
1690
+ if (swRoutes.length === 0) {
1691
+ await refreshRoutes();
1692
+ }
1693
+ const importPath = await resolveSwModuleImport(this);
1694
+ swLifecycleScript = buildSwLifecycleScript(importPath);
1695
+ }
1696
+ return swLifecycleScript ?? "";
1639
1697
  }
1640
1698
  if (swRoutes.length === 0) {
1641
1699
  await refreshRoutes();
@@ -1647,11 +1705,23 @@ function createMokupPlugin(options = {}) {
1647
1705
  });
1648
1706
  },
1649
1707
  async buildStart() {
1650
- if (!swConfig || command !== "build") {
1708
+ if (command !== "build") {
1651
1709
  return;
1652
1710
  }
1653
1711
  await refreshRoutes();
1654
- if (!hasSwRoutes()) {
1712
+ const shouldInject = buildSwLifecycleScript() !== null;
1713
+ swLifecycleScript = null;
1714
+ if (shouldInject) {
1715
+ swLifecycleFileName = resolveAssetsFileName("mokup-sw-lifecycle.js");
1716
+ this.emitFile({
1717
+ type: "chunk",
1718
+ id: swLifecycleVirtualId,
1719
+ fileName: swLifecycleFileName
1720
+ });
1721
+ } else {
1722
+ swLifecycleFileName = null;
1723
+ }
1724
+ if (!swConfig || !hasSwRoutes()) {
1655
1725
  return;
1656
1726
  }
1657
1727
  const fileName = swConfig.path.startsWith("/") ? swConfig.path.slice(1) : swConfig.path;
@@ -1669,6 +1739,22 @@ function createMokupPlugin(options = {}) {
1669
1739
  if (!script) {
1670
1740
  return html;
1671
1741
  }
1742
+ if (command === "build") {
1743
+ if (!swLifecycleFileName) {
1744
+ return html;
1745
+ }
1746
+ const src = resolveHtmlAssetPath(swLifecycleFileName);
1747
+ return {
1748
+ html,
1749
+ tags: [
1750
+ {
1751
+ tag: "script",
1752
+ attrs: { type: "module", src },
1753
+ injectTo: "head"
1754
+ }
1755
+ ]
1756
+ };
1757
+ }
1672
1758
  return {
1673
1759
  html,
1674
1760
  tags: [
@@ -1685,6 +1771,7 @@ function createMokupPlugin(options = {}) {
1685
1771
  root = config.root;
1686
1772
  base = config.base ?? "/";
1687
1773
  command = config.command;
1774
+ assetsDir = config.build.assetsDir ?? "assets";
1688
1775
  },
1689
1776
  async configureServer(server) {
1690
1777
  currentServer = server;
package/dist/vite.mjs CHANGED
@@ -1,13 +1,13 @@
1
+ import { promises, existsSync } from 'node:fs';
1
2
  import { cwd } from 'node:process';
3
+ import { pathToFileURL, fileURLToPath } from 'node:url';
2
4
  import chokidar from 'chokidar';
3
5
  import { Buffer } from 'node:buffer';
4
6
  import { Hono } from 'hono';
5
7
  import { PatternRouter } from 'hono/router/pattern-router';
6
8
  import { resolve, isAbsolute, join, normalize, extname, dirname, relative, basename } from 'pathe';
7
- import { promises } from 'node:fs';
8
9
  import { createRequire } from 'node:module';
9
10
  import { compareRouteScore, parseRouteTemplate } from '@mokup/runtime';
10
- import { pathToFileURL } from 'node:url';
11
11
  import { build } from 'esbuild';
12
12
  import { parse } from 'jsonc-parser';
13
13
 
@@ -588,6 +588,7 @@ function createPlaygroundMiddleware(params) {
588
588
  const routes = params.getRoutes();
589
589
  sendJson(res, {
590
590
  basePath: matchedPath,
591
+ root: baseRoot,
591
592
  count: routes.length,
592
593
  groups: groups.map((group) => ({ key: group.key, label: group.label })),
593
594
  routes: routes.map((route) => toPlaygroundRoute(route, baseRoot, groups))
@@ -1481,6 +1482,19 @@ function resolveSwRuntimeImportPath(base) {
1481
1482
  const normalizedBase = normalizeBase(base);
1482
1483
  return `${normalizedBase}@id/mokup/runtime`;
1483
1484
  }
1485
+ const swModuleCandidates = [
1486
+ new URL("../sw.ts", import.meta.url),
1487
+ new URL("../sw.js", import.meta.url)
1488
+ ];
1489
+ const localSwModulePath = (() => {
1490
+ for (const candidate of swModuleCandidates) {
1491
+ const filePath = fileURLToPath(candidate);
1492
+ if (existsSync(filePath)) {
1493
+ return filePath;
1494
+ }
1495
+ }
1496
+ return fileURLToPath(swModuleCandidates[0] ?? new URL("../sw.ts", import.meta.url));
1497
+ })();
1484
1498
  function hasMiddlewareStack(middlewares) {
1485
1499
  const candidate = middlewares;
1486
1500
  return Array.isArray(candidate.stack);
@@ -1496,6 +1510,7 @@ function createMokupPlugin(options = {}) {
1496
1510
  let root = cwd();
1497
1511
  let base = "/";
1498
1512
  let command = "serve";
1513
+ let assetsDir = "assets";
1499
1514
  let routes = [];
1500
1515
  let serverRoutes = [];
1501
1516
  let swRoutes = [];
@@ -1528,8 +1543,38 @@ function createMokupPlugin(options = {}) {
1528
1543
  const hasSwRoutes = () => !!swConfig && swRoutes.length > 0;
1529
1544
  const resolveSwRequestPath = (path) => resolveRegisterPath(base, path);
1530
1545
  const resolveSwRegisterScope = (scope) => resolveRegisterScope(base, scope);
1546
+ const resolveHtmlAssetPath = (fileName) => {
1547
+ const normalizedFileName = fileName.startsWith("/") ? fileName.slice(1) : fileName;
1548
+ if (base && base.startsWith(".")) {
1549
+ return normalizedFileName;
1550
+ }
1551
+ const normalizedBase = normalizeBase(base);
1552
+ return `${normalizedBase}${normalizedFileName}`;
1553
+ };
1554
+ const resolveAssetsFileName = (fileName) => {
1555
+ const trimmed = assetsDir.replace(/^\/+|\/+$/g, "");
1556
+ if (!trimmed) {
1557
+ return fileName;
1558
+ }
1559
+ return `${trimmed}/${fileName}`;
1560
+ };
1531
1561
  const swVirtualId = "virtual:mokup-sw";
1532
1562
  const resolvedSwVirtualId = `\0${swVirtualId}`;
1563
+ const swLifecycleVirtualId = "virtual:mokup-sw-lifecycle";
1564
+ const resolvedSwLifecycleVirtualId = `\0${swLifecycleVirtualId}`;
1565
+ let swLifecycleFileName = null;
1566
+ let swLifecycleScript = null;
1567
+ async function resolveSwModuleImport(context) {
1568
+ const resolved = await context.resolve("mokup/sw");
1569
+ if (resolved?.id) {
1570
+ return resolved.id;
1571
+ }
1572
+ const fallbackResolved = await context.resolve(localSwModulePath);
1573
+ if (fallbackResolved?.id) {
1574
+ return fallbackResolved.id;
1575
+ }
1576
+ return localSwModulePath;
1577
+ }
1533
1578
  function buildSwLifecycleScript(importPath = "mokup/sw") {
1534
1579
  const shouldUnregister = unregisterConfig.unregister === true || !hasSwEntries;
1535
1580
  if (shouldUnregister) {
@@ -1624,11 +1669,24 @@ function createMokupPlugin(options = {}) {
1624
1669
  if (id === swVirtualId) {
1625
1670
  return resolvedSwVirtualId;
1626
1671
  }
1672
+ if (id === swLifecycleVirtualId) {
1673
+ return resolvedSwLifecycleVirtualId;
1674
+ }
1627
1675
  return null;
1628
1676
  },
1629
1677
  async load(id) {
1630
1678
  if (id !== resolvedSwVirtualId) {
1631
- return null;
1679
+ if (id !== resolvedSwLifecycleVirtualId) {
1680
+ return null;
1681
+ }
1682
+ if (!swLifecycleScript) {
1683
+ if (swRoutes.length === 0) {
1684
+ await refreshRoutes();
1685
+ }
1686
+ const importPath = await resolveSwModuleImport(this);
1687
+ swLifecycleScript = buildSwLifecycleScript(importPath);
1688
+ }
1689
+ return swLifecycleScript ?? "";
1632
1690
  }
1633
1691
  if (swRoutes.length === 0) {
1634
1692
  await refreshRoutes();
@@ -1640,11 +1698,23 @@ function createMokupPlugin(options = {}) {
1640
1698
  });
1641
1699
  },
1642
1700
  async buildStart() {
1643
- if (!swConfig || command !== "build") {
1701
+ if (command !== "build") {
1644
1702
  return;
1645
1703
  }
1646
1704
  await refreshRoutes();
1647
- if (!hasSwRoutes()) {
1705
+ const shouldInject = buildSwLifecycleScript() !== null;
1706
+ swLifecycleScript = null;
1707
+ if (shouldInject) {
1708
+ swLifecycleFileName = resolveAssetsFileName("mokup-sw-lifecycle.js");
1709
+ this.emitFile({
1710
+ type: "chunk",
1711
+ id: swLifecycleVirtualId,
1712
+ fileName: swLifecycleFileName
1713
+ });
1714
+ } else {
1715
+ swLifecycleFileName = null;
1716
+ }
1717
+ if (!swConfig || !hasSwRoutes()) {
1648
1718
  return;
1649
1719
  }
1650
1720
  const fileName = swConfig.path.startsWith("/") ? swConfig.path.slice(1) : swConfig.path;
@@ -1662,6 +1732,22 @@ function createMokupPlugin(options = {}) {
1662
1732
  if (!script) {
1663
1733
  return html;
1664
1734
  }
1735
+ if (command === "build") {
1736
+ if (!swLifecycleFileName) {
1737
+ return html;
1738
+ }
1739
+ const src = resolveHtmlAssetPath(swLifecycleFileName);
1740
+ return {
1741
+ html,
1742
+ tags: [
1743
+ {
1744
+ tag: "script",
1745
+ attrs: { type: "module", src },
1746
+ injectTo: "head"
1747
+ }
1748
+ ]
1749
+ };
1750
+ }
1665
1751
  return {
1666
1752
  html,
1667
1753
  tags: [
@@ -1678,6 +1764,7 @@ function createMokupPlugin(options = {}) {
1678
1764
  root = config.root;
1679
1765
  base = config.base ?? "/";
1680
1766
  command = config.command;
1767
+ assetsDir = config.build.assetsDir ?? "assets";
1681
1768
  },
1682
1769
  async configureServer(server) {
1683
1770
  currentServer = server;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mokup",
3
3
  "type": "module",
4
- "version": "0.2.1",
4
+ "version": "0.2.2",
5
5
  "description": "Mock utilities and Vite plugin for mokup.",
6
6
  "license": "MIT",
7
7
  "homepage": "https://mokup.icebreaker.top",
@@ -68,10 +68,10 @@
68
68
  "hono": "^4.11.4",
69
69
  "jsonc-parser": "^3.3.1",
70
70
  "pathe": "^2.0.3",
71
+ "@mokup/playground": "0.0.4",
72
+ "@mokup/server": "0.0.2",
71
73
  "@mokup/cli": "0.2.0",
72
- "@mokup/playground": "0.0.3",
73
- "@mokup/runtime": "0.1.0",
74
- "@mokup/server": "0.0.2"
74
+ "@mokup/runtime": "0.1.0"
75
75
  },
76
76
  "devDependencies": {
77
77
  "@types/node": "^25.0.9",