From d712cc92c1262303f2e1842f6b45c68c5d770ebf Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Fri, 3 Feb 2017 09:25:31 +0530 Subject: [PATCH 1/2] 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. --- examples/.babelrc | 1 + server/build/babel/find-config-location.js | 16 ++++++++++++++++ server/build/webpack.js | 9 +++++---- 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 server/build/babel/find-config-location.js diff --git a/examples/.babelrc b/examples/.babelrc index 7a73a41bfdf76..042da22a3d6d7 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 0000000000000..6656c4ea62a9c --- /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 a3a61c17aa900..629303aff992b 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')) } From b3b912902601db950d6eb710c76c8223cecf59fd Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Fri, 3 Feb 2017 10:57:05 +0530 Subject: [PATCH 2/2] Fix failing tests. --- test/.babelrc | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 test/.babelrc diff --git a/test/.babelrc b/test/.babelrc new file mode 100644 index 0000000000000..52f9a53108d95 --- /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