forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_entry.ts
More file actions
58 lines (54 loc) · 1.5 KB
/
client_entry.ts
File metadata and controls
58 lines (54 loc) · 1.5 KB
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
import type { Plugin } from "vite";
import { pathWithRoot, type ResolvedFreshViteConfig } from "../utils.ts";
export function clientEntryPlugin(options: ResolvedFreshViteConfig): Plugin {
const modName = "fresh:client-entry";
const modNameUser = "fresh:client-entry-user";
let clientEntry = "";
let isDev = false;
return {
name: "fresh:client-entry",
sharedDuringBuild: true,
config(_, env) {
isDev = env.command === "serve";
},
applyToEnvironment(env) {
return env.config.consumer === "client";
},
configResolved(config) {
clientEntry = pathWithRoot(options.clientEntry, config.root);
},
resolveId: {
filter: {
id: /(fresh:client-entry|fresh:client-entry-user|fresh:client-quirks)/,
},
handler(id) {
if (id === modName) {
return `\0${modName}`;
} else if (id === "fresh:client-quirks") {
return "@fresh/plugin-vite/client";
} else if (id === modNameUser) {
return clientEntry;
}
},
},
load: {
filter: {
id: /\0fresh:client-entry/,
},
async handler() {
let exists = false;
try {
const stat = await Deno.stat(clientEntry);
exists = stat.isFile;
} catch {
exists = false;
}
return `
${isDev ? 'import "preact/debug"' : ""}
export * from "fresh/runtime-client";
${exists ? `import "fresh:client-entry-user";` : ""}
import "@fresh/plugin-vite/client";`;
},
},
};
}