@decocms/blocks 8.2.0 → 8.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/protocol/server/methods/changes.d.ts.map +1 -1
- package/dist/protocol/server/methods/changes.js +4 -0
- package/dist/protocol/server/methods/changes.js.map +1 -1
- package/dist/protocol/storage/fs/git.d.ts +13 -1
- package/dist/protocol/storage/fs/git.d.ts.map +1 -1
- package/dist/protocol/storage/fs/git.js +108 -6
- package/dist/protocol/storage/fs/git.js.map +1 -1
- package/dist/protocol/storage/fs/index.d.ts.map +1 -1
- package/dist/protocol/storage/fs/index.js +19 -1
- package/dist/protocol/storage/fs/index.js.map +1 -1
- package/dist/protocol/storage.d.ts +5 -1
- package/dist/protocol/storage.d.ts.map +1 -1
- package/dist/protocol/storage.js.map +1 -1
- package/dist/v8/cms.d.ts +14 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +36 -0
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/vite/hot.d.ts +8 -0
- package/dist/v8/vite/hot.d.ts.map +1 -0
- package/dist/v8/vite/hot.js +8 -0
- package/dist/v8/vite/hot.js.map +1 -0
- package/dist/v8/vite/index.d.ts +52 -0
- package/dist/v8/vite/index.d.ts.map +1 -0
- package/dist/v8/vite/index.js +136 -0
- package/dist/v8/vite/index.js.map +1 -0
- package/package.json +20 -3
- package/src/protocol/server/methods/changes.ts +4 -0
- package/src/protocol/storage/fs/git.ts +124 -7
- package/src/protocol/storage/fs/index.ts +19 -1
- package/src/protocol/storage.ts +5 -1
- package/src/v8/cms.ts +36 -0
- package/src/v8/vite/hot.ts +7 -0
- package/src/v8/vite/index.ts +170 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@decocms/blocks/vite`: `decoContent()`, the Vite plugin that shows a saved
|
|
3
|
+
* block in open pages during `vite dev`. Vite sites add it to
|
|
4
|
+
* `vite.config.ts`; Next.js sites don't use it.
|
|
5
|
+
*
|
|
6
|
+
* `deco serve` (the site editor's local server) rewrites
|
|
7
|
+
* `.deco/blocks.gen.ts` on every save. In `vite dev`, in the server
|
|
8
|
+
* environments, the plugin makes each module that imports it (usually
|
|
9
|
+
* `src/cms.ts`) accept that import's updates: the new content goes into the
|
|
10
|
+
* CMS already created for its `.deco` folder (`adoptContent`), and only then
|
|
11
|
+
* does the server tell open pages to reload. The importer is the update's
|
|
12
|
+
* boundary, so neither it nor anything that imports it is invalidated or runs
|
|
13
|
+
* again: TanStack Start re-imports its server entry on every request, and a
|
|
14
|
+
* content module that accepted itself still left every module above it stale,
|
|
15
|
+
* so `src/cms.ts` re-ran on every save. Reloading on the file change itself is
|
|
16
|
+
* too early: the page would render the previous save.
|
|
17
|
+
*
|
|
18
|
+
* A module that already accepts the content module's updates itself (the
|
|
19
|
+
* template code sites carried before this plugin) is left as it is.
|
|
20
|
+
*
|
|
21
|
+
* The files on disk are unchanged; `vite build` doesn't use the plugin. It
|
|
22
|
+
* needs Vite 6 or newer (the Environment API) and works with
|
|
23
|
+
* `@cloudflare/vite-plugin` (whatever its worker environment is named) and
|
|
24
|
+
* with plain SSR environments.
|
|
25
|
+
*/
|
|
26
|
+
import type { Plugin } from "vite";
|
|
27
|
+
|
|
28
|
+
/** The event an importer of the content module sends from a server environment once the CMS has its new content. */
|
|
29
|
+
export const CONTENT_UPDATED_EVENT = "decocms:content-updated";
|
|
30
|
+
|
|
31
|
+
/** How long the plugin waits for another update of the same save before reloading open pages. */
|
|
32
|
+
const RELOAD_DELAY_MS = 100;
|
|
33
|
+
|
|
34
|
+
/** The module the footer imports `adoptContent` from. */
|
|
35
|
+
const HOT_MODULE = "@decocms/blocks/vite/hot";
|
|
36
|
+
|
|
37
|
+
/** The content module `deco content` writes, wherever the `.deco` folder is. */
|
|
38
|
+
const CONTENT_MODULE = /(?:^|[\\/])\.deco[\\/]blocks\.gen\.ts$/;
|
|
39
|
+
|
|
40
|
+
/** Source files whose imports the plugin reads. */
|
|
41
|
+
const SOURCE_FILE = /\.(?:[cm]?[jt]sx?)$/;
|
|
42
|
+
|
|
43
|
+
/** Static imports and re-exports: `import x from "s"`, `import "s"`, `export { x } from "s"`. */
|
|
44
|
+
const STATIC_IMPORT =
|
|
45
|
+
/(?:^|[^\w$.])(?:import|export)(?:\s+type)?\s*(?:[\w$*{}\s,]*?\s*from\s*)?(["'])([^"'\r\n]+)\1/g;
|
|
46
|
+
|
|
47
|
+
/** `….accept("s", …)` or `….accept(["s", …], …)`: a module accepting a dependency's updates. */
|
|
48
|
+
const ACCEPT_CALL = /\.accept\s*\(\s*(\[[^\]]*\]|(["'`])[^"'`\r\n]+\2)/g;
|
|
49
|
+
|
|
50
|
+
/** Specifiers that never name a file of the site. */
|
|
51
|
+
const EXTERNAL = /^(?:node:|virtual:|\0|data:|https?:|cloudflare:)/;
|
|
52
|
+
|
|
53
|
+
/** Whether a module id (possibly with a query) is a site's `.deco/blocks.gen.ts`. */
|
|
54
|
+
export function isContentModule(id: string): boolean {
|
|
55
|
+
const file = id.split("?", 1)[0] ?? id;
|
|
56
|
+
if (/[\\/]node_modules[\\/]/.test(file)) return false;
|
|
57
|
+
return CONTENT_MODULE.test(file);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The specifiers of a module's static imports and re-exports, in order, without duplicates. */
|
|
61
|
+
export function staticImports(code: string): string[] {
|
|
62
|
+
const found = new Set<string>();
|
|
63
|
+
for (const match of code.matchAll(STATIC_IMPORT)) found.add(match[2] as string);
|
|
64
|
+
return [...found];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The dependency specifiers a module passes to `import.meta.hot.accept`. */
|
|
68
|
+
export function acceptedSpecifiers(code: string): string[] {
|
|
69
|
+
const found = new Set<string>();
|
|
70
|
+
for (const match of code.matchAll(ACCEPT_CALL)) {
|
|
71
|
+
for (const literal of (match[1] as string).matchAll(/(["'`])([^"'`\r\n]+)\1/g)) {
|
|
72
|
+
found.add(literal[2] as string);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return [...found];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* What the plugin appends to a module that imports the content module (as
|
|
80
|
+
* `specifier`) in a server environment: accept that import's next version,
|
|
81
|
+
* hand it to the CMS, then announce it. The same code sites used to write in
|
|
82
|
+
* `src/cms.ts` by hand.
|
|
83
|
+
*/
|
|
84
|
+
export function importerFooter(specifier: string): string {
|
|
85
|
+
return [
|
|
86
|
+
"",
|
|
87
|
+
"// Added by decoContent() (@decocms/blocks/vite) in vite dev only.",
|
|
88
|
+
`import { adoptContent as __decoAdoptContent } from ${JSON.stringify(HOT_MODULE)};`,
|
|
89
|
+
"if (import.meta.hot) {",
|
|
90
|
+
` import.meta.hot.accept(${JSON.stringify(specifier)}, (next) => {`,
|
|
91
|
+
" if (next) __decoAdoptContent(next.default);",
|
|
92
|
+
` import.meta.hot?.send(${JSON.stringify(CONTENT_UPDATED_EVENT)});`,
|
|
93
|
+
" });",
|
|
94
|
+
"}",
|
|
95
|
+
"",
|
|
96
|
+
].join("\n");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The Vite plugin: in `vite dev`, open pages show a saved block once the
|
|
101
|
+
* server has it. Add it to `plugins` in `vite.config.ts`:
|
|
102
|
+
*
|
|
103
|
+
* ```ts
|
|
104
|
+
* import { decoContent } from "@decocms/blocks/vite";
|
|
105
|
+
* export default defineConfig({ plugins: [decoContent(), ...] });
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export function decoContent(): Plugin {
|
|
109
|
+
return {
|
|
110
|
+
name: "decocms:content",
|
|
111
|
+
apply: "serve",
|
|
112
|
+
configEnvironment(name, config) {
|
|
113
|
+
// The footer's import appears only after this transform, so the dependency
|
|
114
|
+
// scan can't see it: pre-bundle it up front in the server environments.
|
|
115
|
+
// Discovered on the first request instead, the worker re-optimizes its
|
|
116
|
+
// dependencies and reloads the whole program once.
|
|
117
|
+
if (name === "client" || config.consumer === "client") return;
|
|
118
|
+
return { optimizeDeps: { include: [HOT_MODULE] } };
|
|
119
|
+
},
|
|
120
|
+
async transform(code, id, options) {
|
|
121
|
+
const file = id.split("?", 1)[0] ?? id;
|
|
122
|
+
if (id.startsWith("\0") || !SOURCE_FILE.test(file) || /[\\/]node_modules[\\/]/.test(file)) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
if (isContentModule(id)) return null;
|
|
126
|
+
const consumer = this.environment?.config.consumer;
|
|
127
|
+
const server = consumer === undefined ? options?.ssr === true : consumer === "server";
|
|
128
|
+
if (!server) return null;
|
|
129
|
+
|
|
130
|
+
const imports = staticImports(code).filter((specifier) => !EXTERNAL.test(specifier));
|
|
131
|
+
if (imports.length === 0) return null;
|
|
132
|
+
const isContent = async (specifier: string) => {
|
|
133
|
+
const resolved = await this.resolve(specifier, id);
|
|
134
|
+
return resolved !== null && !resolved.external && isContentModule(resolved.id);
|
|
135
|
+
};
|
|
136
|
+
let content: string | undefined;
|
|
137
|
+
for (const specifier of imports) {
|
|
138
|
+
if (await isContent(specifier)) {
|
|
139
|
+
content = specifier;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (content === undefined) return null;
|
|
144
|
+
// A site that still accepts the content module's updates by hand keeps its own code.
|
|
145
|
+
for (const specifier of acceptedSpecifiers(code)) {
|
|
146
|
+
if (await isContent(specifier)) return null;
|
|
147
|
+
}
|
|
148
|
+
// Appended, so no line moves and the incoming source map still holds.
|
|
149
|
+
return { code: code + importerFooter(content), map: null };
|
|
150
|
+
},
|
|
151
|
+
configureServer(server) {
|
|
152
|
+
// A save writes the block's JSON file and then the content module, and
|
|
153
|
+
// each one swaps the module: open pages reload once, after the last.
|
|
154
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
155
|
+
const reload = () => {
|
|
156
|
+
clearTimeout(timer);
|
|
157
|
+
timer = setTimeout(() => {
|
|
158
|
+
const client = server.environments?.client?.hot ?? server.hot;
|
|
159
|
+
client.send({ type: "full-reload" });
|
|
160
|
+
}, RELOAD_DELAY_MS);
|
|
161
|
+
};
|
|
162
|
+
const environments = Object.values(server.environments ?? {}).filter(
|
|
163
|
+
(environment) => environment.config.consumer === "server",
|
|
164
|
+
);
|
|
165
|
+
for (const environment of environments) {
|
|
166
|
+
environment.hot.on(CONTENT_UPDATED_EVENT, reload);
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|