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

koa-parcel-middleware

v1.0.3

Published

parcel bundler koa middleware

Downloads

10

Readme

koa-parcel-middleware

parcel middleware for koa

JavaScript Style Guide semantic-release Greenkeeper badge CircleCI TypeScript package

why

parcel middleware enables you to:

  • wire in advanced features, such as server-side-rendering. isomorphic js in compact form
  • serve your ui application from your server application
  • combine the parcel dev server functionality with an existing server application, rather than an extra process

install

yarn add koa-parcel-middleware koa koa-static

koa and koa-static are required peerDependencies. koa-static is required such that non-js assets (e.g. css, images, etc) may be served gracefully as requested by your ui.

usage

import { createMiddleware } from 'koa-parcel-middleware'
const middleware = createMiddleware({
  bundler: `parcelBundlerInstance`,
  renderHtmlMiddleware?: `<optionally-own-serving-your-entrypoint>`,
  staticMiddleware: `koaStaticInstance` // serving parcel's built assets
})

the following is a rich, complete example of using the middleware api.

import { createMiddleware } from 'koa-parcel-middleware' // :)
import { App } from './app' // e.g. a react <App /> component
import { promises as fs } from 'fs'
import * as path from 'path'
import * as ReactDOMServer from 'react-dom/server'
import Bundler from 'parcel-bundler'
import CombinedStream from 'combined-stream'
import Koa from 'koa'
import serveStatic from 'koa-static'

// your parcel application's _unbuilt_ entry point!
const ENTRY_FILENAME = path.resolve(__dirname, 'index.html')
const isDev = process.env.NODE_ENV === 'development'

async function start () {
  const app = new Koa()
  // your parcel application's _built_ entry point!
  const outFile = path.resolve(__dirname, 'dist', 'index.html')
  const outDir = path.resolve(__dirname, 'dist')
  const options = {
    outDir,
    outFile,
    watch: isDev,
    minify: !isDev,
    scopeHoist: false,
    hmr: isDev,
    detailedReport: isDev
  }
  const bundler = new Bundler(ENTRY_FILENAME, options)
  bundler.bundle()
  const staticMiddleware = serveStatic(outDir)
  const parcelMiddleware = createMiddleware({
    bundler,
    renderHtmlMiddleware: async (ctx, next) => {
      // optionally wire in SSR!

      // index.html
      //
      // <html>
      //   <div id="app"><!-- ssr-content --></div>
      //   <script src="app.tsx"></script>
      // </html>
      const outFileBuffer = await fs.readFile(outFile)
      const [preAppEntry, postAppEntry] = outFileBuffer.toString()
        .split(/<!--.*ssr.*-->/)
      ctx.status = 200
      const htmlStream = new CombinedStream()
      ;[
        preAppEntry,
        ReactDOMServer.renderToNodeStream(App()),
        postAppEntry
      ].map(content => htmlStream.append(content))
      ctx.body = htmlStream
      ctx.type = 'html'
      await next()
    },
    staticMiddleware
  })
  app.use((ctx, next) => parcelMiddleware(ctx, next))
  app.listen(3000)
}
start()