Giter VIP home page Giter VIP logo

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

React Root Plugin for HTML Webpack Plugin

This is a plugin that extends the functionality of the HTML Webpack Plugin by adding an element to the generated HTML file that React can use to render the app.

Installation

Install the plugin with npm:

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

Basic Use

This adds an element (a div with "root" as the id by default) to the HTML file generated by the HTML Webpack Plugin:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const ReactRootPlugin = require('html-webpack-react-root-plugin');
const webpackConfig = {
  entry: 'index.js',
  output: {
    path: 'build',
    filename: 'bundle.js'
  },
  plugins: [new HtmlWebpackPlugin(), new ReactRootPlugin()]
};

Should add this to your build directory.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="bundle.js"></script>
  </body>
</html>

So you can do this:

import React from 'react';
import { render } from 'react-dom';

const Hello = () => (
  <h1>Hello!</h1>
);

render(
  <Hello />,
  document.getElementById('root')
);

Without adding a template!

Syntax

new ReactRootPlugin()

Creates a <div> with an id of "root."

new ReactRootPlugin(object)

Takes a configuration object with the following values:

  • tagName: the type of element created. Defaults to 'div'.
  • tagId: the ID given to the created tag. Defaults to 'root'.
new ReactRootPlugin(string)

If a string is passed it creates a <div> with the passed string as the ID.

If anything other than a string or an object is passed it will use the default values and issue a warning in the Webpack build report.

Examples

This adds an element (a div with "root" as the id by default) to the HTML file generated by the HTML Webpack Plugin:

Add <div id="app"></div> to the created file.

const webpackConfig = {
  entry: 'index.js',
  output: {
    path: 'build',
    filename: 'bundle.js'
  },
  plugins: [
    new HtmlWebpackPlugin(),
    new ReactRootPlugin('app')
  ]
};

Add <main id="application-root"></main> to the created file.

const webpackConfig = {
  entry: 'index.js',
  output: {
    path: 'build',
    filename: 'bundle.js'
  },
  plugins: [
    new HtmlWebpackPlugin(),
    new ReactRootPlugin({ tagName: 'main', tagId: 'application-root' })
  ]
};

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

Contributors

aaron-j-krause avatar krithix avatar

Watchers

 avatar  avatar

Forkers

lukedeandev

html-webpack-react-root-plugin's Issues

Plugin could not be registered at 'html-webpack-plugin-before-html-processing'. Hook was not found.

environment:

        "clean-webpack-plugin": "^3.0.0",
        "css-loader": "^4.2.1",
        "html-webpack-plugin": "^4.3.0",
        "style-loader": "^1.2.1",
        "webpack": "^4.44.1",
        "webpack-cli": "^3.3.12",
        "webpack-dev-server": "^3.11.0"

got error:

/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/Tapable.js:69
                throw new Error(
                ^

Error: Plugin could not be registered at 'html-webpack-plugin-before-html-processing'. Hook was not found.
BREAKING CHANGE: There need to exist a hook at 'this.hooks'. To create a compatibility layer for this hook, hook into 'this._pluginCompat'.
    at Compilation.plugin (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/Tapable.js:69:9)
    at Compilation.deprecated [as plugin] (internal/util.js:70:15)
    at /mnt/d/codes/practice/amis-demo/node_modules/html-webpack-react-root-plugin/lib/plugin.js:30:19
    at SyncHook.eval [as call] (eval at create (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:1)
    at SyncHook.lazyCompileHook (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.newCompilation (/mnt/d/codes/practice/amis-demo/node_modules/webpack/lib/Compiler.js:630:30)
    at /mnt/d/codes/practice/amis-demo/node_modules/webpack/lib/Compiler.js:667:29
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.compile (/mnt/d/codes/practice/amis-demo/node_modules/webpack/lib/Compiler.js:662:28)
    at /mnt/d/codes/practice/amis-demo/node_modules/webpack/lib/Compiler.js:321:11
    at Compiler.readRecords (/mnt/d/codes/practice/amis-demo/node_modules/webpack/lib/Compiler.js:529:11)
    at /mnt/d/codes/practice/amis-demo/node_modules/webpack/lib/Compiler.js:318:10
    at eval (eval at create (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at /mnt/d/codes/practice/amis-demo/node_modules/webpack/lib/CachePlugin.js:50:13
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:7:1)
    at AsyncSeriesHook.lazyCompileHook (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/Hook.js:154:20)
    at /mnt/d/codes/practice/amis-demo/node_modules/webpack/lib/Compiler.js:315:19
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
    at AsyncSeriesHook.lazyCompileHook (/mnt/d/codes/practice/amis-demo/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.run (/mnt/d/codes/practice/amis-demo/node_modules/webpack/lib/Compiler.js:312:24)
    at processOptions (/mnt/d/codes/practice/amis-demo/node_modules/webpack-cli/bin/cli.js:353:14)
    at /mnt/d/codes/practice/amis-demo/node_modules/webpack-cli/bin/cli.js:364:3
    at Object.parse (/mnt/d/codes/practice/amis-demo/node_modules/yargs/yargs.js:576:18)
    at /mnt/d/codes/practice/amis-demo/node_modules/webpack-cli/bin/cli.js:49:8
    at Object.<anonymous> (/mnt/d/codes/practice/amis-demo/node_modules/webpack-cli/bin/cli.js:366:3)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)

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.