Code EcmaScript Guides

Babel 7 dynamic config – making Babel work with esm, Jest, JSX, and Preact server-side rendering

Babel enables use of modern JS code (ES2015 — ES2018) in browsers and environments that don’t inherently support it using a technique known as transpiling. One of the features I enjoy about Babel 7 (currently at beta 46) is the ability to provide a dynamic .babelrc.js configuration file rather than a statically defined .babelrc (JSON) file.

I recently adopted esm (for ES Modules support in Node.js; import and export) on the Verses for Life website.  One of the challenges I ran into with using esm was getting my server to use esm by default for development and production work, but still having my tests work.  (For general esm setup, see esm on npm.)

I’d already decided in the past to use Jest, but, unfortunately, Jest doesn’t play nicely with esm by default and Jest doesn’t support a --require flag like many other JS testing libraries.  So what to do?

The solution is allowing Jest to use Babel to transpile the modules during testing (so you avoid those pesky “unexpected token import” errors), but keeping esm in place for all your other dev and production work.

I set up my Babel 7 config (.babelrc.js) to use Babel to handle modules only in test mode, but esm does the work everywhere else.

.babelrc.js code

This approach enabled my tests to work again.  The downside of this approach is that the way modules are loaded in testing doesn’t match my other environments.  You’ll want to ensure you have appropriate integration tests (a la Puppeteer) to ensure your site works end-to-end.

If you’re early on in deciding your tooling, you can look at the list of the many other test libraries supported by esmand maybe you won’t need this work-around.


Verses for Life now supports iPad and iPhone

I’ve updated the Verses for Life app to include iPad support in addition to the iPhone app.  Download Verses for Life from the App Store for your iPhone or iPad.

If you don’t have an iPhone or iPad, you can access the same verse content directly from the Verses for Life website.  It includes the Topical Memory System, Roman Road, and Prayer verse packs in all the same Bible versions (CEV, ESV, KJV, NASB).  Bookmark the site or save it to the home screen on your smartphone for easy access.


2017 in Review

A short list of highlights from 2017

  • traveled to 8 countries (4 for work, 4 for vacation)
  • read 27 books
  • 10,000+ downloads of Verses for Life (website, app)
  • presented at Amazon’s WebDevCon and WebDevCon EU internal conferences