Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: upgraded remix compiler to remix vite #274

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ WORKDIR /myapp
COPY --from=production-deps /myapp/node_modules /myapp/node_modules
COPY --from=build /myapp/node_modules/.prisma /myapp/node_modules/.prisma

COPY --from=build /myapp/build /myapp/build
COPY --from=build /myapp/public /myapp/public
COPY --from=build /myapp/build/server /myapp/build/server
COPY --from=build /myapp/build/client /myapp/build/client
COPY --from=build /myapp/package.json /myapp/package.json
COPY --from=build /myapp/start.sh /myapp/start.sh
COPY --from=build /myapp/prisma /myapp/prisma

ENTRYPOINT [ "./start.sh" ]
ENTRYPOINT [ "./start.sh" ]
12 changes: 2 additions & 10 deletions app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { cssBundleHref } from "@remix-run/css-bundle";
import type { LinksFunction, LoaderFunctionArgs } from "@remix-run/node";
import type { LoaderFunctionArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import {
Links,
LiveReload,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "@remix-run/react";

import { getUser } from "~/session.server";
import stylesheet from "~/tailwind.css";

export const links: LinksFunction = () => [
{ rel: "stylesheet", href: stylesheet },
...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []),
];
import "~/tailwind.css";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could also do:

import stylesheet from "~/tailwind.css?url";

export const links: LinksFunction = () => [
  { rel: "stylesheet", href: stylesheet }
];

I don't know if there is a preferred way though. I just used it like that.


export const loader = async ({ request }: LoaderFunctionArgs) => {
return json({ user: await getUser(request) });
Expand All @@ -35,7 +28,6 @@ export default function App() {
<Outlet />
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion remix.env.d.ts → env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/// <reference types="@remix-run/dev" />
/// <reference types="vite/client" />
/// <reference types="@remix-run/node" />
4 changes: 2 additions & 2 deletions mocks/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { rest } = require("msw");
const { setupServer } = require("msw/node");
import { rest } from "msw";
import { setupServer } from "msw/node";

// put one-off handlers that don't really need an entire file to themselves here
const miscHandlers = [
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "indie-stack-template",
"type": "module",
"private": true,
"sideEffects": false,
"scripts": {
"build": "remix build",
"dev": "remix dev -c \"npm run dev:serve\"",
"dev:serve": "binode --require ./mocks -- @remix-run/serve:remix-serve ./build/index.js",
"build": "remix vite:build",
"dev": "remix vite:dev",
"format": "prettier --write .",
"format:repo": "npm run format && npm run lint -- --fix",
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint .",
"setup": "prisma generate && prisma migrate deploy && prisma db seed",
"start": "remix-serve ./build/index.js",
"start:mocks": "binode --require ./mocks -- @remix-run/serve:remix-serve ./build/index.js",
"start": "remix-serve ./build/server/index.js",
"start:mocks": "binode --import ./mocks/index.js -- @remix-run/serve:remix-serve ./build/server/index.js",
"test": "vitest",
"test:e2e:dev": "start-server-and-test dev http://localhost:3000 \"npx cypress open\"",
"pretest:e2e:run": "npm run build",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

facing issues while running validate as npx cypress run command is failing with an error ReferenceError: exports is not defined in ES module scope

Tried updating tsconfig.json by following cypress-io/cypress#23552 (comment) but couldn't make it work. Will check again

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've migrated an Indie Stack application to vite.
I'd recommend first moving to ESM and then afterwards migrating to vite. Makes it easier to know where the error is from 😅

Regarding your issue I ended up switching to tsx instead of ts-node as I couldn't get it to work with ts-node.
Using tsx I modified the file cypress/support/commands.ts according to this:

-cy.exec(
-  `npx ts-node -r tsconfig-paths/register ./cypress/support/create-user.ts "${email}"`,
-).then(({ stdout }) => {
-  const cookieValue = stdout
-    .replace(/.*<cookie>(?<cookieValue>.*)<\/cookie>.*/s, "$<cookieValue>")
-    .trim();
-  cy.setCookie("__session", cookieValue);
-});
+cy.exec(`tsx ./cypress/support/create-user.ts "${email}"`).then(
+  ({ stdout }) => {
+    const cookieValue = stdout
+      .replace(/.*<cookie>(?<cookieValue>.*)<\/cookie>.*/s, "$<cookieValue>")
+      .trim();
+    cy.setCookie("__session", cookieValue);
+  },
+);

-cy.exec(
-  `npx ts-node -r tsconfig-paths/register ./cypress/support/delete-user.ts "${email}"`,
-);
+cy.exec(`tsx ./cypress/support/delete-user.ts "${email}"`);

And package.json

"scripts": {
    "build": "remix vite:build",
    "dev": "binode --import ./mocks/index.js -- @remix-run/dev:remix vite:dev",
    "format": "prettier --write .",
    "lint": "eslint --cache --cache-location ./node_modules/.cache/eslint .",
    "setup": "prisma generate && prisma migrate deploy && prisma db seed",
    "start": "remix-serve ./build/server/index.js",
    "start:mocks": "binode --import ./mocks/index.js -- @remix-run/serve:remix-serve ./build/server/index.js",
    "test": "vitest",
    "test:e2e:dev": "start-server-and-test dev http://localhost:3000 \"npx cypress open\"",
    "pretest:e2e:run": "npm run build",
    "test:e2e:run": "cross-env PORT=8811 start-server-and-test start:mocks http://localhost:8811 \"npx cypress run\"",
    "typecheck": "tsc && tsc -p cypress",
    "validate": "run-p \"test -- --run\" lint typecheck test:e2e:run",
    "prisma:studio": "prisma studio",
    "optimize:images": "tsx app/optimize-images.ts"
  },

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Apsysikal, thanks for the input. I tried tsx and still getting zsh:1: command not found: tsx

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you confirm what shell are you using? I read there's some issue with zsh in particular with Cypress

cypress-io/cypress#6081

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-05-04 at 7 34 37 PM

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also got an error (different one) but fixed it on my end. I used bash. The changes made (should also probably update the comment) are:

cypress/support/create-user.ts

-// npx ts-node -r tsconfig-paths/register ./cypress/support/create-user.ts username@example.com,
+// npx tsx ./cypress/support/create-user.ts username@example.com,

cypress/support/delete-user.ts

-// npx ts-node -r tsconfig-paths/register ./cypress/support/delete-user.ts username@example.com,
+// npx tsx ./cypress/support/delete-user.ts username@example.com,

-import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
+import { Prisma } from "@prisma/client";

-error instanceof PrismaClientKnownRequestError &&
+error instanceof Prisma.PrismaClientKnownRequestError &&

You should also add tsx to the devDependencies.

package.json

+"tsx": "^4.9.1",

Expand All @@ -27,7 +27,6 @@
],
"dependencies": {
"@prisma/client": "^4.16.2",
"@remix-run/css-bundle": "*",
"@remix-run/node": "*",
"@remix-run/react": "*",
"@remix-run/serve": "*",
Expand Down Expand Up @@ -80,14 +79,14 @@
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.2.2",
"vite": "^4.5.0",
"vite": "^5.1.6",
"vite-tsconfig-paths": "^4.2.1",
"vitest": "^0.34.6"
},
"engines": {
"node": ">=18.0.0"
},
"prisma": {
"seed": "ts-node -r tsconfig-paths/register prisma/seed.ts"
"seed": "ts-node-esm -r tsconfig-paths/register prisma/seed.ts"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to use 'tsx' this could also just be moved to it. So it would become:

"seed": "tsx prisma/seed.ts"

}
}
2 changes: 1 addition & 1 deletion postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
Expand Down
6 changes: 0 additions & 6 deletions remix.config.js

This file was deleted.

6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"exclude": ["./cypress", "./cypress.config.ts"],
"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
"include": ["env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ES2020"],
"types": ["vitest/globals"],
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"module": "CommonJS",
"moduleResolution": "node",
"module": "ESNext",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"target": "ES2020",
"strict": true,
Expand Down
18 changes: 18 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { vitePlugin as remix } from "@remix-run/dev";
import { installGlobals } from "@remix-run/node";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

installGlobals();

export default defineConfig({
server: {
port: 3000,
},
plugins: [
remix({
ignoredRouteFiles: ["**/*.css"],
}),
tsconfigPaths(),
],
});