Broccoli Middleware

2.1.1 · abandoned · verified Wed Apr 22

broccoli-middleware is a utility that integrates the Broccoli asset pipeline's build output into standard Node.js HTTP servers, typically for development environments. It allows applications, especially those built with Ember CLI, to serve dynamically built assets through middleware like Express. The package is currently at version 2.1.1, with its last publication dating back to 2019. Its development is effectively abandoned, with no recent releases or active maintenance. It differentiates itself by providing a direct bridge between the Broccoli build process and a web server's request-response cycle, allowing for on-the-fly serving of compiled assets without a separate build step before server startup. This contrasts with build tools that pre-build all assets to a /dist folder.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `broccoli-middleware` with an Express server to serve assets from a simulated Broccoli build output directory. It shows basic integration for `app.js` and `styles.css`.

const express = require('express');
const path = require('path');
const fs = require('fs');
const broccoliMiddleware = require('broccoli-middleware');

// Simulate a Broccoli builder output structure
const outputDir = path.join(__dirname, 'tmp/broccoli-output');
fs.mkdirSync(outputDir, { recursive: true });
fs.writeFileSync(path.join(outputDir, 'app.js'), 'console.log("Hello from Broccoli!");');
fs.writeFileSync(path.join(outputDir, 'styles.css'), 'body { background-color: lightblue; }');

const app = express();

// In a real Ember CLI app, `watcher` and `builder` would be provided by ember-cli.
// For this example, we'll mock the 'builder' interface to return a fixed directory.
const mockBuilder = {
  // The `builder.outputPath` is what broccoli-middleware expects.
  outputPath: outputDir
};

app.use(broccoliMiddleware(mockBuilder));

app.get('/', (req, res) => {
  res.send(`
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Broccoli Middleware Example</title>
        <link rel="stylesheet" href="/styles.css">
    </head>
    <body>
        <h1>Welcome to Broccoli Middleware!</h1>
        <script src="/app.js"></script>
        <p>Assets served dynamically from Broccoli's output.</p>
    </body>
    </html>
  `);
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server listening on http://localhost:${PORT}`);
  console.log('Try visiting http://localhost:3000/app.js and http://localhost:3000/styles.css');
  console.log('Note: In a real scenario, mockBuilder would be a live Broccoli builder producing actual changes.');
});

// Cleanup on exit (optional for a quickstart, but good practice)
process.on('exit', () => {
  try { fs.rmSync(path.join(__dirname, 'tmp'), { recursive: true, force: true }); } catch (e) {}
});

view raw JSON →