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

repack-middleware

v0.1.24

Published

Express-like middleware to webpack/hot module update modules

Downloads

31

Readme

Repack middleware

Simply way to use React on your apps, with ES6 syntax, and JSX syntax, and hot module replacement, and server side rendering.

Installation & usage

Frist, install the npm module

npm install repack-middleware

Configuration files

Create a webpack config file

module.exports = {
    output: {
        filename: "app.bundle.js",
        publicPath: "/build/"
    }
}

Add the middleware to your app.

var repackMiddleware = require('repack-middleware');

// ... include the middleware before other routes

app.use(repackMiddleware({
	configFile    : __dirname + "/webpack.config.js",
    componentsPath: __dirname + "/components"
}));

Include the bundled script in your html. The middleware will generate the bundle in memory and serve it on the publicPath specified in the webpack.config.js

<script type="text/javascript" src="/build/app.bundle.js"></script>

Usage

Create the main component:

./components/MainComponent.jsx
This is the view that is being called form the route

import React from 'react';

export default class MainComponent extends React.Component {
    render() {
        var myObjects = this.props.myObjects || [];
        var myStyle = {
            border: "solid 1px #0F0"
        };

        return (
            <div style={myStyle}>
                <h1>This could be React</h1>
                {myObjects.map( (obj, idx) => {
                    return <li key={idx}> {obj.size} {obj.name}</li>
                })}
            </div>
        );
    }
}

Now, in your route, call res.renderReact() to generate the insert html where the the react component should be inserted.

/* GET home page. */
router.get('/', function(req, res, next) {
	var myProps = {
        myObjects: [
        	{name: "User 1", size: 11},
        	{name: "User 2", size: 20},
        	{name: "User 3", size: 30}
        ]
    };

    res.renderReact('MainComponent', myProps, function(err, html) {
        res.locals.reactHtml = html;
        res.render('index', {title: "Hello world"} );
    });
});

And in the view (jade)

    != reactHtml

Note the != used to inject the variable in unescaped way.

Testing

  • Start your server
  • Go and modify something in either .jsx file
    Your browser should have received the modifications without reloading

Documentation

Config

Configuration options are passed to the middleware when it is created.

repackMiddleware ( options )

options

| option | default | Description | |------------ |------------|------------------------------------| | configFile |required | The path to your webpack.config.js | | componentsPath |required | The root folder of your components | | productionMode | process.env.NODE_ENV == 'production' | when true disables webpack middleware and hot module replacement, your app must serve a pregenerated bundle | | serverRender | false | enables server side rendering | | elementId | "main" | The id of the div that will be created containing server side rendered html if enabled or empty (the node on which the React root component will be mounted) | | generateEntry | true | generate a .startup.jsx that can initialize any component directly in the componentsPath folder | | fullReload | false | force the browser to reload on changes (util when using material-ui library for example)

Note, if you enable server side rendering, and modify any of the jsx files, the server will continue to render the old component's html output, due to node's require cache system.

renderReact

res.renderReact ( componentName, propsObject, callback )

the propsObject should specify the initial props, these will be inserted in a <script> tag
to initialize the componentName with these props.

Callback signature

callback ( err, html )

This method is injected by the middleware in the res object to generate React html,
from there the html value, which is the rendered react html can be injected into
the view (unescaped, that depends on the view engine):