From b220193167069acf9641b02d31437c8bf741a5df Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Mon, 13 Feb 2017 02:48:22 +0530 Subject: [PATCH] Babelrc false babelrc (#1094) * Detect babelrc disabling via babelrc. * Simplify boolean condition. --- examples/.babelrc | 2 +- ...find-config-location.js => find-config.js} | 4 ++-- server/build/webpack.js | 20 +++++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) rename server/build/babel/{find-config-location.js => find-config.js} (85%) diff --git a/examples/.babelrc b/examples/.babelrc index 042da22a..6714336e 100644 --- a/examples/.babelrc +++ b/examples/.babelrc @@ -1,3 +1,3 @@ { - "presets": ["../babel"] + "babelrc": false } \ No newline at end of file diff --git a/server/build/babel/find-config-location.js b/server/build/babel/find-config.js similarity index 85% rename from server/build/babel/find-config-location.js rename to server/build/babel/find-config.js index 6656c4ea..2acc7815 100644 --- a/server/build/babel/find-config-location.js +++ b/server/build/babel/find-config.js @@ -1,7 +1,7 @@ import { join } from 'path' import buildConfigChain from 'babel-core/lib/transformation/file/options/build-config-chain' -export default function findBabelConfigLocation (dir) { +export default function findBabelConfig (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') @@ -12,5 +12,5 @@ export default function findBabelConfigLocation (dir) { // 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 + return configList[0] } diff --git a/server/build/webpack.js b/server/build/webpack.js index 91490634..34196897 100644 --- a/server/build/webpack.js +++ b/server/build/webpack.js @@ -10,7 +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' +import findBabelConfig from './babel/find-config' const documentPage = join('pages', '_document.js') const defaultPages = [ @@ -114,16 +114,24 @@ export default async function createCompiler (dir, { dev = false, quiet = false presets: [] } - const configLocation = findBabelConfigLocation(dir) - if (configLocation) { + const externalBabelConfig = findBabelConfig(dir) + if (externalBabelConfig) { console.log(`> Using external babel configuration`) - console.log(`> location: "${configLocation}"`) - mainBabelOptions.babelrc = true + console.log(`> location: "${externalBabelConfig.loc}"`) + // It's possible to turn off babelrc support via babelrc itself. + // In that case, we should add our default preset. + // That's why we need to do this. + const { options } = externalBabelConfig + mainBabelOptions.babelrc = options.babelrc !== false } else { - mainBabelOptions.presets.push(require.resolve('./babel/preset')) mainBabelOptions.babelrc = false } + // Add our default preset if the no "babelrc" found. + if (!mainBabelOptions.babelrc) { + mainBabelOptions.presets.push(require.resolve('./babel/preset')) + } + const rules = (dev ? [{ test: /\.js(\?[^?]*)?$/, loader: 'hot-self-accept-loader',