npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-isomorphic-dev-middleware

v4.1.0

Published

The webpack-dev-middleware, but for isomorphic applications.

Downloads

743

Readme

webpack-isomorphic-dev-middleware

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status Greenkeeper badge

The webpack-dev-middleware, but for isomorphic applications.

Showcase

Installation

$ npm install webpack-isomorphic-dev-middleware --save-dev

The current version works with webpack v2, v3 and v4.
You might get a peer dependency warning when using webpack v2 or v3 but you may ignore it.

Motivation

Building applications powered by webpack with server-side rendering (isomorphic/universal apps) is hard.

When making a production build, you must compile both the client and server. When developing, we want to rebuild the client & server and bring in the new compiled code without restarting/reloading the application. This is complex, especially setting up the development server.

To make your development workflow easier to setup, webpack-isomorphic-dev-middleware offers an express middleware that:

  • Looks for code changes in both the client and the server and automatically compiles them
  • Optimizes compilation by using in-memory filesystem
  • Delays responses until the aggregated compiler finishes
  • Adds isomorphic to res.locals, which includes the webpack stats and the methods exported in your server file
  • Offers beautiful compilation reporting into your terminal
  • Receive status through OS notifications
  • Shows compilation errors in the browser on refresh, similar to the ones you get on the terminal

Usage

const express = require('express');
const webpack = require('webpack');
const webpackIsomorphicDevMiddleware = require('webpack-isomorphic-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');

const clientCompiler = webpack({ /* webpack client config */ });
const serverCompiler = webpack({ /* webpack server config */ });
const app = express();

// Serve any static files from the public folder
app.use('/', express.static('public', { maxAge: 0, etag: false }));
// Add the middleware that will wait for both client and server compilations to be ready
app.use(webpackIsomorphicDevMiddleware(clientCompiler, serverCompiler));
// You may also add webpack-hot-middleware to provide hot module replacement to the client
app.use(webpackHotMiddleware(clientCompiler, { quiet: true }));

// Catch all route to attempt to render our isomorphic app
app.get('*', (req, res, next) => {
    // res.isomorphic contains `compilation` & `exports` properties:
    // - `compilation` contains the webpack-isomorphic-compiler compilation result
    // - `exports` contains the server exports, usually one or more render functions
    const { render } = res.locals.isomorphic.exports;

    render({ req, res })
    .catch((err) => setImmediate(() => next(err)));
});

The middleware function is flexible and supports various signatures:

  • Two separate webpack compilers
const clientCompiler = webpack({ /* webpack client config */ });
const serverCompiler = webpack({ /* webpack server config */ });

app.use(webpackIsomorphicDevMiddleware(clientCompiler, serverCompiler, { /* options */ }));
  • A webpack multi-compiler where the first and second indexes belong to the client and server respectively, see https://webpack.js.org/api/node
const compiler = webpack([
    /* webpack client config */,
    /* webpack server config */,
]);

app.use(webpackIsomorphicDevMiddleware(compiler, { /* options */ }));
const isomorphicCompiler = webpackIsomorphicCompiler(
    /* webpack client config */,
    /* webpack server config */
);

app.use(webpackIsomorphicDevMiddleware(isomorphicCompiler, { /* options */ }));

Available options:

| Name | Description | Type | Default | | ------ | ------------- | -------- | ------- | | memoryFs | Either disable or enable in-memory filesystem (disabling decreases performance) | boolean | true | | watchOptions | Options to pass to webpack's watch | object | | | watchDelay | Delay calling webpack's watch for the given milliseconds | number | 0 | | report | Enables reporting | boolean/object | { stats: 'once' } | notify | Report build status through OS notifications | boolean/object | false | | headers | Headers to be sent when serving compiled files | object | { 'Cache-Control': 'max-age=0, must-revalidate' } | | findServerAssetName | Finds the server asset to require from the Webpack stats | function | first js asset from the first entrypoint |

By default, findServerAsset selects the first JavaScript asset from first entrypoint. If that doesn't suit your Webpack configuration, you may change it:

{
    // Finds the asset with the `server` word in its name
    findServerAssetName: (stats) => stats.assets
        .map((asset) => asset.name)
        .find((name) => /\bserver\b.+\.js$/.test(name));
}

Tests

$ npm test
$ npm test -- --watch during development

License

MIT License