This is a Livecoding Recap – an almost-weekly post about interesting things discovered while livecoding. Usually shorter than 500 words. Often with pictures. Livecoding happens almost every Sunday at 2pm PDT on multiple channels. You should subscribe to My Youtube channel to catch me live.
This article builds on my previous articles about Building an interactive DOM benchmark and Adding Preact to a React project
I understand why people like Vue, and those people are wrong.
But this isn't a rant about Vue. If you like the ease of porting your 2012-era code to a modern framework, Vue is for you. If you're into Mustache templates, Angular directives, and love to split your code into templates, scripts, and styles just like ye olden times, Vue is perfect. Hell, if you love nothing more than to use cumbersome workarounds to avoid JavaScript classes 👉 Vue.
You do you. I built a Vue component and hated every minute of it. But it's important to try new things.
It took about 2 hours to add Vue to my interactive DOM benchmark. Half the time I spent bitching about the Angular 1 style templates. The other half, I complained about 2012-era hacks designed to fake classes.
Oh, and it took an hour just to get it compiling.
Getting started with Vue is easy they said, you'll have fun they said.
— Swizec Teller writing a book on software rewrites (@Swizec) March 4, 2018
vue-loader installs vue-template-es2015-compiler then complains it can't find vue-template-compiler pic.twitter.com/m0w4nmp5Tf
A wonderful experience all over 👌
BUT! I created my first non-trivial Vue component, a simple benchmark 🎉
My first Vue component
You can see the code on GitHub. There are 5 buttons and a scratchpad area that renders a list of immutable nodes. Much like a chatroom or a newsfeed.
Buttons can prepend
, insert
, append
, drop
, and remove
nodes. Clicks map to methods on the Vue component that manipulate a nodes
array in data. The template renders it as a bunch of divs.
<div class="row benchmark-scratchpad"><div v-for="k in nodes">{{ k }}</div></div>
That v-for="k in nodes"
syntax is one of my least favorite parts of Vue. Brings back memories of some Angular 1 projects I worked on. Nightmares lasted months1.
Click events call instance methods via v-on:click="func"
. They manipulate the nodes
array and that triggers a re-render. These methods look a lot like to their React counterparts, but cleaner because there's no setState
in Vue. I liked that 🤫
prepend() {this.nodes = [...this.newNodes(), ...this.nodes];},append() {this.nodes = [...this.nodes, ...this.newNodes()];},
I think that looks more readable and makes more sense than going through setState
in React.
However, you have to define instance attributes in a data:
key and instance methods in a methods:
key. But you override Vue's internal instance methods in the normal namespace. 🤨
module.exports = {// adding attributes here does nothingdata: function () {// this is where you define statereturn {nodes: [],start: null,times: [],// ...};},// override Vue instance methods heremethods: {// define custom instance methods here},};
Wat 🤨
That's kind of how we used to write code in the old days before JavaScript got better. Did I fall into a time machine?
I've heard you can install plugins and tweak configs to get a better approach going, but if it's not the default in the official docs, it doesn't exist. Ask anyone.
Some people like this, so here we are. It was a fun experiment, I don't like it at all. ¯\_(ツ)_/¯
Oh btw we render benchmark results using Vue refs and direct DOM manipulation so as to avoid re-rendering via Vue after each render. That would be unfair.
Adding Vue to a nwb-cli based project
Getting Vue to compile at all was fun, too. I started the project using nwb-cli, which is not The Official Way™. You're supposed to use vue-cli
, but I didn't want to start a new project.
Some finagling with vue-loader
was necessary. Two steps 👇
- install some stuff
- tweak
nwb.config.js
.
Install some stuff
yarn add vue vue-loader vue-template-compiler
You need both vue-loader
and vue-template-compiler
because vue-loader actually installs vue-template-es2015-compiler
then complains that it can't find vue-template-compiler
. This is confusing.
I installed vuera as well. It gives you a wrapper component so you can render Vue inside your React project. Vue is kept internally autonomous so it shouldn't affect the benchmark, but it makes it easier to put all benchmarks in the same project.
// VueBenchmarkWrapper.jsimport React from "react";import { VueWrapper } from "vuera";import VueBenchmark from "./VueBenchmark.vue";export default () => <vuewrapper component={VueBenchmark}>;</vuewrapper>;
Tweak nwb.config.js
After installing Vue, you have to tell Webpack how to use it. That goes in nwb.config.js
.
// nwb.config.jsmodule.exports = {type: "react-app",webpack: {extra: {module: {rules: [{test: /\.vue$/,loader: "vue-loader",},],},},},};
You add a rule that tells Webpack to use vue-loader
for files ending with .vue
. Same as a normal Webpack config file, just wrapped in extra:
so nwb
knows what to do.
This is where nwb
shines over create-react-app
. Extra config without eject.
Next step for the DOM benchmark
What I want to do next is going to blow your mind. Maybe.
Remember my blockchain-redux experiment from December? I want to use it to make this DOM benchmark collaborative.
Every time you click a button, it adds to the benchmark blockchain, distributes it to everyone, updates graphs on everyone's page. We're going to see how these benchmarks behave in the real world on real people's computers.
That's gonna be fun 🤘
- This is a dramatization. There were only cold sweats every time I opened the editor to get started. ↩
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open 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. 👌"
Senior Mindset Book
Get promoted, earn a bigger salary, work for top companies
Learn more20% off this week
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 ❤️