From 3327eb92f434ed53ef24021f3a42d39b548259bf Mon Sep 17 00:00:00 2001 From: Patryk Grunt Date: Tue, 20 Oct 2020 15:22:16 +0200 Subject: [PATCH] Don't fail building when there is .babelrc file. Instead, use .babelrc.build for the temporary config. Babel CLI v7 allows for --config-file option with which you can specify your own config file. Thanks to that, if there is already .babelrc file in the repo, the build can still continue instead of being stopped. ref: https://babeljs.io/docs/en/babel-cli#custom-config-path --- CHANGES.md | 6 ++++++ package.json | 2 +- src/moduleBuild.js | 28 +++++++++++++++++++--------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index cf2bc318..244259c8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,9 @@ +# 0.26.0 / 2020-10-20 +## Fixes +- Don't fail building when there is .babelrc file. Instead, use .babelrc.build for the temporary config. + + [Babel CLI](https://babeljs.io/docs/en/babel-cli#custom-config-path) allows for `--config-file` option with which you can specify your own config file. Thanks to that, if there is already .babelrc file in the repo, the build can still continue instead of being stopped. + # 0.25.0 / 2020-05-20 ## Breaking Changes diff --git a/package.json b/package.json index 9bb79bf9..410531e3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nwb", "description": "A toolkit for React, Preact & Inferno apps, React libraries and other npm modules for the web, with no configuration (until you need it)", - "version": "0.25.0", + "version": "0.26.0", "license": "MIT", "author": "Jonny Buchanan ", "bin": { diff --git a/src/moduleBuild.js b/src/moduleBuild.js index a9d1f8c0..c8aef7d2 100644 --- a/src/moduleBuild.js +++ b/src/moduleBuild.js @@ -10,7 +10,6 @@ import cleanModule from './commands/clean-module' import {getPluginConfig, getUserConfig} from './config' import createBabelConfig from './createBabelConfig' import debug from './debug' -import {UserError} from './errors' import {deepToString, formatPackageName} from './utils' import webpackBuild from './webpackBuild' import {createBanner, createExternals, logGzippedFileSizes} from './webpackUtils' @@ -23,10 +22,12 @@ const DEFAULT_BABEL_IGNORE_CONFIG = [ '**/__tests__/' ] +const DEFAULT_BABEL_CONFIG_FILE = '.babelrc' + /** * Run Babel with generated config written to a temporary .babelrc. */ -function runBabel(name, {copyFiles, outDir, src}, buildBabelConfig, userConfig, cb) { +function runBabel(name, {copyFiles, outDir, src, configFileName}, buildBabelConfig, userConfig, cb) { let babelConfig = createBabelConfig(buildBabelConfig, userConfig.babel, userConfig.path) babelConfig.ignore = DEFAULT_BABEL_IGNORE_CONFIG @@ -37,7 +38,11 @@ function runBabel(name, {copyFiles, outDir, src}, buildBabelConfig, userConfig, args.push('--copy-files', '--no-copy-ignored') } - fs.writeFile('.babelrc', JSON.stringify(babelConfig, null, 2), (err) => { + if (configFileName !== DEFAULT_BABEL_CONFIG_FILE) { + args.push('--config-file', path.resolve(configFileName)) + } + + fs.writeFile(configFileName, JSON.stringify(babelConfig, null, 2), (err) => { if (err) return cb(err) let spinner = ora(`Creating ${name} build`).start() let babel = spawn(require.resolve('.bin/babel'), args, {stdio: 'inherit'}) @@ -50,7 +55,7 @@ function runBabel(name, {copyFiles, outDir, src}, buildBabelConfig, userConfig, else { spinner.succeed() } - fs.unlink('.babelrc', (unlinkError) => { + fs.unlink(configFileName, (unlinkError) => { cb(babelError || unlinkError) }) }) @@ -114,12 +119,16 @@ function buildUMD(args, buildConfig, userConfig, cb) { } export default function moduleBuild(args, buildConfig = {}, cb) { - // XXX Babel doesn't support passing the path to a babelrc file any more - if (fs.existsSync('.babelrc')) { - throw new UserError( - 'Unable to build the module as there is a .babelrc in your project\n' + - 'nwb needs to write a temporary .babelrc to configure the build' + let configFileName = DEFAULT_BABEL_CONFIG_FILE + + if (fs.existsSync(configFileName)) { + console.info( + `There is a ${configFileName} in your project ` + + `nwb needs to write a temporary ${configFileName} to configure the build, ` + + `will use ${configFileName}.build instead` ) + + configFileName += '.build' } if (!process.env.NODE_ENV) { @@ -131,6 +140,7 @@ export default function moduleBuild(args, buildConfig = {}, cb) { let babelCliOptions = { copyFiles: !!args['copy-files'], src: path.resolve('src'), + configFileName } let tasks = [(cb) => cleanModule(args, cb)]