Giter VIP home page Giter VIP logo

html-webpack-root-plugin's Introduction

HTML Webpack Root 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 (or other frameworks) can use to render the app.

Installation

Install the plugin with npm:

$ npm install html-webpack-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 HtmlWebpackRootPlugin = require('html-webpack-root-plugin');
const webpackConfig = {
  entry: 'index.js',
  output: {
    path: 'build',
    filename: 'bundle.js'
  },
  plugins: [new HtmlWebpackPlugin(), new HtmlWebpackRootPlugin()]
};

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 HtmlWebpackRootPlugin()

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

new HtmlWebpackRootPlugin(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 HtmlWebpackRootPlugin(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 HtmlWebpackRootPlugin('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 HtmlWebpackRootPlugin({ tagName: 'main', tagId: 'application-root' })
  ]
};

Notes

Plugin based on html-webpack-react-root-plugin by @aaronjkrause and updated for Webpack 4.

html-webpack-root-plugin's People

Contributors

octalmage avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

html-webpack-root-plugin's Issues

Add support for SSR

It would seem to be pretty straightforward to add SSR support: add an option which takes a function, and inserts the returned value to the root tag. The user is free to implement the actual SSR however they see fit.

Thoughts?

"TypeError: Cannot read property 'tapAsync' of undefined" after update to html-webpack-plugin v4 from v3

/home/srghma/projects/purescript-halogen-mdl/node_modules/html-webpack-root-plugin/lib/plugin.js:30
      compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(
                                                             ^

TypeError: Cannot read property 'tapAsync' of undefined
    at /home/srghma/projects/purescript-halogen-mdl/node_modules/html-webpack-root-plugin/lib/plugin.js:30:62
    at SyncHook.eval [as call] (eval at create (/home/srghma/projects/purescript-halogen-mdl/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:5:1)
    at SyncHook.lazyCompileHook (/home/srghma/projects/purescript-halogen-mdl/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.newCompilation (/home/srghma/projects/purescript-halogen-mdl/node_modules/webpack/lib/Compiler.js:631:26)
    at /home/srghma/projects/purescript-halogen-mdl/node_modules/webpack/lib/Compiler.js:667:29
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/srghma/projects/purescript-halogen-mdl/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:4:1)
    at AsyncSeriesHook.lazyCompileHook (/home/srghma/projects/purescript-halogen-mdl/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.compile (/home/srghma/projects/purescript-halogen-mdl/node_modules/webpack/lib/Compiler.js:662:28)
    at /home/srghma/projects/purescript-halogen-mdl/node_modules/webpack/lib/Compiler.js:321:11
    at Compiler.readRecords (/home/srghma/projects/purescript-halogen-mdl/node_modules/webpack/lib/Compiler.js:529:11)
    at /home/srghma/projects/purescript-halogen-mdl/node_modules/webpack/lib/Compiler.js:318:10
    at eval (eval at create (/home/srghma/projects/purescript-halogen-mdl/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)
    at /home/srghma/projects/purescript-halogen-mdl/node_modules/webpack/lib/CachePlugin.js:50:13
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/srghma/projects/purescript-halogen-mdl/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:5:1)
    at AsyncSeriesHook.lazyCompileHook (/home/srghma/projects/purescript-halogen-mdl/node_modules/tapable/lib/Hook.js:154:20)
    at /home/srghma/projects/purescript-halogen-mdl/node_modules/webpack/lib/Compiler.js:315:19
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/srghma/projects/purescript-halogen-mdl/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:13:1)
    at AsyncSeriesHook.lazyCompileHook (/home/srghma/projects/purescript-halogen-mdl/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.run (/home/srghma/projects/purescript-halogen-mdl/node_modules/webpack/lib/Compiler.js:312:24)
    at processOptions (/home/srghma/projects/purescript-halogen-mdl/node_modules/webpack-cli/bin/cli.js:353:14)
    at /home/srghma/projects/purescript-halogen-mdl/node_modules/webpack-cli/bin/cli.js:364:3
    at Object.parse (/home/srghma/projects/purescript-halogen-mdl/node_modules/webpack-cli/node_modules/yargs/yargs.js:567:18)
    at /home/srghma/projects/purescript-halogen-mdl/node_modules/webpack-cli/bin/cli.js:49:8
    at Object.<anonymous> (/home/srghma/projects/purescript-halogen-mdl/node_modules/webpack-cli/bin/cli.js:366:3)
    at Module._compile (internal/modules/cjs/loader.js:1128:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:983:32)
    at Function.Module._load (internal/modules/cjs/loader.js:891:14)
    at Module.require (internal/modules/cjs/loader.js:1023:19)
    at require (internal/modules/cjs/helpers.js:72:18)
  "devDependencies": {
    "autoprefixer": "^9.7.4",
    "css-loader": "^3.4.2",
    "html-webpack-plugin": "^4.0.1",
    "html-webpack-root-plugin": "^0.10.0",
    "mini-css-extract-plugin": "^0.9.0",
    "postcss-loader": "^3.0.0",
    "precss": "^4.0.0",
    "purs-loader": "^3.7.1",
    "ramda": "^0.27.0",
    "ramda-adjunct": "^2.26.0",
    "style-loader": "^1.1.3",
    "terser-webpack-plugin": "^2.3.5",
    "webpack": "^4.42.0",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3",
    "webpack-merge": "^4.2.2",
    "workbox-webpack-plugin": "^5.1.1"
  }
  plugins: [
    new HtmlWebpackPlugin({
      title: 'test',
    }),
    new HtmlWebpackRootPlugin(), // if commented - error disappears
  ]

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.