deno.land / x / lume@v2.1.4 / plugins / on_demand.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { merge } from "../core/utils/object.ts";import { log } from "../core/utils/log.ts";import onDemand, { getRouter, MiddlewareOptions,} from "../middlewares/on_demand.ts";import { posix } from "../deps/path.ts";
import type Site from "../core/site.ts";import type { Page } from "../core/file.ts";
export interface Options { /** The file path to save the routes */ routesPath?: string;
/** The file path to save the preloaded modules */ preloadPath?: string;
/** Extra data to pass to the pages */ extraData?: (request: Request) => Record<string, unknown>;}
// Default optionsexport const defaults: Options = { routesPath: "/_routes.json", preloadPath: "/_preload.ts",};
/** A plugin to generate pages on demand in the server side */export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return (site: Site) => { const collector = new JsonRouterCollector({ root: site.root(), routesPath: options.routesPath, preloadPath: options.preloadPath, src: site.options.src, });
// Collect and save the routes automatically site.addEventListener("beforeSave", async () => { collector.collectRoutes(site.onDemandPages); const specifiers: string[] = [];
for (const [path, entry] of site.fs.entries) { const ext = posix.extname(path);
switch (ext) { case ".ts": case ".tsx": case ".js": case ".jsx": case ".mjs": specifiers.push(entry.flags.has("remote") ? entry.src : path); break;
default: break; } } await collector.saveRoutes(specifiers); });
// Ignore the routes files by the watcher site.options.watcher.ignore.push(options.routesPath); site.options.watcher.ignore.push(options.preloadPath);
// Add the ondemand middleware site.options.server.middlewares ||= [];
site._data.on_demand = { extraData: options.extraData, routesFile: site.root(options.routesPath), } as MiddlewareOptions;
site.options.server.middlewares.push(onDemand({ site, router: getRouter(collector.routes), })); };}
interface JsonRouterCollectorOptions { root: string; routesPath: string; preloadPath: string; src: string;}
/** * Class to load and manage static routes in a JSON file * Used by default if no router is provided */export class JsonRouterCollector { options: JsonRouterCollectorOptions;
/** Pages that must be generated on demand */ routes = new Map<string, string>();
constructor(options: JsonRouterCollectorOptions) { this.options = options; }
/** Collect the routes of all pages with data.ondemand = true */ collectRoutes(pages: Page[]): void { this.routes.clear();
pages.forEach((page) => { this.routes.set( page.data.url, page.src.path + (page.src.ext || ""), ); }); }
/** Save the routes into the routesFile */ async saveRoutes(specifiers: string[]): Promise<void> { if (!this.routes.size) { return; }
const routesFile = posix.join(this.options.root, this.options.routesPath); const preloadFile = posix.join(this.options.root, this.options.preloadPath); const data: Record<string, string> = {};
this.routes.forEach((path, url) => { data[url] = path; });
// Write the routes file await Deno.writeTextFile(routesFile, `${JSON.stringify(data, null, 2)}\n`);
log.info( `[on_demand plugin] Routes saved at <gray>${routesFile}</gray>`, );
// Write the preload file if (specifiers.length && Object.keys(data).length) { const code = [ "/**", " * Don't execute this function", " * It's just statically analyzable so dynamic imports work on Deno Deploy", " * @see https://deno.com/deploy/changelog#statically-analyzable-dynamic-imports", " */", "export function toStaticallyAnalyzableDynamicImports() {", ...specifiers.map((path) => ` import("./${posix.join(".", this.options.src, path)}");` ), "}", "", ].join("\n");
await Deno.writeTextFile(preloadFile, code);
log.info( `[on_demand plugin] Preloader saved at <gray>${preloadFile}</gray>`, ); } }}
lume

Version Info

Tagged at
5 months ago