Skip to content

Commit

Permalink
Don't fail building when there is .babelrc file.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
PatrykF3D committed Oct 20, 2020
1 parent 7b50e35 commit 3327eb9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
6 changes: 6 additions & 0 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion 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 <jonathan.buchanan@gmail.com>",
"bin": {
Expand Down
28 changes: 19 additions & 9 deletions src/moduleBuild.js
Expand Up @@ -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'
Expand All @@ -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

Expand All @@ -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'})
Expand All @@ -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)
})
})
Expand Down Expand Up @@ -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) {
Expand All @@ -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)]
Expand Down

0 comments on commit 3327eb9

Please sign in to comment.