-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathplugins.ts
187 lines (174 loc) · 5.99 KB
/
plugins.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import path from 'node:path';
import type {Plugin, ResolvedConfig} from 'vite';
import {
setupHydrogenMiddleware,
setupRemixDevServerHooks,
} from './hydrogen-middleware.js';
import {
setupOxygenMiddleware,
startMiniOxygenRuntime,
type MiniOxygen,
} from './mini-oxygen.js';
import {
getH2OPluginContext,
setH2OPluginContext,
DEFAULT_SSR_ENTRY,
type OxygenPluginOptions,
type HydrogenPluginOptions,
} from './shared.js';
import {H2O_BINDING_NAME, createLogRequestEvent} from '../request-events.js';
/**
* Enables Hydrogen utilities for local development
* such as GraphiQL, Subrequest Profiler, etc.
* It must be used in combination with the `oxygen` plugin and Hydrogen CLI.
* @experimental
*/
export function hydrogen(pluginOptions: HydrogenPluginOptions = {}): Plugin[] {
const isRemixChildCompiler = (config: ResolvedConfig) =>
!config.plugins?.some((plugin) => plugin.name === 'remix');
return [
{
name: 'hydrogen:main',
config(config) {
return {
ssr: {
optimizeDeps: {
// Add CJS dependencies that break code in workerd
// with errors like "require/module/exports is not defined":
include: [
// React deps:
'react',
'react/jsx-runtime',
'react/jsx-dev-runtime',
'react-dom',
'react-dom/server',
// Remix deps:
'set-cookie-parser',
'cookie',
// Hydrogen deps:
'content-security-policy-builder',
],
},
},
// Pass the setup functions to the Oxygen runtime.
...setH2OPluginContext({
setupScripts: [setupRemixDevServerHooks],
shouldStartRuntime: (config) => !isRemixChildCompiler(config),
services: {
[H2O_BINDING_NAME]: createLogRequestEvent({
transformLocation: (partialLocation) =>
path.join(config.root ?? process.cwd(), partialLocation),
}),
},
}),
};
},
configureServer(viteDevServer) {
if (isRemixChildCompiler(viteDevServer.config)) return;
// Get options from Hydrogen CLI.
const {cliOptions} = getH2OPluginContext(viteDevServer.config) || {};
return () => {
setupHydrogenMiddleware(viteDevServer, {
...pluginOptions,
...cliOptions,
});
};
},
},
];
}
/**
* Runs backend code in an Oxygen worker instead of Node.js during development.
* It must be placed after `hydrogen` but before `remix` in the Vite plugins list.
* @experimental
*/
export function oxygen(pluginOptions: OxygenPluginOptions = {}): Plugin[] {
let resolvedConfig: ResolvedConfig;
let absoluteWorkerEntryFile: string;
return [
{
name: 'oxygen:runtime',
config(config, env) {
return {
appType: 'custom',
resolve: {
conditions: ['worker', 'workerd'],
},
ssr: {
noExternal: true,
target: 'webworker',
},
// When building, the CLI will set the `ssr` option to `true`
// if no --entry flag is passed for the default SSR entry file.
// Replace it here with a default value.
...(env.isSsrBuild &&
config.build?.ssr && {
build: {
ssr:
config.build?.ssr === true
? // No --entry flag passed by the user, use the
// option passed to the plugin or the default value
pluginOptions.ssrEntry ?? DEFAULT_SSR_ENTRY
: // --entry flag passed by the user, keep it
config.build?.ssr,
},
}),
};
},
configureServer(viteDevServer) {
resolvedConfig = viteDevServer.config;
// Get options from Hydrogen plugin and CLI.
const {shouldStartRuntime, cliOptions, setupScripts, services} =
getH2OPluginContext(resolvedConfig) || {};
if (shouldStartRuntime && !shouldStartRuntime(resolvedConfig)) return;
const workerEntryFile =
cliOptions?.ssrEntry ?? pluginOptions.ssrEntry ?? DEFAULT_SSR_ENTRY;
absoluteWorkerEntryFile = path.isAbsolute(workerEntryFile)
? workerEntryFile
: path.resolve(viteDevServer.config.root, workerEntryFile);
const envPromise = cliOptions?.envPromise ?? Promise.resolve();
let miniOxygen: MiniOxygen;
const miniOxygenPromise = envPromise.then((remoteEnv) => {
return startMiniOxygenRuntime({
viteDevServer,
workerEntryFile,
setupScripts,
services,
env: {...remoteEnv, ...pluginOptions.env},
debug: cliOptions?.debug ?? pluginOptions.debug ?? false,
inspectorPort:
cliOptions?.inspectorPort ?? pluginOptions.inspectorPort ?? 9229,
});
});
process.once('SIGTERM', async () => {
try {
await miniOxygen?.dispose();
} finally {
process.exit();
}
});
return () => {
setupOxygenMiddleware(viteDevServer, async (request) => {
miniOxygen ??= await miniOxygenPromise;
return miniOxygen.dispatch(request);
});
};
},
transform(code, id, options) {
if (
resolvedConfig?.command === 'serve' &&
resolvedConfig?.server?.hmr !== false &&
options?.ssr &&
(id === absoluteWorkerEntryFile ||
id === absoluteWorkerEntryFile + path.extname(id))
) {
return {
// Accept HMR in server entry module to avoid full-page refresh in the browser.
// Note: appending code at the end should not break the source map.
code: code + '\nif (import.meta.hot) import.meta.hot.accept();',
};
}
},
},
];
}