sveltekit-temporal 0.1.0 → 0.3.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
@@ -1,10 +1,26 @@
1
1
  # sveltekit-temporal
2
2
 
3
+ Think about it, temporal may be supported in big 3 wrosers soon, but your users may bit update for some time.
4
+
3
5
  One-shot CLI that wires the Temporal API polyfill into a SvelteKit project, with conditional loading so browsers that ship Temporal natively (Chrome 144+, Firefox 139+) and server runtimes that do too (Node.js 26+) pay zero bytes.
4
6
 
7
+
5
8
  ## Usage
6
9
 
10
+ ## Using with the Svelte CLI (`sv add`)
11
+
12
+ If you prefer the official Svelte CLI, this package also works as an `sv` add-on:
13
+
7
14
  ```bash
15
+ # Or when creating a new project
16
+ npx sv create my-app
17
+ cd my-app
18
+ npx sv add sveltekit-temporal
19
+
20
+ # Into an existing project
21
+ npx sv add sveltekit-temporal
22
+
23
+ #Alternatively, you can run the CLI directly
8
24
  npx sveltekit-temporal
9
25
  ```
10
26
 
@@ -59,18 +75,18 @@ export async function handle({ event, resolve }) {
59
75
 
60
76
  ## Runtime support
61
77
 
62
- | Runtime | Native Temporal | Notes |
63
- |---------|----------------|-------|
64
- | Node.js ≤ 25 | No | Polyfill required |
65
- | Node.js 26+ | Yes (behind `--experimental-temporal`) | Polyfill still safe to include — the conditional check skips loading it |
66
- | Bun | No | Polyfill required; Bun tracks V8 but has not shipped Temporal yet |
67
- | Deno | No | Polyfill required; Deno tracks V8 but has not shipped Temporal yet |
68
- | Chrome 144+ | Yes | Polyfill skipped automatically |
69
- | Firefox 139+ | Yes | Polyfill skipped automatically |
70
- | Safari | No | Polyfill required |
78
+ | Runtime | Native Temporal | Notes |
79
+ | ------------ | -------------------------------------- | ----------------------------------------------------------------------- |
80
+ | Node.js ≤ 25 | No | Polyfill required |
81
+ | Node.js 26+ | Yes (behind `--experimental-temporal`) | Polyfill still safe to include — the conditional check skips loading it |
82
+ | Bun | No | Polyfill required; Bun tracks V8 but has not shipped Temporal yet |
83
+ | Deno | No | Polyfill required; Deno tracks V8 but has not shipped Temporal yet |
84
+ | Chrome 144+ | Yes | Polyfill skipped automatically |
85
+ | Firefox 139+ | Yes | Polyfill skipped automatically |
86
+ | Safari | No | Polyfill required |
71
87
 
72
88
  The conditional bootstrap in `src/lib/temporal.ts` checks `typeof Temporal !== 'undefined'` before loading the polyfill, so upgrading your runtime later requires no code changes.
73
89
 
74
90
  ## Switching polyfills later
75
91
 
76
- Just re-run the CLI and pick the other option. Only the two files that name the package (`src/lib/temporal.*` and `src/app.d.ts`) will change.
92
+ Re-run either the CLI or `sv add` and pick the other option. Only the two files that name the package (`src/lib/temporal.*` and `src/app.d.ts`) will change.
package/bin/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { run } from '../src/index.js';
2
+ import { run } from '../dist/index.js';
3
3
 
4
4
  run().catch((err) => {
5
5
  console.error(err);
package/dist/addon.js ADDED
@@ -0,0 +1,129 @@
1
+ // src/addon.js
2
+ import { defineAddon, defineAddonOptions } from "sv";
3
+
4
+ // src/shared.js
5
+ var MARKER = "// sveltekit-temporal: managed block";
6
+ var END_MARKER = "// end sveltekit-temporal managed block";
7
+ function buildBootstrap(pkgName, isTypeScript) {
8
+ const tsLine1 = isTypeScript ? ` // @ts-expect-error - polyfilling globalThis
9
+ ` : "";
10
+ const tsLine2 = isTypeScript ? ` // @ts-expect-error - patching Date.prototype
11
+ ` : "";
12
+ return `${MARKER}
13
+ // Conditionally load the Temporal polyfill when the runtime lacks native support.
14
+ // Native (Chrome 144+, Firefox 139+) keeps zero overhead \u2014 the dynamic import is
15
+ // code-split by Vite and only fetched on browsers that need it.
16
+
17
+ if (typeof globalThis.Temporal === 'undefined') {
18
+ const { Temporal, toTemporalInstant } = await import('${pkgName}');
19
+ ${tsLine1} globalThis.Temporal = Temporal;
20
+ ${tsLine2} Date.prototype.toTemporalInstant = toTemporalInstant;
21
+ }
22
+
23
+ export {};
24
+ `;
25
+ }
26
+ function buildAppDtsBlock(pkgName) {
27
+ return `${MARKER}
28
+ import type { Temporal as TemporalNS } from '${pkgName}';
29
+
30
+ declare global {
31
+ const Temporal: typeof TemporalNS;
32
+
33
+ interface Date {
34
+ toTemporalInstant(): TemporalNS.Instant;
35
+ }
36
+ }
37
+ ${END_MARKER}`;
38
+ }
39
+ function buildNewAppDts(pkgName) {
40
+ return `// See https://svelte.dev/docs/kit/types#app.d.ts for information about these interfaces
41
+ declare global {
42
+ namespace App {
43
+ // interface Error {}
44
+ // interface Locals {}
45
+ // interface PageData {}
46
+ // interface PageState {}
47
+ // interface Platform {}
48
+ }
49
+ }
50
+
51
+ ${buildAppDtsBlock(pkgName)}
52
+
53
+ export {};
54
+ `;
55
+ }
56
+ function processAppDts(content, pkgName) {
57
+ const block = buildAppDtsBlock(pkgName);
58
+ if (content.includes(MARKER)) {
59
+ const pattern = new RegExp(
60
+ `${escapeRegex(MARKER)}[\\s\\S]*?${escapeRegex(END_MARKER)}\\n?`,
61
+ "m"
62
+ );
63
+ const replaced = content.replace(pattern, `${block}
64
+ `);
65
+ return replaced === content ? false : replaced;
66
+ }
67
+ const exportMatch = content.match(/\n\s*export\s*\{\s*\};?\s*$/);
68
+ if (exportMatch) {
69
+ return content.replace(exportMatch[0], `
70
+
71
+ ${block}
72
+ ${exportMatch[0]}`);
73
+ }
74
+ return `${content.trimEnd()}
75
+
76
+ ${block}
77
+
78
+ export {};
79
+ `;
80
+ }
81
+ function escapeRegex(s) {
82
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
83
+ }
84
+
85
+ // src/addon.js
86
+ var options = defineAddonOptions().add("polyfill", {
87
+ question: "Which Temporal polyfill would you like to use?",
88
+ type: "select",
89
+ options: ["@js-temporal/polyfill", "temporal-polyfill"],
90
+ default: "@js-temporal/polyfill"
91
+ }).build();
92
+ var addon_default = defineAddon({
93
+ id: "temporal",
94
+ shortDescription: "Temporal API polyfill with conditional loading for SvelteKit",
95
+ options,
96
+ setup({ isKit, unsupported }) {
97
+ if (!isKit) unsupported("Requires SvelteKit.");
98
+ },
99
+ run({ sv, options: options2, language, directory }) {
100
+ const pkgName = options2.polyfill;
101
+ const isTs = language === "ts";
102
+ const ext = language;
103
+ sv.dependency(pkgName, "latest");
104
+ sv.file(`${directory.lib}/temporal.${ext}`, (content) => {
105
+ if (content && !content.includes(MARKER)) return false;
106
+ return buildBootstrap(pkgName, isTs);
107
+ });
108
+ sv.file(
109
+ `${directory.kitRoutes}/+layout.${ext}`,
110
+ (content) => content.includes("import '$lib/temporal'") ? false : `import '$lib/temporal';
111
+ ${content}`
112
+ );
113
+ sv.file(
114
+ `${directory.src}/hooks.server.${ext}`,
115
+ (content) => content.includes("import '$lib/temporal'") ? false : `import '$lib/temporal';
116
+ ${content}`
117
+ );
118
+ if (isTs) {
119
+ sv.file(
120
+ `${directory.src}/app.d.ts`,
121
+ (content) => content.trim() ? processAppDts(content, pkgName) : buildNewAppDts(pkgName)
122
+ );
123
+ }
124
+ },
125
+ nextSteps: () => ["Reference Temporal directly anywhere in your app \u2014 no imports needed."]
126
+ });
127
+ export {
128
+ addon_default as default
129
+ };