Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fail building when there is .babelrc file. #562

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 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.

# MISSING
## Dependencies

- @babel/cli: v7.8.4 → [v7.10.3](https://github.com/babel/babel/blob/master/CHANGELOG.md#v7102-2020-05-30)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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.2",
"version": "0.26.0",
"license": "MIT",
"author": "Jonny Buchanan <[email protected]>",
"bin": {
Expand Down
28 changes: 19 additions & 9 deletions src/moduleBuild.js
Original file line number Diff line number Diff line change
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