Git Product home page Git Product logo

stateful-react-container-webpack-plugin's Introduction

stateful-react-container-webpack-plugin

This Webpack plugin in intended to be used with html-webpack-plugin in projects using React.js. This plugin allows you to use html-webpack-plugin to build templates automatically with Webpack while also server-side rendering and passing initial state to the React.js client-side app.

There may be an example of the usage over at Kit when I've had time to update it. See the Example output to get an idea what this does.

Installation

npm install -S stateful-react-container-webpack-plugin

Setup

After installing the plugin, you will need to add it to the plugins in your Webpack configuration:

plugins: [
  new HtmlWebpackPlugin(),
  new StatefulReactContainerPlugin(),
],

Options

The plugin provides three options (passed as an object argument to the plugin) to customize usage:

  • id (default: container): The id of the React container.js element.
  • attribute (default: state): The name of the data attribute to store the initial state for the React.js app.
  • variable (default: state): The name of the variable containing the initial state in the template.
  • noState (default: false): Omits the data attribute and its value. This is useful if you only want this plugin to generate the container element and are not using server rendering.
  • position (default: 'start'): Determines whether the container is immediately after the opening body tag or immediately before the closing tag. Can be either 'start' or 'end'.

Example output

With the default configuration, the <body> tag is replaced with the following markup:

<body><div id="container" data-state="<%= state %>"></div>

Notes

This plugin assumes that you're using ejs to render your templates on the server side (hence the <%= state %> in the output). Using Express.js, you might render the built template as follows:

// in a request handler
const state = JSON.stringify({
  user: req.session.user,
});
res.render('app.ejs', { state });

stateful-react-container-webpack-plugin's People

Contributors

hph avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

stateful-react-container-webpack-plugin's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.