Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
itsMapleLeaf committed Oct 15, 2023
1 parent db6cb9f commit 53841e9
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ npx degit itsMapleLeaf/remix-electron/template my-desktop-app

### Adding to an existing Remix project

Install remix-electron and peer dependencies:
Install remix-electron and electron:

```bash
npm i remix-electron electron @remix-run/node @remix-run/server-runtime react react-dom
npm i remix-electron electron
```

Add a file at `desktop/main.js` to run the electron app. The `initRemix` function returns a url to load in the browser window.
Add a file at `desktop/index.js` to run the electron app. The `initRemix` function returns a url to load in the browser window.

```ts
// desktop/main.js
// desktop/index.js
const { initRemix } = require("remix-electron")
const { app, BrowserWindow } = require("electron")
const { join } = require("node:path")

/** @type {BrowserWindow | undefined} */
let win

app.on("ready", async () => {
try {
const url = await initRemix({
serverBuild: join(__dirname, "build"),
serverBuild: join(__dirname, "../build/index.js"),
})

win = new BrowserWindow({ show: false })
Expand All @@ -45,36 +46,35 @@ app.on("ready", async () => {
})
```

Update `serverBuildPath` in your Remix config:

```js
// remix.config.js
/** @type {import("@remix-run/dev/config").AppConfig} */
module.exports = {
serverBuildPath: "desktop/build/index.js",
// ...
}
```

Build the app with `npm run build`, then run `npx electron desktop/main.js` to start the app! 🚀
Build the app with `npm run build`, then run `npx electron desktop/index.js` to start the app! 🚀

## Using Electron APIs

Importing `"electron"` directly in route files results in Electron trying to get bundled and called in the browser / renderer process.

To circumvent this, create a `electron.server.ts` file, which re-exports from electron. The `.server` suffix tells Remix to only load it in the main process. You should use `.server` for any code that runs in the main process and uses node/electron APIs.

```js
```ts
// app/electron.server.ts
// @ts-nocheck
import electron from "electron"
export = electron
export default electron
```

```ts
// app/routes/_index.tsx
import electron from "~/electron.server"

export function loader() {
return {
userDataPath: electron.app.getPath("userData"),
}
}
```

Likewise, for any code running in the renderer process, e.g. using the [clipboard](https://www.electronjs.org/docs/latest/api/clipboard) module, you can use the `.client` suffix. Renderer process modules require `nodeIntegration`.

```js
// desktop/main.ts
```ts
// desktop/index.js
function createWindow() {
// ...
win = new BrowserWindow({
Expand Down Expand Up @@ -108,7 +108,7 @@ Options:
**app/context.ts**

```ts
import type * as remix from "@remix-run/server-runtime"
import type * as remix from "@remix-run/node"

// your context type
export type LoadContext = {
Expand Down

0 comments on commit 53841e9

Please sign in to comment.