Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    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.

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

    Did you enjoy this article?

    Continue reading about Livecoding #21: Use Babel for libraries, not Webpack

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

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

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    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 ❤️

    Created by Swizec with ❤️