From 57e9a5e5f66f8943179d56813f3e90ba1ab48bc9 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Fri, 3 Feb 2017 11:03:35 +0530 Subject: [PATCH] Find custom babel config location properly. (#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. --- examples/.babelrc | 1 + server/build/babel/find-config-location.js | 16 ++++++++++++++++ server/build/webpack.js | 9 +++++---- test/.babelrc | 9 +++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 server/build/babel/find-config-location.js create mode 100644 test/.babelrc diff --git a/examples/.babelrc b/examples/.babelrc index 7a73a41bfd..042da22a3d 100644 --- a/examples/.babelrc +++ b/examples/.babelrc @@ -1,2 +1,3 @@ { + "presets": ["../babel"] } \ No newline at end of file diff --git a/server/build/babel/find-config-location.js b/server/build/babel/find-config-location.js new file mode 100644 index 0000000000..6656c4ea62 --- /dev/null +++ b/server/build/babel/find-config-location.js @@ -0,0 +1,16 @@ +import { join } from 'path' +import buildConfigChain from 'babel-core/lib/transformation/file/options/build-config-chain' + +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 +} diff --git a/server/build/webpack.js b/server/build/webpack.js index a3a61c17aa..629303aff9 100644 --- a/server/build/webpack.js +++ b/server/build/webpack.js @@ -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' @@ -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 = [ @@ -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')) } diff --git a/test/.babelrc b/test/.babelrc new file mode 100644 index 0000000000..52f9a53108 --- /dev/null +++ b/test/.babelrc @@ -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" + ] +} \ No newline at end of file -- GitLab