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-http-server

v0.5.0

Published

On-demand runtime webpack compilations over HTTP

Downloads

666

Readme

webpack-http-server

On-demand runtime webpack compilations over HTTP.

Overview

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/compilation
Content-Type: application/json

{"entry":"/Users/you/some/module.js"}

The server will respond you with the compilation info:

{
  "id": "COMPILATION_ID",
  "previewUrl": "http://127.0.0.1/compilation/"
}

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:

  1. 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.
  2. 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.
  3. 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.

HTTP request

fetch(`${server.serverUrl}/compilation`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    entry: '/Users/octocat/projects/demo/index.js',
  }),
})

Node.js API

const result = await server.compile(['/Users/octocat/projects/demo/index.js'])
console.log('preview is running on "%s"', result.previewUrl)