From f73cd0efcb168cc304f40e33ff24f6951ef6663b Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Fri, 6 Jan 2023 12:01:56 +1100 Subject: [PATCH 01/12] initial Vanilla Extract integration --- .changeset/giant-kings-repair.md | 8 + .changeset/hungry-owls-greet.md | 2 +- .changeset/little-avocados-fetch.md | 2 +- docs/guides/styling.md | 62 ++- integration/css-modules-test.ts | 1 + integration/css-side-effect-imports-test.ts | 1 + .../deterministic-build-output-test.ts | 17 +- integration/vanilla-extract-test.ts | 394 ++++++++++++++++++ package.json | 1 + .../remix-dev/__tests__/readConfig-test.ts | 2 + packages/remix-dev/compiler/compileBrowser.ts | 23 +- packages/remix-dev/compiler/compilerServer.ts | 8 +- .../compiler/plugins/vanillaExtractPlugin.ts | 107 +++++ packages/remix-dev/config.ts | 2 + packages/remix-dev/package.json | 1 + packages/remix-react/entry.ts | 1 + packages/remix-server-runtime/entry.ts | 1 + packages/remix-testing/create-remix-stub.tsx | 1 + yarn.lock | 149 ++++++- 19 files changed, 772 insertions(+), 11 deletions(-) create mode 100644 .changeset/giant-kings-repair.md create mode 100644 integration/vanilla-extract-test.ts create mode 100644 packages/remix-dev/compiler/plugins/vanillaExtractPlugin.ts diff --git a/.changeset/giant-kings-repair.md b/.changeset/giant-kings-repair.md new file mode 100644 index 00000000000..0afc9d0b354 --- /dev/null +++ b/.changeset/giant-kings-repair.md @@ -0,0 +1,8 @@ +--- +"@remix-run/dev": minor +"@remix-run/react": minor +"@remix-run/server-runtime": minor +"@remix-run/testing": minor +--- + +Add unstable support for [Vanilla Extract](https://vanilla-extract.style) via the `future.unstable_vanillaExtract` feature flag diff --git a/.changeset/hungry-owls-greet.md b/.changeset/hungry-owls-greet.md index 3257dafd368..a40b1fdcef3 100644 --- a/.changeset/hungry-owls-greet.md +++ b/.changeset/hungry-owls-greet.md @@ -5,4 +5,4 @@ "@remix-run/testing": minor --- -Add unstable support for CSS side-effect imports via `future.unstable_cssSideEffectImports` feature flag +Add unstable support for CSS side-effect imports via the `future.unstable_cssSideEffectImports` feature flag diff --git a/.changeset/little-avocados-fetch.md b/.changeset/little-avocados-fetch.md index a090ecb9575..b8873ce2946 100644 --- a/.changeset/little-avocados-fetch.md +++ b/.changeset/little-avocados-fetch.md @@ -6,4 +6,4 @@ "@remix-run/testing": minor --- -Add unstable support for CSS Modules via `future.unstable_cssModules` feature flag +Add unstable support for CSS Modules via the `future.unstable_cssModules` feature flag diff --git a/docs/guides/styling.md b/docs/guides/styling.md index ff57b457700..7577dc845d0 100644 --- a/docs/guides/styling.md +++ b/docs/guides/styling.md @@ -772,7 +772,7 @@ NOTE: You may run into hydration warnings when using Styled Components. Hopefull Many common approaches to CSS within the React community are only possible when bundling CSS, meaning that the CSS files you write during development are collected into a separate bundle as part of the build process. -When using CSS bundling features, the Remix compiler will generate a single CSS file containing all bundled styles in your application. Note that any [regular stylesheet imports][regular-stylesheet-imports-2] will remain as separate files. +When using CSS bundling features, the Remix compiler will generate a single CSS file containing all bundled styles in your application. Note that any [regular stylesheet imports][regular-stylesheet-imports] will remain as separate files. Unlike many other tools in the React ecosystem, we do not insert the CSS bundle into the page automatically. Instead, we ensure that you always have control over the link tags on your page. This lets you decide where the CSS file is loaded relative to other stylesheets in your app. @@ -843,6 +843,63 @@ export const Button = React.forwardRef( Button.displayName = "Button"; ``` +### Vanilla Extract + +This feature is unstable and currently only available behind a feature flag. We're confident in the use cases it solves but the API and implementation may change in the future. + +[Vanilla Extract][vanilla-extract] is a zero-runtime CSS-in-TypeScript (or JavaScript) library that lets you use TypeScript as your CSS preprocessor. Styles are written in separate `*.css.ts` (or `*.css.js`) files and all code within them is executed during the build process rather than in your user's browser. + +First, ensure you've set up [CSS bundling][css-bundling] in your application. + +Next, install Vanilla Extract's core styling package as a dev dependency. + +```sh +npm install -D @vanilla-extract/css +``` + +Then, to enable Vanilla Extract, set the `future.unstable_vanillaExtract` feature flag in `remix.config.js`. + +```js filename=remix.config.js +/** @type {import('@remix-run/dev').AppConfig} */ +module.exports = { + future: { + unstable_vanillaExtract: true, + }, + // ... +}; +``` + +With this feature flag enabled, you can now opt into Vanilla Extract via the `.css.ts`/`.css.js` file name convention. For example: + +```ts filename=app/components/button/styles.css.ts +import { style } from "@vanilla-extract/css"; + +export const root = style({ + border: 'solid 1px', + background: 'white', + color: '#454545', +}); +``` + +```tsx filename=app/components/button/index.js lines=[1,9] +import * as styles from "./styles.css"; // Note that `.ts` is omitted here + +export const Button = React.forwardRef( + ({ children, ...props }, ref) => { + return ( +