Skip to content

Commit f66484c

Browse files
Add Next.js plugin (#336)
1 parent 8a25cda commit f66484c

File tree

20 files changed

+1932
-101
lines changed

20 files changed

+1932
-101
lines changed

.changeset/config.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
"___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH": {
1313
"onlyUpdatePeerDependentsWhenOutOfRange": true
1414
},
15-
"ignore": ["@fixtures/*", "test-helpers", "vanilla-extract-webpack-react"]
15+
"ignore": [
16+
"@fixtures/*",
17+
"test-helpers",
18+
"vanilla-extract-example-next",
19+
"vanilla-extract-example-webpack-react"
20+
]
1621
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ packages/**/README.md
1313
!.yarn/plugins
1414
!.yarn/sdks
1515
!.yarn/versions
16-
.pnp.*
16+
.pnp.*
17+
.next

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ README.md
99
.github/ISSUE_TEMPLATE
1010
site/docs-manifest.json
1111
.changeset
12+
.next

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const exampleStyle = style({
5656
});
5757
```
5858

59-
> 💡 These `.css.ts` files will be evaluated at build time. None of the code in these files will be included in your final bundle. Think of it as using TypeScript as your preprocessor instead of Sass, Less, etc.
59+
> 💡 Once you've [configured your build tooling,](#setup) these `.css.ts` files will be evaluated at build time. None of the code in these files will be included in your final bundle. Think of it as using TypeScript as your preprocessor instead of Sass, Less, etc.
6060
6161
**Then consume them in your markup.**
6262

@@ -83,6 +83,7 @@ Want to work at a higher level while maximising style re-use? Check out 🍨 [S
8383
- [esbuild](#esbuild)
8484
- [Vite](#vite)
8585
- [Snowpack](#snowpack)
86+
- [Next.js](#nextjs)
8687
- [Gatsby](#gatsby)
8788
- [Test environments](#test-environments)
8889
- [Configuration](#configuration)
@@ -285,6 +286,55 @@ npm install @vanilla-extract/css @vanilla-extract/snowpack-plugin
285286

286287
> Please note: There are currently no automatic readable class names during development. However, you can still manually provide a debug ID as the last argument to functions that generate scoped styles, e.g. `export const className = style({ ... }, 'className');`
287288
289+
### Next.js
290+
291+
1. Install the dependencies.
292+
293+
```bash
294+
npm install @vanilla-extract/css @vanilla-extract/babel-plugin @vanilla-extract/next-plugin
295+
```
296+
297+
2. If you don't have a `.babelrc` file in the root of your project, create one. Add the [Babel](https://babeljs.io) plugin to your `.babelrc` file, ensuring that you're also including `"next/babel"` in your `presets` array.
298+
299+
```json
300+
{
301+
"presets": ["next/babel"],
302+
"plugins": ["@vanilla-extract/babel-plugin"]
303+
}
304+
```
305+
306+
3. If you don't have a `next.config.js` file in the root of your project, create one. Add the [Next.js](https://nextjs.org) plugin to your `next.config.js` file.
307+
308+
> 💡 This plugin accepts an optional [configuration object](#configuration).
309+
310+
```js
311+
const {
312+
createVanillaExtractPlugin
313+
} = require('@vanilla-extract/next-plugin');
314+
const withVanillaExtract = createVanillaExtractPlugin();
315+
316+
const nextConfig = {};
317+
318+
module.exports = withVanillaExtract(nextConfig);
319+
```
320+
321+
If required, this plugin can be composed with other plugins.
322+
323+
```js
324+
const {
325+
createVanillaExtractPlugin
326+
} = require('@vanilla-extract/next-plugin');
327+
const withVanillaExtract = createVanillaExtractPlugin();
328+
329+
const withMDX = require('@next/mdx')({
330+
extension: /\.mdx$/
331+
});
332+
333+
const nextConfig = {};
334+
335+
module.exports = withVanillaExtract(withMDX(nextConfig));
336+
```
337+
288338
### Gatsby
289339

290340
To add to your [Gatsby](https://www.gatsbyjs.com) site, use the [gatsby-plugin-vanilla-extract](https://github.com/KyleAMathews/gatsby-plugin-vanilla-extract) plugin.

examples/next/.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["next/babel"],
3+
"plugins": ["@vanilla-extract/babel-plugin"]
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { style } from '@vanilla-extract/css';
2+
3+
export const root = style({
4+
background: 'pink',
5+
color: 'blue',
6+
padding: '16px',
7+
transition: 'opacity .1s ease', // Testing autoprefixer
8+
':hover': {
9+
opacity: 0.8,
10+
},
11+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as styles from './HelloWorld.css';
2+
3+
export function HelloWorld() {
4+
return (
5+
<div className={styles.root}>
6+
<h1>🧁 Hello from vanilla-extract!</h1>
7+
</div>
8+
);
9+
}

examples/next/next-env.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/types/global" />
3+
/// <reference types="next/image-types/global" />
4+
5+
// NOTE: This file should not be edited
6+
// see https://nextjs.org/docs/basic-features/typescript for more information.

examples/next/next.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { createVanillaExtractPlugin } = require('@vanilla-extract/next-plugin');
2+
const withVanillaExtract = createVanillaExtractPlugin();
3+
4+
module.exports = withVanillaExtract();

examples/next/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "vanilla-extract-example-next",
3+
"description": "Example project using vanilla-extract, compiling with Next.js",
4+
"version": "0.0.0",
5+
"private": true,
6+
"scripts": {
7+
"dev": "next dev",
8+
"build": "next build",
9+
"start": "next start"
10+
},
11+
"dependencies": {
12+
"next": "^11.0.0",
13+
"react": "^17.0.2",
14+
"react-dom": "^17.0.2"
15+
},
16+
"devDependencies": {
17+
"@types/react": "^17",
18+
"@vanilla-extract/babel-plugin": "^1.0.1",
19+
"@vanilla-extract/css": "^1.4.0",
20+
"@vanilla-extract/next-plugin": "^1.0.0"
21+
},
22+
"browserslist": [
23+
">0.3%",
24+
"not ie 11",
25+
"not dead",
26+
"not op_mini all"
27+
]
28+
}

0 commit comments

Comments
 (0)