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

babel-cli: add --skip-initial-build option to only compile on changes when watching #3489

Merged
merged 1 commit into from
May 30, 2016

Conversation

lxe
Copy link

@lxe lxe commented Apr 30, 2016

Currently, the -w option would build the files, and then keep the process running, recompiling the files on changes.

Consider this 'watch' workflow:

#/usr/bin/env bash

watch () {
  # compile sources and watch for changes
  babel src -d dest -w &

  # restart server when anything in dest/ changes
  nodemon --watch dest dest/server &

  wait
}

This causes 2 problems:

  • Since nodemon dest/server can start before babel finishes compilation of
  • Nodemon would pick up every change the in dist/ and restart the server on initialization multiple times.

Let's compile the files before starting the watch commands:

#/usr/bin/env bash

watch () {
  # compile sources
  babel src -d dest

  # compile sources and watch for changes
  babel src -d dest -w &

  # restart server when anything in dest/ changes
  nodemon dest/server &

  wait
}

This solves the first problem, however now we are recompiling things twice, which causes nodemon to flap.

We can solve all that with sleep, but that's far from ideal.

Instead, lets introduce --just-watch:

#/usr/bin/env bash

watch () {
  # compile sources
  babel src -d dest

  # just watch for changes and recompile when needed
  babel src -d dest -w --just-watch &

  # restart server when anything in dest/ changes
  nodemon dest/server &

  wait
}

This way nodemon always starts cleanly.

@loganfsmyth
Copy link
Member

Do you happen to know if there's precedent for a flag like this with any other tools? Babel can't be the only tool where this is an issue. I don't have a problem with this flag, but I feel like it'd be clearer with a name like "--skip-initial-build`

@lxe
Copy link
Author

lxe commented Apr 30, 2016

@loganfsmyth this is similar (in spirit) to the --ignoreInitial chokidar flag, which won't emit events on "initial" change. The need for this is better illustrated by programatical usage of build of tools, such as babel (via gulp, etc), where we can listen on events and dispatch actions with ease. It's harder when using vanilla CLI tools and bash.

As far as the name is concerned, --skip-initial-build is definitely better.

@lukehorvat
Copy link

lukehorvat commented Apr 30, 2016

If we're going to think about nodemon, we might want to handle removal of files too. Babel's -w doesn't clean up removed/renamed files from the output directory.

Currently I use three package.json scripts to handle it:

{
  "build": "rimraf dest && babel src -d dest",
  "start": "npm run build && node dest",
  "watch": "nodemon -w src -x npm start"
}

But it recompiles the entire app every time something changes, which is not ideal.

@codecov-io
Copy link

codecov-io commented May 8, 2016

Current coverage is 87.83%

Merging #3489 into master will not change coverage

@@             master      #3489   diff @@
==========================================
  Files           194        194          
  Lines         10128      10128          
  Methods        1537       1537          
  Messages          0          0          
  Branches       2247       2247          
==========================================
  Hits           8895       8895          
  Misses         1233       1233          
  Partials          0          0          

Powered by Codecov. Last updated by 13c961d...2255c79

@lxe
Copy link
Author

lxe commented May 12, 2016

I'm not sure why this had such major coverage impact

@lxe lxe force-pushed the optinal-nocompile-watch branch from 1f762c1 to cec8e43 Compare May 12, 2016 01:06
@@ -61,7 +61,9 @@ module.exports = function (commander, filenames) {
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Babel is generally pretty strict about arguments. It would be good to show an error if commander.skipInitialBuild is true but commander.watch is false.

@loganfsmyth
Copy link
Member

We should also squash these commits before landing this.

@hzoo hzoo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label May 16, 2016
@lxe lxe force-pushed the optinal-nocompile-watch branch from cec8e43 to b3585b5 Compare May 20, 2016 01:55
@lxe lxe changed the title babel-cli: add --just-watch option to only compile on changes babel-cli: add --skip-initial-build option to only compile on changes when watching May 20, 2016
@lxe lxe force-pushed the optinal-nocompile-watch branch from b3585b5 to 4bf6f8a Compare May 20, 2016 16:47
@loganfsmyth loganfsmyth merged commit 85ec783 into babel:master May 30, 2016
@loganfsmyth
Copy link
Member

Thanks!

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 7, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants