This package is, effectively, an Express server that exposes webpack compilations over HTTP. This means you can request to compile a module on runtime by dispatching a request like this:
POST http://127.0.0.1:8080/compilationContent-Type: application/json{"entry":"/Users/you/some/module.js"}
The server will respond you with the compilation info:
Each compilation request creates a unique compilation ID and /compilations/:id route to preview the runtime of the given module after it's compiled.
Motivation
I've build this library because I'm a fan of example-driven testing. When I employ it on bigger projects, it means that every test I have has a runtime usage example alongside it. While I could compile those examples before tests, I chose to have a runtime compilation server instead. Here are my reasons:
Compilation server guarantees up-to-date examples. I may forget to run npm run build:examples before running tests, which will yield irrelevant test results.
Compilation server is more performant. By using the server and compiling only those examples that my current test run needs, I can save resources by skipping irrelevant examples.
Compilation server creates a runtime. Even if I build examples before tests, I still need something to serve them. This server does that as well via compilation previews.
Why not webpack-dev-server?
Webpack Dev Server is a great tool to run your compilation over HTTP. However, it's scoped to a single compilation. You cannot change the entrypoint for your dev server without having to stop it, modify the webpack config, and re-run the server. This is extremely time consuming and unreliable.
Getting started
Install
npm install webpack-http-server
Create a server
import { WebpackHttpServer } from 'webpack-http-server'const server = new WebpackHttpServer()await server.listen()console.log('Compilation server listening at "%s"', server.serverUrl)
The compilation server runs on a random vacant port. Rely on the serverUrl property to get its full address.
Request a compilation
There are two ways to request a compilation: HTTP request and Node.js API.