This repository was archived by the owner on Jan 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Add ESM support #33
Merged
Merged
Add ESM support #33
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ import * as webFetch from "@remix-run/web-fetch" | |
| // if we override everything else, we get errors caused by the mismatch of built-in types and remix types | ||
| global.File = webFetch.File | ||
|
|
||
| import { watch } from "node:fs/promises" | ||
| import { constants, access, watch } from "node:fs/promises" | ||
| import type { AppLoadContext, ServerBuild } from "@remix-run/node" | ||
| import { broadcastDevReady, createRequestHandler } from "@remix-run/node" | ||
| import { app, protocol } from "electron" | ||
|
|
@@ -25,6 +25,7 @@ interface InitRemixOptions { | |
| mode?: string | ||
| publicFolder?: string | ||
| getLoadContext?: GetLoadContextFunction | ||
| esm?: boolean | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -38,18 +39,29 @@ export async function initRemix({ | |
| mode, | ||
| publicFolder: publicFolderOption = "public", | ||
| getLoadContext, | ||
| esm = typeof require === "undefined", | ||
| }: InitRemixOptions): Promise<string> { | ||
| const appRoot = app.getAppPath() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the electron app root not the remix project's and therefore |
||
| const publicFolder = asAbsolutePath(publicFolderOption, appRoot) | ||
| const publicFolder = asAbsolutePath(publicFolderOption, process.cwd()) | ||
|
|
||
| if ( | ||
| !(await access(publicFolder, constants.R_OK).then( | ||
| () => true, | ||
| () => false, | ||
| )) | ||
| ) { | ||
| throw new Error( | ||
| `Public folder ${publicFolder} does not exist. Make sure that the initRemix \`publicFolder\` option is configured correctly.`, | ||
| ) | ||
| } | ||
|
|
||
| const buildPath = | ||
| typeof serverBuildOption === "string" | ||
| ? require.resolve(serverBuildOption) | ||
| : undefined | ||
| typeof serverBuildOption === "string" ? serverBuildOption : undefined | ||
|
|
||
| let serverBuild = | ||
| typeof serverBuildOption === "string" | ||
| ? /** @type {ServerBuild} */ require(serverBuildOption) | ||
| typeof buildPath === "string" | ||
| ? /** @type {ServerBuild} */ await import( | ||
| esm ? `${buildPath}?${Date.now()}` : buildPath | ||
| ) | ||
| : serverBuildOption | ||
|
|
||
| await app.whenReady() | ||
|
|
@@ -95,9 +107,13 @@ export async function initRemix({ | |
| ) { | ||
| void (async () => { | ||
| for await (const _event of watch(buildPath)) { | ||
| purgeRequireCache(buildPath) | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
| serverBuild = require(buildPath) | ||
| if (esm) { | ||
| serverBuild = await import(`${buildPath}?${Date.now()}`) | ||
| } else { | ||
| purgeRequireCache(buildPath) | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
| serverBuild = require(buildPath) | ||
| } | ||
| await broadcastDevReady(serverBuild) | ||
| } | ||
| })() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| /build | ||
| /public/build | ||
| .cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| import electron from "electron" | ||
| export default electron |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { MetaFunction } from "@remix-run/node" | ||
| import { | ||
| Links, | ||
| LiveReload, | ||
| Meta, | ||
| Outlet, | ||
| Scripts, | ||
| ScrollRestoration, | ||
| } from "@remix-run/react" | ||
|
|
||
| export const meta: MetaFunction = () => { | ||
| return [{ title: "New Remix App" }] | ||
| } | ||
|
|
||
| export default function App() { | ||
| return ( | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charSet="utf8" /> | ||
| <meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
| <Meta /> | ||
| <Links /> | ||
| </head> | ||
| <body> | ||
| <Outlet /> | ||
| <ScrollRestoration /> | ||
| <Scripts /> | ||
| {process.env.NODE_ENV === "development" && <LiveReload />} | ||
| </body> | ||
| </html> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { useLoaderData } from "@remix-run/react" | ||
| import { useState } from "react" | ||
| import electron from "~/electron.server" | ||
|
|
||
| export function loader() { | ||
| return { | ||
| userDataPath: electron.app.getPath("userData"), | ||
| } | ||
| } | ||
|
|
||
| export default function Index() { | ||
| const data = useLoaderData<typeof loader>() | ||
| const [count, setCount] = useState(0) | ||
| return ( | ||
| <div style={{ fontFamily: "system-ui, sans-serif", lineHeight: "1.4" }}> | ||
| <h1>Welcome to Remix</h1> | ||
| <p data-testid="user-data-path">{data.userDataPath}</p> | ||
| <button | ||
| type="button" | ||
| data-testid="counter" | ||
| onClick={() => setCount(count + 1)} | ||
| > | ||
| {count} | ||
| </button> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { | ||
| type ActionFunctionArgs, | ||
| NodeOnDiskFile, | ||
| json, | ||
| unstable_createFileUploadHandler, | ||
| unstable_parseMultipartFormData, | ||
| } from "@remix-run/node" | ||
| import { Form, useActionData } from "@remix-run/react" | ||
|
|
||
| export async function action({ request }: ActionFunctionArgs) { | ||
| const formData = await unstable_parseMultipartFormData( | ||
| request, | ||
| unstable_createFileUploadHandler(), | ||
| ) | ||
|
|
||
| const file = formData.get("file") | ||
| if (!(file instanceof NodeOnDiskFile)) { | ||
| throw new Error("No file uploaded") | ||
| } | ||
|
|
||
| const text = await file.text() | ||
| return json({ text }) | ||
| } | ||
|
|
||
| export default function MultipartUploadsTest() { | ||
| const data = useActionData<typeof action>() | ||
| return ( | ||
| <> | ||
| <Form method="post" encType="multipart/form-data"> | ||
| <input type="file" name="file" /> | ||
| <button type="submit">Submit</button> | ||
| </Form> | ||
| <p data-testid="result">{data?.text}</p> | ||
| </> | ||
| ) | ||
| } |
13 changes: 13 additions & 0 deletions
13
workspaces/test-app-esm/app/routes/referrer-redirect.action.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import type { ActionFunction } from "@remix-run/node" | ||
| import { redirect } from "@remix-run/node" | ||
|
|
||
| export const action: ActionFunction = async ({ request }) => { | ||
| const { redirects } = Object.fromEntries(await request.formData()) | ||
| const referrer = request.headers.get("referer") | ||
| if (!referrer) { | ||
| throw new Error("No referrer header") | ||
| } | ||
| const url = new URL(referrer) | ||
| url.searchParams.set("redirects", String(Number(redirects) + 1)) | ||
| return redirect(url.toString()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.