Skip to content

Commit 57e9a5e

Browse files
arunodankzawa
authored andcommitted
Find custom babel config location properly. (vercel#969)
* Find custom babel config location properly. Earlier we simply check for the .bablerc file in the dir. But the actual logic is much complex. Now we are using the babel's actual logic to find the custom config location. * Fix failing tests.
1 parent 729088c commit 57e9a5e

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

examples/.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{
2+
"presets": ["../babel"]
23
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { join } from 'path'
2+
import buildConfigChain from 'babel-core/lib/transformation/file/options/build-config-chain'
3+
4+
export default function findBabelConfigLocation (dir) {
5+
// We need to provide a location of a filename inside the `dir`.
6+
// For the name of the file, we could be provide anything.
7+
const filename = join(dir, 'filename.js')
8+
const options = { babelrc: true, filename }
9+
10+
// First We need to build the config chain.
11+
// Then we need to remove the config item with the location as "base".
12+
// That's the config we are passing as the "options" below
13+
const configList = buildConfigChain(options).filter(i => i.loc !== 'base')
14+
15+
return configList[0] ? configList[0].loc : null
16+
}

server/build/webpack.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { resolve, join } from 'path'
22
import { createHash } from 'crypto'
3-
import { existsSync } from 'fs'
43
import webpack from 'webpack'
54
import glob from 'glob-promise'
65
import WriteFilePlugin from 'write-file-webpack-plugin'
@@ -11,6 +10,7 @@ import WatchPagesPlugin from './plugins/watch-pages-plugin'
1110
import JsonPagesPlugin from './plugins/json-pages-plugin'
1211
import getConfig from '../config'
1312
import * as babelCore from 'babel-core'
13+
import findBabelConfigLocation from './babel/find-config-location'
1414

1515
const documentPage = join('pages', '_document.js')
1616
const defaultPages = [
@@ -115,9 +115,10 @@ export default async function createCompiler (dir, { dev = false, quiet = false
115115
presets: []
116116
}
117117

118-
const hasBabelRc = existsSync(join(dir, '.babelrc'))
119-
if (hasBabelRc) {
120-
console.log('> Using .babelrc defined in your app root')
118+
const configLocation = findBabelConfigLocation(dir)
119+
if (configLocation) {
120+
console.log(`> Using external babel configuration`)
121+
console.log(`> location: "${configLocation}"`)
121122
} else {
122123
mainBabelOptions.presets.push(require.resolve('./babel/preset'))
123124
}

test/.babelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
// To let test apps(and Jest) to use Next's babel preset
4+
"../babel",
5+
// To transpile import statements into commonjs.
6+
// That's because Jest(runs in Node.js) don't know how to handle them.
7+
"es2015"
8+
]
9+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy