datamade / how-to

📚 Doing all sorts of things, the DataMade way
MIT License
88 stars 12 forks source link

Revisit JavaScript build tooling #149

Closed hancush closed 3 years ago

hancush commented 3 years ago

Our current toolchain for transpiling JavaScript is to add a custom precompiler to Django Compressor calling Browserify with a Babelify transform step, generally using offline compression to bundle the resulting scripts as static assets. This works ok, but some less than ideal features include:

I've discussed each of these at more length in https://github.com/datamade/how-to/issues/149#issuecomment-738339006. I'd like to look at what we'd need to change to break transpilation into its own build step, rather than running it at as a precompiler via Django Compressor.

Original description

Documentation request

There are a lot of config files that go into any mildly complex JavaScript build, and I don't fully understand how they work together and/or affect other pieces of the build, e.g., Browserify and Babel. I'd like to expand our docs to better articulate these relationships so I finally learn how this all works.

hancush commented 3 years ago

One problem I'm having is that it's really opaque how, if at all, Django Compressor uses .babelrc, etc. Follow up question: Will we ever want in-request transpilation of JavaScript, and if not, why do we favor Django Compressor over a straight up JavaScript build step?

fgregg commented 3 years ago

on demand transpilation seems surprising to me too. i don't know why you wouldn't want those be static assets.

hancush commented 3 years ago

Some thoughts:

hancush commented 3 years ago

Eureka!

hancush commented 3 years ago

Started updates with a default Babel config that will transpile our JavaScript to ES5 compatible syntax and instructions for transpiling problematic dependencies in https://github.com/datamade/how-to/compare/master..hcg/babel-config.

I'd love to find a better solution than guess and check to identify those problem modules. Next time!

hancush commented 3 years ago

I wrote a simple Python CLI that walks a node_modules directory and identifies packages targeting versions of Node that may include ES6 syntax, i.e., break browser compatibility: https://github.com/hancush/python-es6-sniffer

It successfully identified the problematic packages from CA FWD, plus a few others.

I chose this, because switching to Webpack to use the plugin that achieves the same thing seems too heavy for our use case. (One article described Browserify as Lincoln Logs and Webpack as the whole cabin, which I found helpful.)

I'll add guidance on how to use the CLI next time.