• Stars
    star
    166
  • Rank 219,820 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Webpack plugin that renders React components to HTML files

DEPRECATED - Use static-site-generator-webpack-plugin instead.

npm Dependency Status

React-to-HTML Webpack Plugin

Webpack plugin that renders React components to HTML files.

Components are rendered after all source files have been compiled, so JSX works without any issues.

Warning! This plugin executes your code in a Node context after each compilation.

Install

$ npm install --save-dev react-to-html-webpack-plugin

Basic Usage

This basic example assumes that the React component renders <html> as the root element. This works for simple cases, but if you need more fine grained control of the entire document, use the template option instead.

webpack.config.js

var ReactToHtmlPlugin = require('react-to-html-webpack-plugin');

module.exports = {

  entry: './index.jsx',

  output: {
    filename: 'index.js',
    path: 'dist',
    /* IMPORTANT!
     * You must compile to UMD or CommonJS
     * so it can be required in a Node context: */
    library: 'MyComponentExample',
    libraryTarget: 'umd'
  },

  module: {
    loaders: [
      { test: /\.jsx$/, loader: 'jsx-loader' }
    ]
  },

  plugins: [
    new ReactToHtmlPlugin('index.html', 'index.js')
  ]

};

index.jsx

var React = require('react');
var MyComponent = require('./MyComponent.jsx');

if (typeof document !== 'undefined') {
  React.render(<MyComponent />, document);
}

/* IMPORTANT!
 * You must export a component: */
module.exports = MyComponent;

Using a hashed source file

webpack.config.js

var ReactToHtmlPlugin = require('react-to-html-webpack-plugin');

module.exports = {

  entry: {
    main: './index.js'
  },

  output: {
    filename: 'assets/[hash].js',
    path: 'dist',
    /* IMPORTANT!
     * You must compile to UMD or CommonJS
     * so it can be required in a Node context: */
    library: 'MyComponentExample',
    libraryTarget: 'umd'
  },

  module: {
    loaders: [
      { test: /\.jsx$/, loader: 'jsx-loader' }
    ]
  },

  plugins: [
    // Note: "index.js" is not used, instead use "main" which is the name of the entry
    // Using "index.js" would result in a file not found error because it has been hashed
    new ReactToHtmlPlugin('index.html', 'main')
  ]

};

API

new ReactToHtmlPlugin('index.html', 'index.js', { options... });

Options

template (function)

You can optionally provide a function that returns an HTML string.

The template is called with the following data:

{
  html: '...',
  assets: {
    chunkName: assetPath,
    ...
  }
}

For example:

var ejs = require('ejs'); // or whatever you like ;)

...

new ReactToHtmlPlugin('index.html', 'index.js', {
  template: function(data) {
    return ejs.render(`
      <html>
        ...
        <body>
          <div id="app">
            <%- html %>
          </div>
          <% for (var chunk in assets) { -%>
          <script src="<%= assets[chunk] %>"></script>
          <% } -%>
        </body>
      </html>
    `, data);
  }
});

static (boolean)

Optionally enable usage of renderToStaticMarkup. This is recommended when you don't plan to run React on the client.

License

MIT License

More Repositories

1

stellar.js

Stellar.js - Parallax scrolling made easy
JavaScript
4,642
star
2

static-site-generator-webpack-plugin

Minimal, unopinionated static site generator powered by webpack
JavaScript
1,608
star
3

redial

Universal data fetching and route lifecycle management for React etc.
JavaScript
1,104
star
4

react-themeable

Utility for making React components easily themeable
JavaScript
550
star
5

fathom

Fathom.js - Present JavaScript in its native environment.
JavaScript
527
star
6

redux-analytics

Analytics middleware for Redux
JavaScript
489
star
7

postcss-local-scope-example

Example usage of postcss-local-scope
JavaScript
136
star
8

presentation-build-wars-gulp-vs-grunt

CSS
77
star
9

tmpload

Asynchronous Template Loading and Caching for jQuery Templates
JavaScript
62
star
10

redux-tap

Simple side-effect middleware for Redux
JavaScript
58
star
11

remix-vanilla-extract-prototype

TypeScript
53
star
12

serviceworker-loader

ServiceWorker loader for Webpack
JavaScript
52
star
13

gulp-coveralls

Gulp plugin to send code coverage to Coveralls
JavaScript
45
star
14

web-app-manifest-loader

Web app manifest loader for webpack
JavaScript
41
star
15

isolated-scroll

Prevent scoll events from bubbling up to parent elements
HTML
40
star
16

nextjs-vanilla-extract-example

TypeScript
36
star
17

grunt-micro

Ensure your micro-framework stays micro
JavaScript
35
star
18

react-isolated-scroll

React component for isolated-scroll
JavaScript
34
star
19

figma-leading-trim

Figma plugin for trimming space above and below text elements
TypeScript
29
star
20

react-static-site-playground

WIP
JavaScript
26
star
21

jquery-html5data

$.html5data
JavaScript
22
star
22

presentation-a-state-of-change-object-observe

JavaScript
22
star
23

css-modules-in-js-demo

Working demo of using JavaScript as a CSS Modules preprocessor
JavaScript
21
star
24

astro-vanilla-extract-demo

Basic demo of Astro and vanilla-extract
Astro
18
star
25

Eventralize

jQuery Events for Object Oriented JavaScript
JavaScript
18
star
26

presentation-the-case-for-css-modules

HTML
15
star
27

node-lanyrd-scraper

Lanyrd event scraper for Node.js
JavaScript
14
star
28

allthethings.js

Let your array iterations read like actual sentences
JavaScript
11
star
29

fifteen-kilos-webpack-plugin

Bring the magic of fifteen-kilos to your entire project
HTML
11
star
30

instrument-methods

Simple object method instrumentation
JavaScript
11
star
31

react-fifteen-kilos

Bring the magic of fifteen-kilos to your entire React application
JavaScript
10
star
32

react-progressive-component-starter

WIP
JavaScript
9
star
33

presentation-a-unified-styling-language

HTML
8
star
34

reconomise

Crowd sourced local business support network and priority ranking system. 'Best Use of SAPI' at RHoK Melbourne June '12.
JavaScript
8
star
35

react-themeable-experiment

WIP
JavaScript
7
star
36

bespoke-remote-prototype

Prototype presentation for bespoke-remote
JavaScript
7
star
37

presentation-the-end-of-global-css

HTML
6
star
38

angular-delegator

Write smaller, cleaner AngularJS services
JavaScript
6
star
39

presentation-bespoke.js

DIY Presentations with Bespoke.js
JavaScript
5
star
40

batsignal

Help radiator for remote agile teams. Built using Meteor.
JavaScript
5
star
41

presentation-return-of-the-progressive-web

The Return of the Progressive Web
HTML
5
star
42

redux-hotjar

Declarative Hotjar tagging for Redux
JavaScript
4
star
43

stellar.js-site

Stellar.js Project Page
JavaScript
4
star
44

webpack-serviceworker-demo

Webpack Service Worker demo
JavaScript
4
star
45

presentation-first-class-styles

HTML
4
star
46

indiana

Experimental isomorphic JavaScript demo
JavaScript
3
star
47

bespoke-webpack-boilerplate

Bespoke.js boilerplate powered by Webpack
JavaScript
3
star
48

markdalgleish.com

My personal blog - built with Octopress
JavaScript
3
star
49

remix-vite-css-url-demo

Demo of Remix + Vite + .css?url
JavaScript
3
star
50

stateless-dtm

Purely stateless interface for Dynamic Tag Manager
JavaScript
3
star
51

twitface

Twitter avatar API client for Node.js
JavaScript
2
star
52

chook-jstestdriver

JsTestDriver adapter for Chook, the headless, framework-agnostic unit test runner for Node.js
JavaScript
2
star
53

chook

Headless, framework-agnostic unit test runner for Node.js
JavaScript
2
star
54

css-hooks-playground

Just playing around with CSS Hooks
TypeScript
2
star
55

remix-vite-cloudflare-workers-playground

Demo app using Remix with Vite on Cloudflare Workers
TypeScript
1
star
56

postcss-local-scope-inheritance-example

Example usage of postcss-local-scope with class inheritance
JavaScript
1
star
57

redux-helloworld

Just playing around with Redux
JavaScript
1
star
58

zenhub

Placeholder repo for my Zenhub mega-board
1
star
59

css-modules-selector-experiment

Exploration of non-class selectors in CSS Modules
JavaScript
1
star
60

cuecard

iPad-controlled presentation framework for Node.js
JavaScript
1
star
61

tmpload-site

tmpload Project Page
JavaScript
1
star
62

Eventralize-site

Eventralize Project Page
JavaScript
1
star
63

presentation-dawn-of-the-progressive-single-page-app

HTML
1
star
64

fathom-site

Fathom.js Project Page
JavaScript
1
star
65

cuecard-example

Example usage of Cuecard
JavaScript
1
star
66

kansas

Experimental stereoscopic rendering for React components
JavaScript
1
star
67

ducks-component-experiment

Experimental React component powered by Ducks
JavaScript
1
star
68

bespoke.js-site

Bespoke.js Project Page
CSS
1
star
69

preconstruct-node-esm-import-issue

JavaScript
1
star
70

presentation-a-bespoke-ecosystem

JavaScript
1
star
71

presentation-tabs

Ryan loses
CSS
1
star
72

baltic

Experimental continuous deployment with Codeship for Bespoke.js
JavaScript
1
star
73

presentation-web-components

Web Components: Why You're Already An Expert
JavaScript
1
star
74

bespoke-theme-prototype

CSS
1
star
75

angular-lisa-example

Sample app wiring AngularJS and Lisa together
JavaScript
1
star
76

balance-parens

Balance a string of parentheses.
JavaScript
1
star
77

chook-zombie

Experimental Zombie support for Chook
JavaScript
1
star
78

jquery-html5data-site

$.html5data Project Page
JavaScript
1
star
79

walletconnnect-deeplink-prompt-issue

TypeScript
1
star
80

melbcss-modules-hello-world

A simple CSS Modules example, extracted from a live coding demo at MelbCSS
JavaScript
1
star