Giter Site home page Giter Site logo

mankdev / react-to-html-webpack-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from markdalgleish/react-to-html-webpack-plugin

0.0 2.0 0.0 14 KB

Webpack plugin that renders React components to HTML files

License: MIT License

JavaScript 100.00%

react-to-html-webpack-plugin's Introduction

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

react-to-html-webpack-plugin's People

Contributors

jonathaningram avatar markdalgleish avatar raveclassic avatar

Watchers

 avatar  avatar

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.