Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Chore/stable swc compiler options #34074

Merged
merged 24 commits into from Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b25340c
Make relay a stable compiler option
padmaia Feb 7, 2022
d3fbea9
Make styledComponents a stable compiler option
padmaia Feb 7, 2022
552adf0
Make reactRemoveProperties stable compiler option
padmaia Feb 7, 2022
ee47a7d
Make experimentalDecorators a stable compiler opt
padmaia Feb 7, 2022
0f40ac5
Make removeConsole stable compiler option
padmaia Feb 7, 2022
0346b8d
Make importSource a stable compiler option
padmaia Feb 7, 2022
3a5ea34
Fix compiler doc
padmaia Feb 7, 2022
c633094
Add telemetry
padmaia Feb 8, 2022
125c857
Update test configs
padmaia Feb 8, 2022
8843611
Merge branch 'canary' into chore/stable-swc-compiler-options
padmaia Feb 8, 2022
bf267b4
Remove jsxImportSource configuration
padmaia Feb 8, 2022
bd7bb08
Pass tests
padmaia Feb 8, 2022
4d83c5b
Add warning for ignored options when Babel is used
padmaia Feb 8, 2022
9d53c79
Add cleanup of test files
padmaia Feb 8, 2022
aa52afc
Merge branch 'canary' into chore/stable-swc-compiler-options
padmaia Feb 8, 2022
ed8fe9c
Update docs/advanced-features/compiler.md
padmaia Feb 9, 2022
b58987b
Fix doc
padmaia Feb 9, 2022
e4006db
Add version history row
padmaia Feb 9, 2022
f6ed3c3
Update packages/next/build/webpack-config.ts
padmaia Feb 9, 2022
f6d8881
Change version ordering
padmaia Feb 9, 2022
6a744cf
Fix formatting
padmaia Feb 9, 2022
df51081
Add warnings to update config
padmaia Feb 9, 2022
84a8a3d
Add jest to supported features in compiler doc
padmaia Feb 9, 2022
87b9eaa
Merge branch 'canary' into chore/stable-swc-compiler-options
kodiakhq[bot] Feb 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
143 changes: 73 additions & 70 deletions docs/advanced-features/compiler.md
Expand Up @@ -7,9 +7,10 @@ description: Learn about the Next.js Compiler, written in Rust, which transforms
<details open>
<summary><b>Version History</b></summary>

| Version | Changes |
| --------- | --------------------------------------------------------------- |
| `v12.0.0` | Next.js Compiler [introduced](https://nextjs.org/blog/next-12). |
| Version | Changes |
| --------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `v12.1.0` | Added support for Styled Components, Relay, Remove React Properties, Legacy Decorators, Remove Console, and jsxImportSource. |
styfle marked this conversation as resolved.
Show resolved Hide resolved
| `v12.0.0` | Next.js Compiler [introduced](https://nextjs.org/blog/next-12). |

</details>

Expand All @@ -30,21 +31,7 @@ We chose to build on SWC for a few reasons:
- **WebAssembly:** Rust's support for WASM is essential for supporting all possible platforms and taking Next.js development everywhere.
styfle marked this conversation as resolved.
Show resolved Hide resolved
- **Community:** The Rust community and ecosystem are amazing and still growing.

## Experimental Features

### Minification

You can opt-in to using the Next.js compiler for minification. This is 7x faster than Terser.

```js
// next.config.js

module.exports = {
swcMinify: true,
}
```

If you have feedback about `swcMinify`, please share it on the [feedback discussion](https://github.com/vercel/next.js/discussions/30237).
## Supported Features

### Styled Components

Expand All @@ -56,7 +43,7 @@ First, update to the latest version of Next.js: `npm install next@latest`. Then,
// next.config.js

module.exports = {
experimental: {
compiler: {
// ssr and displayName are configured by default
styledComponents: true,
},
Expand All @@ -65,43 +52,14 @@ module.exports = {

Currently, only the `ssr` and `displayName` transforms have been implemented. These two transforms are the main requirement for using `styled-components` in Next.js.

### Jest

Jest support not only includes the transformation previously provided by Babel, but also simplifies configuring Jest together with Next.js including:

- Auto mocking of `.css`, `.module.css` (and their `.scss` variants), and image imports
- Automatically sets up `transform` using SWC
- Loading `.env` (and all variants) into `process.env`
- Ignores `node_modules` from test resolving and transforms
- Ignoring `.next` from test resolving
- Loads `next.config.js` for flags that enable experimental SWC transforms

First, update to the latest version of Next.js: `npm install next@latest`. Then, update your `jest.config.js` file:

```js
// jest.config.js
const nextJest = require('next/jest')

// Providing the path to your Next.js app which will enable loading next.config.js and .env files
const createJestConfig = nextJest({ dir })

// Any custom config you want to pass to Jest
const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
}

// createJestConfig is exported in this way to ensure that next/jest can load the Next.js configuration, which is async
module.exports = createJestConfig(customJestConfig)
```

### Relay

To enable [Relay](https://relay.dev/) support:

```js
// next.config.js
module.exports = {
experimental: {
compiler: {
relay: {
// This should match relay.config.js
src: './',
Expand All @@ -123,7 +81,7 @@ To remove properties matching the default regex `^data-test`:
```js
// next.config.js
module.exports = {
experimental: {
compiler: {
reactRemoveProperties: true,
},
}
Expand All @@ -134,30 +92,14 @@ To remove custom properties:
```js
// next.config.js
module.exports = {
experimental: {
compiler: {
// The regexes defined here are processed in Rust so the syntax is different from
// JavaScript `RegExp`s. See https://docs.rs/regex.
reactRemoveProperties: { properties: ['^data-custom$'] },
},
}
```

### Legacy Decorators

Next.js will automatically detect `experimentalDecorators` in `jsconfig.json` or `tsconfig.json` and apply that. This is commonly used with older versions of libraries like `mobx`.

This flag is only supported for compatibility with existing applications. We do not recommend using legacy decorators in new applications.

First, update to the latest version of Next.js: `npm install next@latest`. Then, update your `jsconfig.json` or `tsconfig.json` file:

```js
{
"compilerOptions": {
"experimentalDecorators": true
}
}
```

### Remove Console

This transform allows for removing all `console.*` calls in application code (not `node_modules`). Similar to `babel-plugin-transform-remove-console`.
Expand All @@ -167,7 +109,7 @@ Remove all `console.*` calls:
```js
// next.config.js
module.exports = {
experimental: {
compiler: {
removeConsole: true,
},
}
Expand All @@ -178,14 +120,30 @@ Remove `console.*` output except `console.error`:
```js
// next.config.js
module.exports = {
experimental: {
compiler: {
removeConsole: {
exclude: ['error'],
},
},
}
```

### Legacy Decorators

Next.js will automatically detect `experimentalDecorators` in `jsconfig.json` or `tsconfig.json`. Legacy decorators are commonly used with older versions of libraries like `mobx`.

This flag is only supported for compatibility with existing applications. We do not recommend using legacy decorators in new applications.

First, update to the latest version of Next.js: `npm install next@latest`. Then, update your `jsconfig.json` or `tsconfig.json` file:

```js
{
"compilerOptions": {
"experimentalDecorators": true
}
}
```

### importSource

Next.js will automatically detect `jsxImportSource` in `jsconfig.json` or `tsconfig.json` and apply that. This is commonly used with libraries like Theme UI.
Expand All @@ -195,11 +153,56 @@ First, update to the latest version of Next.js: `npm install next@latest`. Then,
```js
{
"compilerOptions": {
"jsxImportSource": true
"jsxImportSource": 'preact'
}
}
```

## Experimental Features

### Minification

You can opt-in to using the Next.js compiler for minification. This is 7x faster than Terser.

```js
// next.config.js

module.exports = {
swcMinify: true,
}
```

If you have feedback about `swcMinify`, please share it on the [feedback discussion](https://github.com/vercel/next.js/discussions/30237).

### Jest

Jest support not only includes the transformation previously provided by Babel, but also simplifies configuring Jest together with Next.js including:

- Auto mocking of `.css`, `.module.css` (and their `.scss` variants), and image imports
- Automatically sets up `transform` using SWC
- Loading `.env` (and all variants) into `process.env`
- Ignores `node_modules` from test resolving and transforms
- Ignoring `.next` from test resolving
- Loads `next.config.js` for flags that enable experimental SWC transforms

First, update to the latest version of Next.js: `npm install next@latest`. Then, update your `jest.config.js` file:

```js
// jest.config.js
const nextJest = require('next/jest')

// Providing the path to your Next.js app which will enable loading next.config.js and .env files
const createJestConfig = nextJest({ dir })

// Any custom config you want to pass to Jest
const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
}

// createJestConfig is exported in this way to ensure that next/jest can load the Next.js configuration, which is async
module.exports = createJestConfig(customJestConfig)
```

## Unsupported Features

When your application has a `.babelrc` file, Next.js will automatically fall back to using Babel for transforming individual files. This ensures backwards compatibility with existing applications that leverage custom Babel plugins.
Expand Down
9 changes: 9 additions & 0 deletions errors/ignored-compiler-options.md
@@ -0,0 +1,9 @@
# ignored-compiler-options

#### Why This Error Occurred

Options under the `compiler` key in `next.config.js` only apply to the new Rust based compiler and will be ignored if Babel is used instead. This message will appear if Next.js detects a Babel configuration file while `compiler` options are configured in `next.config.js`

### Useful Links

- [Next.js Compiler](https://nextjs.org/docs/advanced-features/compiler)