The Nx Plugin for Remix contains executors, generators, and utilities for managing Remix applications and libraries within an Nx workspace. It provides:
- Integration with libraries such as Storybook, Jest, Vitest, Playwright and Cypress.
- Generators to help scaffold code quickly, including:
- Libraries, both internal to your codebase and publishable to npm
- Routes
- Loaders
- Actions
- Meta
- Utilities for automatic workspace refactoring.
Setting up @nx/remix
Section titled “Setting up @nx/remix”Installation
Section titled “Installation”In any Nx workspace, you can install @nx/remix
by running the following command:
nx add @nx/remix
This will install the correct version of @nx/remix
.
How @nx/remix Infers Tasks
Section titled “How @nx/remix Infers Tasks”The @nx/remix
plugin will create a task for any project that has a Remix configuration file present. Any of the following files will be recognized as a Remix configuration file:
remix.config.js
remix.config.mjs
remix.config.cjs
View Inferred Tasks
Section titled “View Inferred Tasks”To view inferred tasks for a project, open the project details view in Nx Console or run nx show project my-project --web
in the command line.
@nx/remix Configuration
Section titled “@nx/remix Configuration”The @nx/remix/plugin
is configured in the plugins
array in nx.json
.
{ "plugins": [ { "plugin": "@nx/remix/plugin", "options": { "buildTargetName": "build", "devTargetName": "dev", "startTargetName": "start", "typecheckTargetName": "typecheck" } } ]}
The buildTargetName
, devTargetName
, startTargetName
and typecheckTargetName
options control the names of the inferred Remix tasks. The default names are build
, dev
, start
and typecheck
.
Using the Remix Plugin
Section titled “Using the Remix Plugin”Generate a Remix Application
Section titled “Generate a Remix Application”NX Generating @nx/remix:application
✔ What unit test runner should be used? · vitest✔ Which E2E test runner would you like to use? · playwright
UPDATE package.jsonUPDATE nx.jsonCREATE apps/myapp/project.jsonCREATE apps/myapp/README.mdCREATE apps/myapp/app/nx-welcome.tsxCREATE apps/myapp/app/root.tsxCREATE apps/myapp/app/routes/_index.tsxCREATE apps/myapp/public/favicon.icoCREATE apps/myapp/remix.config.jsCREATE apps/myapp/remix.env.d.tsCREATE apps/myapp/tests/routes/_index.spec.tsxCREATE apps/myapp/tsconfig.app.jsonCREATE apps/myapp/tsconfig.jsonCREATE apps/myapp/.gitignoreCREATE apps/myapp/package.jsonCREATE apps/myapp/tsconfig.spec.jsonCREATE apps/myapp/vitest.config.tsCREATE apps/myapp/test-setup.tsCREATE apps/myapp/.eslintrc.jsonCREATE apps/myapp/.eslintignoreCREATE apps/myapp-e2e/project.jsonCREATE apps/myapp-e2e/src/example.spec.tsCREATE apps/myapp-e2e/playwright.config.tsCREATE apps/myapp-e2e/tsconfig.jsonCREATE apps/myapp-e2e/.eslintrc.json
Build, Serve and Test your Application
Section titled “Build, Serve and Test your Application”- To build your application run:
> nx run myapp:build
Building Remix app in production mode...
Built in 857ms
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
NX Successfully ran target build for project myapp (3s)
- To serve your application for use during development run:
> nx run myapp:dev
> remix dev --manual
💿 remix dev
info building...info built (388ms)[remix-serve] http://localhost:3000 (http://192.168.0.12:3000)
- To test the application using vitest run:
> nx run myapp:test
> vitest
RUN v1.6.0 /Users/columferry/dev/nrwl/issues/gh_issues/nx26943/apps/myapp
✓ tests/routes/_index.spec.tsx (1) ✓ renders loader data
Test Files 1 passed (1) Tests 1 passed (1) Start at 13:22:54 Duration 533ms (transform 47ms, setup 68ms, collect 123ms, tests 36ms, environment 204ms, prepare 35ms)
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
NX Successfully ran target test for project myapp (950ms)
Generating an Nx Library
Section titled “Generating an Nx Library”When developing your application, it often makes sense to split your codebase into smaller more focused libraries.
To generate a library to use in your Remix application run:
NX Generating @nx/remix:library
✔ What test runner should be used? · vitestUPDATE nx.jsonUPDATE package.jsonCREATE babel.config.jsonCREATE libs/login/project.jsonCREATE libs/login/.eslintrc.jsonCREATE libs/login/README.mdCREATE libs/login/src/index.tsCREATE libs/login/tsconfig.lib.jsonCREATE libs/login/tsconfig.jsonCREATE libs/login/vite.config.tsCREATE libs/login/tsconfig.spec.jsonCREATE libs/login/src/lib/login.module.cssCREATE libs/login/src/lib/login.spec.tsxCREATE libs/login/src/lib/login.tsxUPDATE tsconfig.base.jsonCREATE libs/login/src/test-setup.tsCREATE libs/login/src/server.ts
You can then use the library by importing one of the exports into your application:
apps/myapp/app/routes/index.tsx
import { Login } from '@acme/login';
export default function Index() { return ( <div> <Login /> </div> );}
You can also run test on your library:
nx test login
Generating a Route
Section titled “Generating a Route”To generate a route for your application:
NX Generating @nx/remix:route
CREATE apps/myapp/app/routes/admin.tsxCREATE apps/myapp/app/styles/admin.css
Using a loader from your Library
Section titled “Using a loader from your Library”To use a Route Loader where the logic lives in your library, follow the steps below.
- Generate a loader for your route:
NX Generating @nx/remix:loader
UPDATE apps/myapp/app/routes/admin.tsx
- Add a new file in your
login
lib
libs/login/src/lib/admin/admin.loader.ts
import { json, LoaderFunctionArgs } from '@remix-run/node';
export const adminLoader = async ({ request }: LoaderFunctionArgs) => { return json({ message: 'Hello, world!', });};
Export the function from the libs/login/src/server.ts
file:
export * from './lib/admin/admin.loader';
- Use the loader in your
apps/myapp/app/routes/admin.tsx
Replace the default loader code:
export const loader = async ({ request }: LoaderFunctionArgs) => { return json({ message: 'Hello, world!', });};
with
import { adminLoader } from '@acme/login/server';
export const loader = adminLoader;
GitHub Repository with Example
Section titled “GitHub Repository with Example”You can see an example of an Nx Workspace using Remix by clicking below.
Example repository/nrwl/nx-recipes/tree/main/remix