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

Add experimental flag to force SWC transforms #36789

Merged
merged 2 commits into from May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions errors/swc-disabled.md
Expand Up @@ -13,3 +13,16 @@ Many of the integrations with external libraries that currently require custom B
- Emotion

In order to prioritize transforms that will help you adopt SWC please provide your `.babelrc` on [the feedback thread](https://github.com/vercel/next.js/discussions/30174).

#### Possible Ways to Fix It

If you want to use SWC despite the presence of a `.babelrc` file you can force it in your `next.config.js` file.

```js
// next.config.js
module.exports = {
experimental: {
forceSwcTransforms: true,
},
}
```
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Expand Up @@ -411,7 +411,7 @@ export default async function getBaseWebpackConfig(

const distDir = path.join(dir, config.distDir)

let useSWCLoader = !babelConfigFile
let useSWCLoader = !babelConfigFile || config.experimental.forceSwcTransforms
let SWCBinaryTarget: [Feature, boolean] | undefined = undefined
if (useSWCLoader) {
// TODO: we do not collect wasm target yet
Expand Down
2 changes: 2 additions & 0 deletions packages/next/server/config-shared.ts
Expand Up @@ -135,6 +135,7 @@ export interface ExperimentalConfig {
}
>
swcTraceProfiling?: boolean
forceSwcTransforms?: boolean
}

/**
Expand Down Expand Up @@ -505,6 +506,7 @@ export const defaultConfig: NextConfig = {
layoutRaw: false,
remotePatterns: [],
},
forceSwcTransforms: false,
},
}

Expand Down
68 changes: 68 additions & 0 deletions test/e2e/swc-warnings/index.test.ts
@@ -0,0 +1,68 @@
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'

describe('swc warnings by default', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'.babelrc': `
{
"presets": ["next/babel"]
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())

it('should have warning', async () => {
await renderViaHTTP(next.url, '/')
expect(next.cliOutput).toContain(
'Disabled SWC as replacement for Babel because of custom Babel configuration'
)
})
})

describe('can force swc', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
nextConfig: {
experimental: {
forceSwcTransforms: true,
},
},
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'.babelrc': `
{
"presets": ["next/babel"]
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())

it('should not have warning', async () => {
await renderViaHTTP(next.url, '/')
expect(next.cliOutput).not.toContain(
'Disabled SWC as replacement for Babel because of custom Babel configuration'
)
})
})