Skip to content

Commit 27889c8

Browse files
refactor(vanilla-extract): migrate to built-in Vanilla Extract support (#213)
1 parent 21e5247 commit 27889c8

File tree

12 files changed

+27
-96
lines changed

12 files changed

+27
-96
lines changed

vanilla-extract/.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/** @type {import('eslint').Linter.Config} */
22
module.exports = {
33
extends: ["@remix-run/eslint-config", "@remix-run/eslint-config/node"],
4-
ignorePatterns: ["app/styles/**/*"],
54
};

vanilla-extract/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ node_modules
44
/build
55
/public/build
66
.env
7-
8-
/app/styles

vanilla-extract/.styles/index.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

vanilla-extract/README.md

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# vanilla-extract example
1+
# Vanilla Extract example
22

3-
Integrate Remix with [vanilla-extract.](https://vanilla-extract.style)
3+
Integrate Remix with [Vanilla Extract.](https://vanilla-extract.style)
44

55
## Preview
66

@@ -10,33 +10,19 @@ Open this example on [CodeSandbox](https://codesandbox.com):
1010

1111
## Example
1212

13-
This example shows how to use vanilla-extract in Remix. Vanilla-extract is a zero-runtime CSS-in-TypeScript library that converts `.css.ts` files into static CSS at build time, providing first-class support for local-scoping of classes, variables, themes and more.
14-
15-
## Architecture and Tradeoffs
16-
17-
- A separate [tsup](https://github.com/egoist/tsup) process is needed to generate CSS, JavaScript and type definitions, whereas vanilla-extract is typically hooked up to your bundler. Effectively we're treating vanilla-extract more like a traditional CSS preprocessor like [Sass](https://sass-lang.com) or [Less.](https://lesscss.org)
18-
- All styles from `.css.ts` files need to be manually re-exported from `/.styles/index.ts` which is then compiled into `/app/styles` for the Remix app to consume. You can think of it as maintaining a style manifest file. Conceptually this is the same as writing an `index.scss` file in Sass, except that JavaScript code and type definitions are generated in addition to CSS.
19-
- All styles are built into `/app/styles/index.css` which your Remix app needs to include via a `links` function.
20-
- Remix app code always needs to import styles from `~/styles`, even if a `.css.ts` file is in the same directory as the Remix code that imports it. If you don't do this, your vanilla-extract styles won't be compiled properly as they will go directly through the Remix compiler.
21-
- You can keep the file size down using [Sprinkles](https://vanilla-extract.style/documentation/packages/sprinkles) which generates compression-friendly atomic CSS. You can then access these classes at runtime via the type-safe `sprinkles` function.
22-
- To reduce boilerplate in your React code, Sprinkles can be wired up to a `Box` component as demonstrated in this project, allowing you to access atomic styles via props.
13+
This example shows how to use the built-in Vanilla Extract support in Remix. Vanilla Extract is a zero-runtime CSS-in-TypeScript library that converts `.css.ts` files into static CSS at build time, providing first-class support for local-scoping of classes, variables, themes and more.
2314

2415
## Relevant Files
2516

26-
- [tsup.config.js](./tsup.config.js) where tsup is configured with the vanilla-extract esbuild plugin.
27-
- [package.json](./package.json) where tsup is hooked up to the `dev` and `build` scripts using [npm-run-all.](http://npmjs.com/package/npm-run-all)
28-
- [.styles/index.ts](./.styles/index.ts) where everything generated by vanilla-extract is re-exported for the Remix app to consume.
29-
- [.styles/global.css.ts](./.styles/global.css.ts) where global styles are defined.
30-
- [.styles/sprinkles.css.ts](./.styles/sprinkles.css.ts) where utility classes are configured using Sprinkles.
31-
- [app/root.tsx](./app/root.tsx) where the generated CSS file is imported into the Remix app.
17+
- [remix.config.js](./remix.config.js) where the `future.unstable_vanillaExtract` flag is enabled.
18+
- [app/root.tsx](./app/root.tsx) where the CSS bundle is imported into the Remix app, provided by the `@remix-run/css-bundle` package.
19+
- [app/global.css.ts](./.styles/global.css.ts) where global styles are defined.
20+
- [app/components/Box/sprinkles.css.ts](./.styles/sprinkles.css.ts) where utility classes are configured using Sprinkles.
3221
- [app/components/Box/Box.ts](./app/components/Box/Box.ts) where the utility classes generated by Sprinkles are hooked up to a primitive `Box` component.
33-
- [app/components/Text/Text.tsx](./app/components/Text/Text.tsx) where you can see an example of component-level styles being imported from `~/styles`.
22+
- [app/components/Text/Text.tsx](./app/components/Text/Text.tsx) where you can see an example of component-level styles being consumed.
3423
- [app/components/Text/Text.css.ts](./app/components/Text/Text.css.ts) where you can see an example of component-level styles being defined.
35-
- [.eslintrc](.eslintrc) where everything in the generated `/app/styles` directory is added to ESLint's `ignorePatterns`.
36-
- [.gitignore](.gitignore) where the generated `/app/styles` directory is added to the ignore list.
3724

3825
## Related Links
3926

4027
- [vanilla-extract](https://vanilla-extract.style)
4128
- [Sprinkles](https://vanilla-extract.style/documentation/packages/sprinkles)
42-
- [tsup](https://tsup.egoist.dev)

vanilla-extract/app/components/Box/Box.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import type { ClassValue } from "clsx";
99
import clsx from "clsx";
1010
import type { AllHTMLAttributes, ElementType } from "react";
11-
import { createElement } from "react";
12-
import { forwardRef } from "react";
11+
import { createElement, forwardRef } from "react";
1312

14-
import type { Sprinkles } from "~/styles";
15-
import { sprinkles } from "~/styles";
13+
import type { Sprinkles } from "./sprinkles.css";
14+
import { sprinkles } from "./sprinkles.css";
1615

1716
interface ExtendedBoxProps extends Sprinkles {
1817
as?: ElementType;

vanilla-extract/app/components/Text/Text.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
/**
2-
* Note that styles are imported from "~/styles" rather than
3-
* "./Text.css.ts". This is because we need to import the
4-
* compiled output from the vanilla-extract build step,
5-
* otherwise our `.css.ts` files would go through the Remix
6-
* compiler and wouldn't generate any static CSS.
7-
*/
81
import type { ReactNode } from "react";
92

10-
import { componentStyles } from "~/styles";
11-
123
import { Box } from "../Box/Box";
134

14-
// Select the styles that we scoped to this component
15-
const styles = componentStyles.Text;
5+
import * as styles from "./Text.css";
166

177
export function Text({
188
children,

vanilla-extract/app/root.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { MetaFunction } from "@remix-run/node";
1+
import "./global.css";
2+
3+
import { cssBundleHref } from "@remix-run/css-bundle";
4+
import type { LinksFunction, MetaFunction } from "@remix-run/node";
25
import {
36
Links,
47
LiveReload,
@@ -8,16 +11,9 @@ import {
811
ScrollRestoration,
912
} from "@remix-run/react";
1013

11-
import vanillaExtractStyles from "~/styles/index.css";
12-
13-
export function links() {
14-
return [
15-
{
16-
rel: "stylesheet",
17-
href: vanillaExtractStyles,
18-
},
19-
];
20-
}
14+
export const links: LinksFunction = () => {
15+
return cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : [];
16+
};
2117

2218
export const meta: MetaFunction = () => ({
2319
charset: "utf-8",

vanilla-extract/package.json

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,18 @@
44
"*.css.ts"
55
],
66
"scripts": {
7-
"build": "run-s \"build:*\"",
8-
"build:css": "npm run generate:css",
9-
"build:remix": "remix build",
10-
"dev": "run-p \"dev:*\"",
11-
"dev:css": "npm run generate:css -- --watch",
12-
"dev:remix": "remix dev",
13-
"generate:css": "tsup",
7+
"build": "remix build",
8+
"dev": "remix dev",
149
"start": "remix-serve build",
1510
"typecheck": "tsc"
1611
},
1712
"dependencies": {
13+
"@remix-run/css-bundle": "~1.14.2",
1814
"@remix-run/node": "~1.14.2",
1915
"@remix-run/react": "~1.14.2",
2016
"@remix-run/serve": "~1.14.2",
21-
"@vanilla-extract/css": "^1.9.0",
22-
"@vanilla-extract/sprinkles": "^1.5.0",
17+
"@vanilla-extract/css": "^1.11.0",
18+
"@vanilla-extract/sprinkles": "^1.5.2",
2319
"clsx": "^1.2.1",
2420
"isbot": "^3.6.5",
2521
"react": "^18.2.0",
@@ -30,10 +26,7 @@
3026
"@remix-run/eslint-config": "~1.14.2",
3127
"@types/react": "^18.0.25",
3228
"@types/react-dom": "^18.0.8",
33-
"@vanilla-extract/esbuild-plugin": "^2.1.0",
3429
"eslint": "^8.27.0",
35-
"npm-run-all": "^4.1.5",
36-
"tsup": "^6.2.3",
3730
"typescript": "^4.8.4"
3831
},
3932
"engines": {

0 commit comments

Comments
 (0)