Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Fix babel-loader failing on JSON5 syntax (vercel#26194)
Browse files Browse the repository at this point in the history
## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added

## 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.

## Documentation / Examples

- [ ] Make sure the linting passes

Fixes: vercel#26163
  • Loading branch information
jwilsson authored Jun 16, 2021
1 parent c9a4697 commit cacf34d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/next/build/babel/loader/get-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readFileSync } from 'fs'
import JSON5 from 'next/dist/compiled/json5'

import { createConfigItem, loadOptions } from 'next/dist/compiled/babel/core'
import loadConfig from 'next/dist/compiled/babel/core-lib-config'
Expand Down Expand Up @@ -151,7 +152,7 @@ const isJsFile = /\.js$/
function getCustomBabelConfig(configFilePath: string) {
if (isJsonFile.exec(configFilePath)) {
const babelConfigRaw = readFileSync(configFilePath, 'utf8')
return JSON.parse(babelConfigRaw)
return JSON5.parse(babelConfigRaw)
} else if (isJsFile.exec(configFilePath)) {
return require(configFilePath)
}
Expand Down
12 changes: 12 additions & 0 deletions test/integration/babel-custom/fixtures/babel-json5/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"next/babel",
{
"preset-env": {
"modules": "commonjs" // Here's a comment
}
}
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <h1>Hello World</h1>
4 changes: 4 additions & 0 deletions test/integration/babel-custom/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ describe('Babel', () => {
it('should allow setting targets to a string', async () => {
await nextBuild(join(__dirname, '../fixtures/targets-string'))
})

it('should allow babelrc JSON5 syntax', async () => {
await nextBuild(join(__dirname, '../fixtures/babel-json5'))
})
})

0 comments on commit cacf34d

Please sign in to comment.