houdini-react 2.0.0-go.2 → 2.0.0-go.20

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.
@@ -0,0 +1,52 @@
1
+ // a suspense cache is an object that maintains a key-value store of
2
+ // objects. If a value is missing when get() is called, a promise
3
+ // is thrown that resolves when a value is passed to set()
4
+ import { LRUCache } from 'houdini/runtime'
5
+
6
+ export function suspense_cache<T>(initialData?: Record<string, T>): SuspenseCache<T> {
7
+ const cache = new SuspenseCache<T>()
8
+
9
+ for (const [key, value] of Object.entries(initialData ?? {})) {
10
+ cache.set(key, value)
11
+ }
12
+
13
+ return cache
14
+ }
15
+
16
+ export class SuspenseCache<_Data> extends LRUCache<_Data> {
17
+ // if get is called before set, we need to invoke a callback.
18
+ // that means we need a place to put our callbacks
19
+ #callbacks: Map<string, { resolve: () => void; reject: () => void }[]> = new Map()
20
+
21
+ get(key: string): _Data {
22
+ // if there is a value, use that
23
+ if (super.has(key)) {
24
+ return super.get(key)!
25
+ }
26
+
27
+ // we don't have a value, so we need to throw a promise
28
+ // that resolves when a value is passed to set()
29
+ throw new Promise<void>((resolve, reject) => {
30
+ this.#subscribe(key, resolve, reject)
31
+ })
32
+ }
33
+
34
+ // TODO: reject?
35
+
36
+ set(key: string, value: _Data) {
37
+ // perform the set like normal
38
+ super.set(key, value)
39
+
40
+ // if there are subscribers, resolve them
41
+ if (this.#callbacks.has(key)) {
42
+ // resolve all of the callbacks
43
+ this.#callbacks.get(key)?.forEach(({ resolve }) => resolve())
44
+ // delete the key
45
+ this.#callbacks.delete(key)
46
+ }
47
+ }
48
+
49
+ #subscribe(key: string, resolve: () => void, reject: () => void) {
50
+ this.#callbacks.set(key, [...(this.#callbacks.get(key) || []), { resolve, reject }])
51
+ }
52
+ }
@@ -0,0 +1,2 @@
1
+ export * from './Router'
2
+ export { type SuspenseCache, suspense_cache } from './cache'
@@ -0,0 +1 @@
1
+ export { renderToStream } from 'react-streaming/server';
@@ -0,0 +1,4 @@
1
+ import { renderToStream } from "react-streaming/server";
2
+ export {
3
+ renderToStream
4
+ };
@@ -0,0 +1 @@
1
+ {"type":"module"}
File without changes
@@ -0,0 +1,3 @@
1
+ import { VitePluginContext } from 'houdini/vite';
2
+ import { PluginOption } from 'vite';
3
+ export default function (ctx: VitePluginContext): PluginOption;
package/vite/index.js ADDED
@@ -0,0 +1,284 @@
1
+ import { fs, path } from "houdini";
2
+ import {
3
+ app_component_path,
4
+ adapter_config_path,
5
+ plugin_dir,
6
+ client_build_directory
7
+ } from "houdini/router/conventions";
8
+ import { load_manifest } from "houdini/router/manifest";
9
+ import { createRequire } from "node:module";
10
+ import { build } from "vite";
11
+ import { transform_file } from "./transform.js";
12
+ const _require = createRequire(import.meta.url);
13
+ let reactStreamingServerPath = "";
14
+ try {
15
+ const main = _require.resolve("react-streaming");
16
+ const pkgDir = main.replace(/\/dist\/.*$/, "");
17
+ reactStreamingServerPath = path.join(pkgDir, "dist/server/index.node-and-web.js");
18
+ } catch {
19
+ }
20
+ function vite_default(ctx) {
21
+ let manifest;
22
+ let viteEnv;
23
+ let devServer = false;
24
+ let isSSRBuild = false;
25
+ let cfCache = null;
26
+ return {
27
+ name: "houdini-react",
28
+ configResolved(config) {
29
+ isSSRBuild = !!config.build.ssr;
30
+ },
31
+ async config(userConfig, env) {
32
+ viteEnv = env;
33
+ if (userConfig.build?.ssr) {
34
+ return reactStreamingServerPath ? { resolve: { alias: { "react-streaming/server": reactStreamingServerPath } } } : {};
35
+ }
36
+ try {
37
+ manifest = await load_manifest({ config: ctx.config });
38
+ } catch (e) {
39
+ console.log(
40
+ "something went wrong. please try again. \n error: " + e.message
41
+ );
42
+ manifest = {
43
+ pages: {},
44
+ layouts: {},
45
+ page_queries: {},
46
+ layout_queries: {},
47
+ artifacts: [],
48
+ local_schema: false,
49
+ local_yoga: false,
50
+ component_fields: {}
51
+ };
52
+ }
53
+ let conf = {
54
+ build: { rollupOptions: {} }
55
+ };
56
+ if (env.command === "build") {
57
+ conf.base = "/assets";
58
+ }
59
+ const compiledAssetsDir = client_build_directory(ctx.config);
60
+ await fs.mkdirp(compiledAssetsDir);
61
+ conf.build = {
62
+ outDir: compiledAssetsDir,
63
+ rollupOptions: {
64
+ output: {
65
+ assetFileNames: "assets/[name].js",
66
+ entryFileNames: "[name].js"
67
+ },
68
+ input: {
69
+ "entries/app": app_component_path(ctx.config),
70
+ ...ctx.adapter ? { "entries/adapter": adapter_config_path(ctx.config) } : {}
71
+ }
72
+ }
73
+ };
74
+ if (env.command === "build" && ctx.adapter && ctx.adapter.includePaths) {
75
+ const extra = typeof ctx.adapter.includePaths === "function" ? ctx.adapter.includePaths({ config: ctx.config }) : ctx.adapter.includePaths;
76
+ Object.assign(conf.build.rollupOptions.input, extra);
77
+ }
78
+ for (const [id, page] of Object.entries(manifest.pages)) {
79
+ ;
80
+ conf.build.rollupOptions.input[`pages/${id}`] = `virtual:houdini/pages/${page.id}.jsx`;
81
+ }
82
+ return reactStreamingServerPath ? {
83
+ ...conf,
84
+ resolve: { alias: { "react-streaming/server": reactStreamingServerPath } }
85
+ } : conf;
86
+ },
87
+ resolveId(id) {
88
+ if (!id.includes("virtual:houdini")) {
89
+ return;
90
+ }
91
+ return id.substring(id.indexOf("virtual:houdini"));
92
+ },
93
+ async transform(code, filepath) {
94
+ filepath = path.posixify(filepath);
95
+ if (filepath.startsWith("/src/")) {
96
+ filepath = path.join(process.cwd(), filepath);
97
+ }
98
+ if (!ctx.config.includeFile(filepath)) {
99
+ return;
100
+ }
101
+ if (cfCache === null) {
102
+ try {
103
+ cfCache = ctx.db.prepare("SELECT type, field, fragment FROM component_fields").all();
104
+ } catch {
105
+ cfCache = [];
106
+ }
107
+ }
108
+ return transform_file(
109
+ {
110
+ config: ctx.config,
111
+ content: code,
112
+ filepath: path.posixify(filepath),
113
+ watch_file: this.addWatchFile.bind(this)
114
+ },
115
+ cfCache
116
+ );
117
+ },
118
+ async closeBundle() {
119
+ if (viteEnv.mode !== "production" || devServer || isSSRBuild) {
120
+ return;
121
+ }
122
+ if (!ctx.adapter || ctx.adapter?.disableServer) {
123
+ return;
124
+ }
125
+ const compiledAssetsDir = client_build_directory(ctx.config);
126
+ await build({
127
+ build: {
128
+ ssr: true,
129
+ outDir: path.join(compiledAssetsDir, "ssr"),
130
+ rollupOptions: {
131
+ output: {
132
+ assetFileNames: "assets/[name].js",
133
+ entryFileNames: "[name].js"
134
+ },
135
+ input: {
136
+ "entries/adapter": adapter_config_path(ctx.config)
137
+ }
138
+ }
139
+ }
140
+ });
141
+ },
142
+ async load(id) {
143
+ if (!id.startsWith("virtual:houdini")) {
144
+ return;
145
+ }
146
+ if (!manifest) {
147
+ return;
148
+ }
149
+ let [, which, arg] = id.split("/");
150
+ const parsedPath = arg ? path.parse(arg) : "";
151
+ const pageName = parsedPath ? parsedPath.name : "";
152
+ if (which === "pages") {
153
+ const page = manifest.pages[pageName];
154
+ if (!page) {
155
+ throw new Error("unknown page" + pageName);
156
+ }
157
+ const pendingQueries = page.queries.filter((query) => {
158
+ const pg = Object.values(manifest.page_queries).find((q) => q.name === query);
159
+ if (pg) {
160
+ return pg.loading;
161
+ }
162
+ const layout = Object.values(manifest.layout_queries).find(
163
+ (q) => q.name === query
164
+ );
165
+ return layout?.loading;
166
+ });
167
+ return `
168
+ import App from '$houdini/plugins/houdini-react/units/render/App'
169
+ import Component from '$houdini/plugins/houdini-react/units/entries/${pageName}.jsx'
170
+ import { hydrate_page } from '$houdini/plugins/houdini-react/runtime/hydration'
171
+ hydrate_page(App, Component, '${pageName}', ${JSON.stringify(pendingQueries)})
172
+ `;
173
+ }
174
+ if (which === "artifacts") {
175
+ return `
176
+ import artifact from '$houdini/artifacts/${pageName}'
177
+ import { register_artifact } from '$houdini/plugins/houdini-react/runtime/hydration'
178
+ register_artifact('${pageName}', artifact)
179
+ `;
180
+ }
181
+ if (which === "static-entry") {
182
+ return `
183
+ import App from '$houdini/plugins/houdini-react/units/render/App'
184
+ import manifest from '$houdini/plugins/houdini-react/runtime/manifest'
185
+ import { mount_static_app } from '$houdini/plugins/houdini-react/runtime/hydration'
186
+ mount_static_app(App, manifest)
187
+ `;
188
+ }
189
+ },
190
+ async configureServer(server) {
191
+ devServer = true;
192
+ server.middlewares.use(async (req, res, next) => {
193
+ if (!req.url) {
194
+ next();
195
+ return;
196
+ }
197
+ const { default: router_manifest } = await server.ssrLoadModule(
198
+ path.join(plugin_dir(ctx.config, "houdini-react"), "runtime", "manifest.ts")
199
+ );
200
+ const { createServerAdapter } = await server.ssrLoadModule(
201
+ adapter_config_path(ctx.config)
202
+ );
203
+ const requestHeaders = new Headers();
204
+ for (const header of Object.entries(req.headers ?? {})) {
205
+ requestHeaders.set(header[0], header[1]);
206
+ }
207
+ const port = server.config.server.port ?? 5173;
208
+ const request = new Request(
209
+ `http://localhost:${port}` + req.url,
210
+ req.method === "POST" ? {
211
+ method: req.method,
212
+ headers: requestHeaders,
213
+ body: await getBody(req)
214
+ } : void 0
215
+ );
216
+ let documentPremable = `<script type="module" src="/@vite/client" async=""><\/script>`;
217
+ try {
218
+ const transformed = await server.transformIndexHtml(
219
+ req.url,
220
+ "<!DOCTYPE html><html><head></head><body></body></html>"
221
+ );
222
+ const headMatch = transformed.match(/<head>([\s\S]*?)<\/head>/);
223
+ if (headMatch?.[1]?.trim()) {
224
+ documentPremable = headMatch[1].trim();
225
+ }
226
+ } catch {
227
+ }
228
+ try {
229
+ const result = await createServerAdapter({
230
+ production: false,
231
+ manifest: router_manifest,
232
+ assetPrefix: "/virtual:houdini",
233
+ pipe: res,
234
+ documentPremable
235
+ })(request);
236
+ if (result && result.status === 404) {
237
+ return next();
238
+ }
239
+ if (result && typeof result !== "boolean") {
240
+ if (res.closed) {
241
+ return;
242
+ }
243
+ for (const header of result.headers ?? []) {
244
+ res.setHeader(header[0], header[1]);
245
+ }
246
+ if (result.status >= 300 && result.status < 400) {
247
+ res.writeHead(result.status, {
248
+ Location: result.headers.get("Location") ?? "",
249
+ ...[...result.headers].reduce(
250
+ (headers, [key, value]) => ({
251
+ ...headers,
252
+ [key]: value
253
+ }),
254
+ {}
255
+ )
256
+ });
257
+ } else {
258
+ res.write(await result.text());
259
+ }
260
+ res.end();
261
+ }
262
+ } catch (e) {
263
+ console.error(e);
264
+ res.end();
265
+ }
266
+ });
267
+ }
268
+ };
269
+ }
270
+ function getBody(request) {
271
+ return new Promise((resolve) => {
272
+ const bodyParts = [];
273
+ let body;
274
+ request.on("data", (chunk) => {
275
+ bodyParts.push(chunk);
276
+ }).on("end", () => {
277
+ body = Buffer.concat(bodyParts).toString();
278
+ resolve(body);
279
+ });
280
+ });
281
+ }
282
+ export {
283
+ vite_default as default
284
+ };
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1,11 @@
1
+ import type { TransformPage } from 'houdini';
2
+ import type { SourceMapInput } from 'rollup';
3
+ export type ComponentFieldRow = {
4
+ type: string;
5
+ field: string;
6
+ fragment: string;
7
+ };
8
+ export declare function transform_file(page: TransformPage, cfRows: ComponentFieldRow[]): Promise<{
9
+ code: string;
10
+ map?: SourceMapInput;
11
+ }>;
@@ -0,0 +1,92 @@
1
+ import * as graphql from "graphql";
2
+ import {
3
+ ArtifactKind,
4
+ artifact_import,
5
+ ensure_imports,
6
+ find_graphql,
7
+ parseJS,
8
+ path,
9
+ printJS
10
+ } from "houdini";
11
+ import { componentField_unit_path, houdini_root } from "houdini/router/conventions";
12
+ import * as recast from "recast";
13
+ const AST = recast.types.builders;
14
+ async function transform_file(page, cfRows) {
15
+ const isJSX = page.filepath.endsWith(".tsx") || page.filepath.endsWith(".jsx");
16
+ if (!isJSX && !page.filepath.endsWith(".ts") && !page.filepath.endsWith(".js")) {
17
+ return { code: page.content, map: page.map };
18
+ }
19
+ const script = parseJS(page.content, isJSX ? { plugins: ["jsx"] } : {});
20
+ const cfMap = {};
21
+ for (const row of cfRows) {
22
+ if (row.type && row.field && row.fragment) {
23
+ cfMap[row.type] ??= {};
24
+ cfMap[row.type][row.field] = row.fragment;
25
+ }
26
+ }
27
+ await find_graphql(page.config, script, {
28
+ skipGraphqlType: true,
29
+ tag({ node, artifact, parsedDocument }) {
30
+ const { id: artifactRef } = artifact_import({ page, script, artifact });
31
+ const properties = [AST.objectProperty(AST.stringLiteral("artifact"), artifactRef)];
32
+ if (is_paginated(parsedDocument)) {
33
+ if (artifact.kind !== ArtifactKind.Query) {
34
+ const refetchName = artifact.name + "_Pagination_Query";
35
+ const { id: refetchRef } = artifact_import({
36
+ page,
37
+ script,
38
+ artifact: { name: refetchName }
39
+ });
40
+ properties.push(
41
+ AST.objectProperty(AST.stringLiteral("refetchArtifact"), refetchRef)
42
+ );
43
+ } else {
44
+ properties.push(
45
+ AST.objectProperty(AST.stringLiteral("refetchArtifact"), artifactRef)
46
+ );
47
+ }
48
+ }
49
+ if (Object.keys(cfMap).length > 0) {
50
+ const typeInfo = new graphql.TypeInfo(page.config.schema);
51
+ graphql.visit(
52
+ parsedDocument,
53
+ graphql.visitWithTypeInfo(typeInfo, {
54
+ Field(fieldNode) {
55
+ const parentType = typeInfo.getParentType();
56
+ const typeName = parentType?.name;
57
+ if (!typeName)
58
+ return;
59
+ const fragmentName = cfMap[typeName]?.[fieldNode.name.value];
60
+ if (!fragmentName)
61
+ return;
62
+ const entryPointPath = componentField_unit_path(
63
+ page.config,
64
+ fragmentName
65
+ );
66
+ ensure_imports({
67
+ script,
68
+ sourceModule: "$houdini/" + path.relative(houdini_root(page.config), entryPointPath)
69
+ });
70
+ }
71
+ })
72
+ );
73
+ }
74
+ node.replaceWith(AST.objectExpression(properties));
75
+ }
76
+ });
77
+ return printJS(script);
78
+ }
79
+ function is_paginated(doc) {
80
+ let paginated = false;
81
+ graphql.visit(doc, {
82
+ Directive(node) {
83
+ if (node.name.value === "paginate") {
84
+ paginated = true;
85
+ }
86
+ }
87
+ });
88
+ return paginated;
89
+ }
90
+ export {
91
+ transform_file
92
+ };
package/shim.cjs DELETED
@@ -1,64 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- function getBinaryPath() {
4
- // lookup table for all platforms and binary distribution packages
5
- const BINARY_DISTRIBUTION_PACKAGES = {
6
- 'linux-x64': 'houdini-react-linux-x64',
7
- 'linux-arm64': 'houdini-react-linux-arm64',
8
- 'win32-x64': 'houdini-react-windows-x64',
9
- 'win32-arm64': 'houdini-react-windows-arm64',
10
- 'darwin-x64': 'houdini-react-darwin-x64',
11
- 'darwin-arm64': 'houdini-react-darwin-arm64',
12
- }
13
-
14
- // windows binaries end with .exe so we need to special case them
15
- const binaryName = process.platform === 'win32' ? 'houdini-react.exe' : 'houdini-react'
16
-
17
- // determine package name for this platform
18
- const platformSpecificPackageName =
19
- BINARY_DISTRIBUTION_PACKAGES[`${process.platform}-${process.arch}`]
20
-
21
- try {
22
- // resolving will fail if the optionalDependency was not installed
23
- return require.resolve(`../${platformSpecificPackageName}/bin/${binaryName}`)
24
- } catch (e) {
25
- return require('path').join(__dirname, binaryName)
26
- }
27
- }
28
- // instead of execFileSync, use spawn to handle the process more gracefully
29
- const childProcess = require('child_process').spawn(getBinaryPath(), process.argv.slice(2), {
30
- stdio: 'inherit',
31
- })
32
-
33
- // array of signals we want to handle
34
- const signals = ['SIGTERM', 'SIGINT', 'SIGQUIT', 'SIGHUP']
35
-
36
- // handle each signal
37
- signals.forEach((signal) => {
38
- process.on(signal, () => {
39
- if (childProcess) {
40
- // on windows, we need to use taskkill for proper tree killing
41
- if (process.platform === 'win32') {
42
- require('child_process').spawn('taskkill', ['/pid', childProcess.pid, '/f', '/t'])
43
- } else {
44
- try {
45
- childProcess.kill(signal)
46
- } catch (err) {
47
- // if the process is already gone, that's fine
48
- if (err.code !== 'ESRCH') throw err
49
- }
50
- }
51
- }
52
- process.exit(0)
53
- })
54
- })
55
-
56
- // handle child process exit
57
- childProcess.on('exit', (code, signal) => {
58
- // if the child was terminated due to a signal, exit with the same signal
59
- if (signal) {
60
- process.exit(0)
61
- } else {
62
- process.exit(code)
63
- }
64
- })