-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
serverEntryModulePlugin.ts
72 lines (68 loc) · 2.21 KB
/
serverEntryModulePlugin.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
import type { Plugin } from "esbuild";
import type { RemixConfig } from "../../config";
import {
serverBuildVirtualModule,
assetsManifestVirtualModule,
} from "../virtualModules";
/**
* Creates a virtual module called `@remix-run/dev/server-build` that exports the
* compiled server build for consumption in remix request handlers. This allows
* for you to consume the build in a custom server entry that is also fed through
* the compiler.
*/
export function serverEntryModulePlugin(config: RemixConfig): Plugin {
let filter = serverBuildVirtualModule.filter;
return {
name: "server-entry-module",
setup(build) {
build.onResolve({ filter }, ({ path }) => {
return {
path,
namespace: "server-entry-module",
};
});
build.onLoad({ filter }, async () => {
return {
resolveDir: config.appDirectory,
loader: "js",
contents: `
import * as entryServer from ${JSON.stringify(`./${config.entryServerFile}`)};
${Object.keys(config.routes)
.map((key, index) => {
// IMPORTANT: Any values exported from this generated module must also be
// typed in `packages/remix-dev/server-build.ts` to avoid tsc errors.
let route = config.routes[key];
return `import * as route${index} from ${JSON.stringify(
`./${route.file}`
)};`;
})
.join("\n")}
export { default as assets } from ${JSON.stringify(
assetsManifestVirtualModule.id
)};
export const assetsBuildDirectory = ${JSON.stringify(
config.relativeAssetsBuildDirectory
)};
export const future = ${JSON.stringify(config.future)};
export const publicPath = ${JSON.stringify(config.publicPath)};
export const entry = { module: entryServer };
export const routes = {
${Object.keys(config.routes)
.map((key, index) => {
let route = config.routes[key];
return `${JSON.stringify(key)}: {
id: ${JSON.stringify(route.id)},
parentId: ${JSON.stringify(route.parentId)},
path: ${JSON.stringify(route.path)},
index: ${JSON.stringify(route.index)},
caseSensitive: ${JSON.stringify(route.caseSensitive)},
module: route${index}
}`;
})
.join(",\n ")}
};`,
};
});
},
};
}