@netlify/vite-plugin-react-router 0.0.0

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.
package/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ # Changelog
package/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ MIT License
2
+
3
+ Copyright (c) Netlify Inc. 2024
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ persons to whom the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or
11
+ substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # React Router Adapter for Netlify
2
+
3
+ The React Router Adapter for Netlify allows you to deploy your [React Router](https://reactrouter.com) app to
4
+ [Netlify Functions](https://docs.netlify.com/functions/overview/).
5
+
6
+ To deploy a React Router 7+ site to Netlify, install this package:
7
+
8
+ ## How to use
9
+
10
+ ```sh
11
+ npm install @netlify/vite-plugin-react-router
12
+ ```
13
+
14
+ and include the Netlify plugin in your `vite.config.ts`:
15
+
16
+ ```typescript
17
+ import { reactRouter } from '@react-router/dev/vite'
18
+ import { defineConfig } from 'vite'
19
+ import tsconfigPaths from 'vite-tsconfig-paths'
20
+ import netlifyPlugin from '@netlify/vite-plugin-react-router' // <- add this
21
+
22
+ export default defineConfig({
23
+ plugins: [
24
+ reactRouter(),
25
+ tsconfigPaths(),
26
+ netlifyPlugin(), // <- add this
27
+ ],
28
+ })
29
+ ```
@@ -0,0 +1,29 @@
1
+ import { ServerBuild, AppLoadContext } from 'react-router';
2
+ import { Context } from '@netlify/functions';
3
+ import { Plugin } from 'vite';
4
+
5
+ type LoadContext = AppLoadContext & Context;
6
+ /**
7
+ * A function that returns the value to use as `context` in route `loader` and
8
+ * `action` functions.
9
+ *
10
+ * You can think of this as an escape hatch that allows you to pass
11
+ * environment/platform-specific values through to your loader/action.
12
+ */
13
+ type GetLoadContextFunction = (request: Request, context: Context) => Promise<LoadContext> | LoadContext;
14
+ type RequestHandler = (request: Request, context: LoadContext) => Promise<Response | void>;
15
+ /**
16
+ * Given a build and a callback to get the base loader context, this returns
17
+ * a Netlify Function handler (https://docs.netlify.com/functions/overview/) which renders the
18
+ * requested path. The loader context in this lifecycle will contain the Netlify Functions context
19
+ * fields merged in.
20
+ */
21
+ declare function createRequestHandler({ build, mode, getLoadContext, }: {
22
+ build: ServerBuild;
23
+ mode?: string;
24
+ getLoadContext?: GetLoadContextFunction;
25
+ }): RequestHandler;
26
+
27
+ declare function netlifyPlugin(): Plugin;
28
+
29
+ export { type GetLoadContextFunction, type RequestHandler, createRequestHandler, netlifyPlugin as default };
@@ -0,0 +1,29 @@
1
+ import { ServerBuild, AppLoadContext } from 'react-router';
2
+ import { Context } from '@netlify/functions';
3
+ import { Plugin } from 'vite';
4
+
5
+ type LoadContext = AppLoadContext & Context;
6
+ /**
7
+ * A function that returns the value to use as `context` in route `loader` and
8
+ * `action` functions.
9
+ *
10
+ * You can think of this as an escape hatch that allows you to pass
11
+ * environment/platform-specific values through to your loader/action.
12
+ */
13
+ type GetLoadContextFunction = (request: Request, context: Context) => Promise<LoadContext> | LoadContext;
14
+ type RequestHandler = (request: Request, context: LoadContext) => Promise<Response | void>;
15
+ /**
16
+ * Given a build and a callback to get the base loader context, this returns
17
+ * a Netlify Function handler (https://docs.netlify.com/functions/overview/) which renders the
18
+ * requested path. The loader context in this lifecycle will contain the Netlify Functions context
19
+ * fields merged in.
20
+ */
21
+ declare function createRequestHandler({ build, mode, getLoadContext, }: {
22
+ build: ServerBuild;
23
+ mode?: string;
24
+ getLoadContext?: GetLoadContextFunction;
25
+ }): RequestHandler;
26
+
27
+ declare function netlifyPlugin(): Plugin;
28
+
29
+ export { type GetLoadContextFunction, type RequestHandler, createRequestHandler, netlifyPlugin as default };
package/dist/index.js ADDED
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ createRequestHandler: () => createRequestHandler,
24
+ default: () => netlifyPlugin
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/server.ts
29
+ var import_react_router = require("react-router");
30
+ function createRequestHandler({
31
+ build,
32
+ mode,
33
+ getLoadContext
34
+ }) {
35
+ const reactRouterHandler = (0, import_react_router.createRequestHandler)(build, mode);
36
+ return async (request, netlifyContext) => {
37
+ const start = Date.now();
38
+ console.log(`[${request.method}] ${request.url}`);
39
+ try {
40
+ const mergedLoadContext = await getLoadContext?.(request, netlifyContext) || netlifyContext;
41
+ const response = await reactRouterHandler(request, mergedLoadContext);
42
+ response.headers.set("x-nf-runtime", "Node");
43
+ console.log(`[${response.status}] ${request.url} (${Date.now() - start}ms)`);
44
+ return response;
45
+ } catch (error) {
46
+ console.error(error);
47
+ return new Response("Internal Error", { status: 500 });
48
+ }
49
+ };
50
+ }
51
+
52
+ // src/plugin.ts
53
+ var import_promises = require("fs/promises");
54
+ var import_node_path = require("path");
55
+ var import_posix = require("path/posix");
56
+
57
+ // package.json
58
+ var name = "@netlify/vite-plugin-react-router";
59
+ var version = "0.0.0";
60
+
61
+ // src/plugin.ts
62
+ var NETLIFY_FUNCTIONS_DIR = ".netlify/functions-internal";
63
+ var FUNCTION_FILENAME = "react-router-server.mjs";
64
+ var FUNCTION_HANDLER_CHUNK = "server";
65
+ var FUNCTION_HANDLER_MODULE_ID = "virtual:netlify-server";
66
+ var RESOLVED_FUNCTION_HANDLER_MODULE_ID = `\0${FUNCTION_HANDLER_MODULE_ID}`;
67
+ var toPosixPath = (path) => path.split(import_node_path.sep).join(import_posix.sep);
68
+ var FUNCTION_HANDLER = (
69
+ /* js */
70
+ `
71
+ import { createRequestHandler } from "@netlify/vite-plugin-react-router";
72
+ import * as build from "virtual:react-router/server-build";
73
+ export default createRequestHandler({
74
+ build,
75
+ getLoadContext: async (_req, ctx) => ctx,
76
+ });
77
+ `
78
+ );
79
+ function generateNetlifyFunction(handlerPath) {
80
+ return (
81
+ /* js */
82
+ `
83
+ export { default } from "${handlerPath}";
84
+
85
+ export const config = {
86
+ name: "React Router server handler",
87
+ generator: "${name}@${version}",
88
+ path: "/*",
89
+ preferStatic: true,
90
+ };
91
+ `
92
+ );
93
+ }
94
+ function netlifyPlugin() {
95
+ let resolvedConfig;
96
+ let currentCommand;
97
+ let isSsr;
98
+ return {
99
+ name: "vite-plugin-react-router-netlify-functions",
100
+ config(config, { command, isSsrBuild }) {
101
+ currentCommand = command;
102
+ isSsr = isSsrBuild;
103
+ if (command === "build") {
104
+ if (isSsrBuild) {
105
+ if (typeof config.build?.rollupOptions?.input === "string") {
106
+ config.build.rollupOptions.input = {
107
+ [FUNCTION_HANDLER_CHUNK]: FUNCTION_HANDLER_MODULE_ID,
108
+ index: config.build.rollupOptions.input
109
+ };
110
+ if (config.build.rollupOptions.output && !Array.isArray(config.build.rollupOptions.output)) {
111
+ config.build.rollupOptions.output.entryFileNames = "[name].js";
112
+ }
113
+ }
114
+ }
115
+ }
116
+ },
117
+ async resolveId(source) {
118
+ if (source === FUNCTION_HANDLER_MODULE_ID) {
119
+ return RESOLVED_FUNCTION_HANDLER_MODULE_ID;
120
+ }
121
+ },
122
+ // See https://vitejs.dev/guide/api-plugin#virtual-modules-convention.
123
+ load(id) {
124
+ if (id === RESOLVED_FUNCTION_HANDLER_MODULE_ID) {
125
+ return FUNCTION_HANDLER;
126
+ }
127
+ },
128
+ async configResolved(config) {
129
+ resolvedConfig = config;
130
+ },
131
+ // See https://rollupjs.org/plugin-development/#writebundle.
132
+ async writeBundle() {
133
+ if (currentCommand === "build" && isSsr) {
134
+ const functionsDirectory = (0, import_node_path.join)(resolvedConfig.root, NETLIFY_FUNCTIONS_DIR);
135
+ await (0, import_promises.mkdir)(functionsDirectory, { recursive: true });
136
+ const handlerPath = (0, import_node_path.join)(resolvedConfig.build.outDir, `${FUNCTION_HANDLER_CHUNK}.js`);
137
+ const relativeHandlerPath = toPosixPath((0, import_node_path.relative)(functionsDirectory, handlerPath));
138
+ await (0, import_promises.writeFile)((0, import_node_path.join)(functionsDirectory, FUNCTION_FILENAME), generateNetlifyFunction(relativeHandlerPath));
139
+ }
140
+ }
141
+ };
142
+ }
143
+ // Annotate the CommonJS export names for ESM import in node:
144
+ 0 && (module.exports = {
145
+ createRequestHandler
146
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,119 @@
1
+ // src/server.ts
2
+ import { createRequestHandler as createReactRouterRequestHandler } from "react-router";
3
+ function createRequestHandler({
4
+ build,
5
+ mode,
6
+ getLoadContext
7
+ }) {
8
+ const reactRouterHandler = createReactRouterRequestHandler(build, mode);
9
+ return async (request, netlifyContext) => {
10
+ const start = Date.now();
11
+ console.log(`[${request.method}] ${request.url}`);
12
+ try {
13
+ const mergedLoadContext = await getLoadContext?.(request, netlifyContext) || netlifyContext;
14
+ const response = await reactRouterHandler(request, mergedLoadContext);
15
+ response.headers.set("x-nf-runtime", "Node");
16
+ console.log(`[${response.status}] ${request.url} (${Date.now() - start}ms)`);
17
+ return response;
18
+ } catch (error) {
19
+ console.error(error);
20
+ return new Response("Internal Error", { status: 500 });
21
+ }
22
+ };
23
+ }
24
+
25
+ // src/plugin.ts
26
+ import { mkdir, writeFile } from "node:fs/promises";
27
+ import { join, relative, sep } from "node:path";
28
+ import { sep as posixSep } from "node:path/posix";
29
+
30
+ // package.json
31
+ var name = "@netlify/vite-plugin-react-router";
32
+ var version = "0.0.0";
33
+
34
+ // src/plugin.ts
35
+ var NETLIFY_FUNCTIONS_DIR = ".netlify/functions-internal";
36
+ var FUNCTION_FILENAME = "react-router-server.mjs";
37
+ var FUNCTION_HANDLER_CHUNK = "server";
38
+ var FUNCTION_HANDLER_MODULE_ID = "virtual:netlify-server";
39
+ var RESOLVED_FUNCTION_HANDLER_MODULE_ID = `\0${FUNCTION_HANDLER_MODULE_ID}`;
40
+ var toPosixPath = (path) => path.split(sep).join(posixSep);
41
+ var FUNCTION_HANDLER = (
42
+ /* js */
43
+ `
44
+ import { createRequestHandler } from "@netlify/vite-plugin-react-router";
45
+ import * as build from "virtual:react-router/server-build";
46
+ export default createRequestHandler({
47
+ build,
48
+ getLoadContext: async (_req, ctx) => ctx,
49
+ });
50
+ `
51
+ );
52
+ function generateNetlifyFunction(handlerPath) {
53
+ return (
54
+ /* js */
55
+ `
56
+ export { default } from "${handlerPath}";
57
+
58
+ export const config = {
59
+ name: "React Router server handler",
60
+ generator: "${name}@${version}",
61
+ path: "/*",
62
+ preferStatic: true,
63
+ };
64
+ `
65
+ );
66
+ }
67
+ function netlifyPlugin() {
68
+ let resolvedConfig;
69
+ let currentCommand;
70
+ let isSsr;
71
+ return {
72
+ name: "vite-plugin-react-router-netlify-functions",
73
+ config(config, { command, isSsrBuild }) {
74
+ currentCommand = command;
75
+ isSsr = isSsrBuild;
76
+ if (command === "build") {
77
+ if (isSsrBuild) {
78
+ if (typeof config.build?.rollupOptions?.input === "string") {
79
+ config.build.rollupOptions.input = {
80
+ [FUNCTION_HANDLER_CHUNK]: FUNCTION_HANDLER_MODULE_ID,
81
+ index: config.build.rollupOptions.input
82
+ };
83
+ if (config.build.rollupOptions.output && !Array.isArray(config.build.rollupOptions.output)) {
84
+ config.build.rollupOptions.output.entryFileNames = "[name].js";
85
+ }
86
+ }
87
+ }
88
+ }
89
+ },
90
+ async resolveId(source) {
91
+ if (source === FUNCTION_HANDLER_MODULE_ID) {
92
+ return RESOLVED_FUNCTION_HANDLER_MODULE_ID;
93
+ }
94
+ },
95
+ // See https://vitejs.dev/guide/api-plugin#virtual-modules-convention.
96
+ load(id) {
97
+ if (id === RESOLVED_FUNCTION_HANDLER_MODULE_ID) {
98
+ return FUNCTION_HANDLER;
99
+ }
100
+ },
101
+ async configResolved(config) {
102
+ resolvedConfig = config;
103
+ },
104
+ // See https://rollupjs.org/plugin-development/#writebundle.
105
+ async writeBundle() {
106
+ if (currentCommand === "build" && isSsr) {
107
+ const functionsDirectory = join(resolvedConfig.root, NETLIFY_FUNCTIONS_DIR);
108
+ await mkdir(functionsDirectory, { recursive: true });
109
+ const handlerPath = join(resolvedConfig.build.outDir, `${FUNCTION_HANDLER_CHUNK}.js`);
110
+ const relativeHandlerPath = toPosixPath(relative(functionsDirectory, handlerPath));
111
+ await writeFile(join(functionsDirectory, FUNCTION_FILENAME), generateNetlifyFunction(relativeHandlerPath));
112
+ }
113
+ }
114
+ };
115
+ }
116
+ export {
117
+ createRequestHandler,
118
+ netlifyPlugin as default
119
+ };
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@netlify/vite-plugin-react-router",
3
+ "version": "0.0.0",
4
+ "description": "React Router 7+ Vite plugin for Netlify",
5
+ "type": "commonjs",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "require": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "import": {
16
+ "types": "./dist/index.d.mts",
17
+ "default": "./dist/index.mjs"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist/**/*",
23
+ "LICENSE",
24
+ "README.md",
25
+ "CHANGELOG.md"
26
+ ],
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/netlify/remix-compute",
30
+ "directory": "packages/vite-plugin-react-router"
31
+ },
32
+ "keywords": [
33
+ "react-router",
34
+ "vite-plugin",
35
+ "netlify"
36
+ ],
37
+ "license": "MIT",
38
+ "bugs": {
39
+ "url": "https://github.com/netlify/remix-compute/issues"
40
+ },
41
+ "homepage": "https://github.com/netlify/remix-compute#readme",
42
+ "dependencies": {
43
+ "@react-router/node": "^7.0.1",
44
+ "isbot": "^5.0.0",
45
+ "react-router": "^7.0.1"
46
+ },
47
+ "devDependencies": {
48
+ "@netlify/functions": "^2.8.1",
49
+ "@types/react": "^18.0.27",
50
+ "@types/react-dom": "^18.0.10",
51
+ "react": "^18.2.0",
52
+ "react-dom": "^18.2.0",
53
+ "tsup": "^8.0.2",
54
+ "vite": "^5.4.11"
55
+ },
56
+ "peerDependencies": {
57
+ "vite": ">=5.0.0"
58
+ },
59
+ "engines": {
60
+ "node": ">=18"
61
+ },
62
+ "publishConfig": {
63
+ "access": "public"
64
+ },
65
+ "scripts": {
66
+ "build": "tsup-node src/index.ts --format esm,cjs --dts --target node18 --clean",
67
+ "build:watch": "pnpm run build --watch"
68
+ }
69
+ }