Skip to content
Swizec Teller - a geek with a hatswizec.com

Livecoding #21: Use Babel for libraries, not Webpack

This is a Livecoding Recap – an almost-weekly post about interesting things discovered while livecoding ?. Always under 500 words and with pictures. You can follow my channel, here. New streams almost every Sunday at 2pm PDT. There’s live chat, come say hai ?

It does the thing! We did it! \o/

Sort of … it’s pretty damn buggy. Flickering, wobbling, colors switching place. It’s a really shitty animated piechart when you think about it.

But animated it is, and the flickering and the wobbling and the color changing is no fault of our TransitionableComponent library. The point was to get that working.

Last week, we couldn’t even get the import to work. No matter what we tried, TransitionableComponent was always an object and wouldn’t let us extend. Which makes sense; you extend classes (functions), not objects.

Before today’s stream, I tried a few more things:

  • upgrade node to 4.5 LTS —> nope ??

  • upgrade node to latest 6.4 —> nope ??

  • import from script path directly —> nope ? ?

  • extend TransitionableComponent.constructor —> lol, nope ??

Nothing worked. It was hopeless. The stream was starting, and I was still stuck at the same point where we’d left off the week before. People joined, people dropped.

Desperate, I said “Fuck it, let’s avoid Webpack”. This was a last ditch attempt at salvaging the thing, the stream, and my honor.

Lo and behold: it worked. ?

Of course then a bunch of people in the chat said, "Oh yeah, I totally knew that would work”.

Well then, why the fuck didn’t you say so? …

It’s okay. They helped me figure out exactly how to do the Babel step to make it work. We’re good. :)

If you’re building a library, don’t use Webpack to compile. Use Babel directly. Here’s why:

w4lK3BY

That’s because Webpack will create a single JS file optimized for non-commonjs environments. So there is no require or module.exports with Webpack. Babel will only transpile es2015 to es5, so module.exports and requires will stay, and node can understand them.

Then, when the project uses your library, webpack will resolve and bundle that es5 code.

Thanks ilmattodel93! You’re the best!

I feel like I’ve learned a valuable lesson, even if it did take embarrassingly long to discover. I even found a clue three weeks ago → React source looks like pure ES5 without any Webpack crap.

Now, about that flickering and the wobbling: It happens because we’re animating the d attribute of a path element, like this:

class Path extends TransitionableComponent {
render() {
return (
<path d={this.state.d} style={this.props.style} ref="node">
);
}
}
</path>

The flickering is when d reaches an invalid value. This confuses both D3 and SVG. The wobbling is a result of interpolation between two values of d. I’d paste them here, but they’re hard to read.

This is either a bug in D3 or a fundamental issue with SVG and/or interpolation. A better approach might be to transition startAngle and endAngle attributes hidden deep inside the pie() layout.

We’ll try that next week. After that, I think it’s time to play with react-native and D3.

PS: the edited and improved versions of these videos are becoming a video course. Readers of the engineer package of React+d3js ES6 get the video course for free when it’s ready.

Did you enjoy this article?

Published on August 29th, 2016 in d3js, Front End, Livecoding, Technical

Learned something new?
Want to become a high value JavaScript expert?

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️