Skip to content

Commit

Permalink
Font loader with babel error (vercel#41151)
Browse files Browse the repository at this point in the history
Adds build error when using font loaders with babel. Otherwise you'll
get other unrelated errors.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
2 people authored and Kikobeats committed Oct 24, 2022
1 parent 233abc7 commit 3183ed0
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 0 deletions.
14 changes: 14 additions & 0 deletions errors/babel-font-loader-conflict.md
@@ -0,0 +1,14 @@
# Babel and Font loader conflict

#### Why This Error Occurred

You have tried to use `experimental.fontLoaders` with a custom babel config. When your application has a custom babel config you opt-out of the Next.js Compiler which is required to use `experimental.fontLoaders`.

#### Possible Ways to Fix It

- Remove your custom babel config and use the Next.js Compiler

### Useful Links

- [Next.js Compiler](https://nextjs.org/docs/advanced-features/compiler)
- [Customizing Babel Config](https://nextjs.org/docs/advanced-features/customizing-babel-config)
4 changes: 4 additions & 0 deletions errors/manifest.json
Expand Up @@ -737,6 +737,10 @@
{
"title": "nonce-contained-invalid-characters",
"path": "/errors/nonce-contained-invalid-characters.md"
},
{
"title": "babel-font-loader-conflict",
"path": "/errors/babel-font-loader-conflict.md"
}
]
}
Expand Down
10 changes: 10 additions & 0 deletions packages/next/build/webpack-config.ts
Expand Up @@ -646,6 +646,16 @@ export default async function getBaseWebpackConfig(
loggedIgnoredCompilerOptions = true
}

if (babelConfigFile && config.experimental.fontLoaders) {
Log.error(
`"experimental.fontLoaders" is enabled which requires SWC although Babel is being used due to custom babel config being present "${path.relative(
dir,
babelConfigFile
)}".\nSee more info here: https://nextjs.org/docs/messages/babel-font-loader-conflict`
)
process.exit(1)
}

const getBabelLoader = () => {
return {
loader: require.resolve('./babel/loader/index'),
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/next-font/babel.test.ts
@@ -0,0 +1,33 @@
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { join } from 'path'

describe('@next/font/google babel', () => {
const isNextStart = (global as any).isNextStart
let next: NextInstance

if (!isNextStart) {
it('should only run on next start', () => {})
return
}

beforeAll(async () => {
next = await createNext({
skipStart: true,
files: new FileRef(join(__dirname, 'babel')),
dependencies: {
'@next/font': 'canary',
},
})
})
afterAll(() => next.destroy())

test('Build error when using babel', async () => {
await expect(next.start()).rejects.toThrow(
'next build failed with code/signal 1'
)
expect(next.cliOutput).toMatch(
/"experimental.fontLoaders" is enabled which requires SWC although Babel is being used due to custom babel config being present ".babelrc"./
)
})
})
4 changes: 4 additions & 0 deletions test/e2e/next-font/babel/.babelrc
@@ -0,0 +1,4 @@
{
"presets": ["next/babel"],
"plugins": []
}
File renamed without changes.
3 changes: 3 additions & 0 deletions test/e2e/next-font/babel/pages/index.js
@@ -0,0 +1,3 @@
export default function Page() {
return <p>Hello world</p>
}
Expand Up @@ -5,8 +5,14 @@ import webdriver from 'next-webdriver'
import { join } from 'path'

describe('font-loader-in-document-error', () => {
const isDev = (global as any).isNextDev
let next: NextInstance

if (!isDev) {
it('should only run on next dev', () => {})
return
}

beforeAll(async () => {
next = await createNext({
files: {
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/next-font/font-loader-in-document/next.config.js
@@ -0,0 +1,9 @@
module.exports = {
experimental: {
fontLoaders: {
'@next/font/google': {
subsets: ['latin'],
},
},
},
}

0 comments on commit 3183ed0

Please sign in to comment.