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

Find custom babel config location properly. #969

Merged
merged 2 commits into from
Feb 3, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/.babelrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{
"presets": ["../babel"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier our location detection logic couldn't find this.
So, we add next/babel by default.

That's why providing a empty .babelrc here worked.
But now we have to do this.

}
16 changes: 16 additions & 0 deletions server/build/babel/find-config-location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { join } from 'path'
import buildConfigChain from 'babel-core/lib/transformation/file/options/build-config-chain'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe importing private module is inevitable ? :|

Copy link
Contributor Author

@arunoda arunoda Feb 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, we have to copy the code or logic.
I thought this method might be a better option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit as a Babel maintainer this makes me a little sad. If there's something we're not providing in the API, why not ask for it? Now if we change stuff like this we'll randomly break you with no way of knowing, and I'm 100% sure we'll get angry users coming to yell at us.


export default function findBabelConfigLocation (dir) {
// We need to provide a location of a filename inside the `dir`.
// For the name of the file, we could be provide anything.
const filename = join(dir, 'filename.js')
const options = { babelrc: true, filename }

// First We need to build the config chain.
// Then we need to remove the config item with the location as "base".
// That's the config we are passing as the "options" below
const configList = buildConfigChain(options).filter(i => i.loc !== 'base')

return configList[0] ? configList[0].loc : null
}
9 changes: 5 additions & 4 deletions server/build/webpack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { resolve, join } from 'path'
import { createHash } from 'crypto'
import { existsSync } from 'fs'
import webpack from 'webpack'
import glob from 'glob-promise'
import WriteFilePlugin from 'write-file-webpack-plugin'
Expand All @@ -11,6 +10,7 @@ import WatchPagesPlugin from './plugins/watch-pages-plugin'
import JsonPagesPlugin from './plugins/json-pages-plugin'
import getConfig from '../config'
import * as babelCore from 'babel-core'
import findBabelConfigLocation from './babel/find-config-location'

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

const hasBabelRc = existsSync(join(dir, '.babelrc'))
if (hasBabelRc) {
console.log('> Using .babelrc defined in your app root')
const configLocation = findBabelConfigLocation(dir)
if (configLocation) {
console.log(`> Using external babel configuration`)
console.log(`> location: "${configLocation}"`)
} else {
mainBabelOptions.presets.push(require.resolve('./babel/preset'))
}
Expand Down
9 changes: 9 additions & 0 deletions test/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
// To let test apps(and Jest) to use Next's babel preset
"../babel",
// To transpile import statements into commonjs.
// That's because Jest(runs in Node.js) don't know how to handle them.
"es2015"
]
}