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

Fix babel-loader failing on JSON5 syntax #26194

Merged
merged 6 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'))
})
})