Node module reloading

Working with node servers is part of the job. While iterating on such servers, it’s common to go for a solution involving automatic restarts using something like nodemon.

npx nodemon --exec "npm start" --watch src

This will automatically restart the server whenever there are changes to the src folder. But I think we can do better than that!

The restart can be expensive, depending on the kinds of initialization that’s involved. What if we just make node reevaluate only the parts that changed?

Read more

Posted in Programming - 0 Comments

Modernizing a webpack/babel config w/ esm treeshaking

Hearing so much about treeshaking in webpack, (and rollup, and parcel), it would seem reasonable that it’d be a well documented type of migration.

The webpack docs have a section on tree shaking, where it looks alluringly simple. We just have to use import { thing } from 'lib'; everywhere, and reap the blessings of the webpack gods.

If, however, we are using plugins like babel in our webpack configuration, (and honestly, who doesn’t?) there are several things that can prevent the tree shaking from working.

TLDR: After a couple days of trying, failing and trying again, the traps seem to be:

  • We now want to transpile everything. To process those sweet new esm bundles.
  • Don’t transpile module types, i.e. make sure babel doesn’t change esm to commonjs.
  • Don’t inject imports via transforms, e.g. polyfills, since it messes with module type detection.
  • Don’t mix esm import and commonjs module.exports, they are incompatible.
Read more

Posted in Programming Webpack - 0 Comments

Package sanity checks

There are a lot of ways in which developing packages for node can be confusing, especially in an environment where not every contributor is an avid npm package maintainer.

Some common mistakes are easily prevented, and avoiding them will save much time and frustration.

  • Is this the correct version of node for this project?
  • Are the dependencies installed correctly, with the right versions?

These issues can be solved once and for all with some npm / shell magic.

Read more

Posted in Programming Node.js - 0 Comments

Notes on returning to Angular 1

This post is written for the purpose of covering some gotchas and observations when going back to using Angular 1.x. For reference, we’ll use react components and lifecycles.

This might seem backwards, but has come up when returning to a large legacy codebase.

Read more

Posted in Programming Angular - 0 Comments

Express HMR with webpack

Update: Don’t do this unless you really need to run code transforms on your server side code. Use Node module reloading, to swap without the overhead from webpack.

Finally got around to setting up a working express hot module reloading config.

When, a year ago, I started replacing a custom Module implementation in our express servers with plain node modules in the name of simplicity, my colleagues started complaining about no longer having server side reloading. But with this, developing on the server is back to its former glory!

The purpose is to have only part of an express server work in this way, so that a module being required can update itself in place.

We’ll focus on getting a single module, and all it’s subdependencies to reload during development. Specifically, how to dynamically discover express.Router modules and mount them with reloading on an existing “normal” express server.

If you’re looking for a way to run your entire server through webpack, take a look at Hot reload all the things!

Read more

Posted in Programming Webpack - 0 Comments