sveltekit-temporal 0.2.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 +3 -0
- package/bin/cli.js +1 -1
- package/dist/addon.js +129 -0
- package/dist/index.js +929 -0
- package/package.json +11 -5
- package/src/addon.js +0 -54
- package/src/index.js +0 -256
- package/src/shared.js +0 -76
package/README.md
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
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
|
|
|
7
10
|
## Using with the Svelte CLI (`sv add`)
|
package/bin/cli.js
CHANGED
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
|
+
};
|