GithubHelp home page GithubHelp logo

fimion / html-webpack-injector Goto Github PK

View Code? Open in Web Editor NEW

This project forked from architgarg/html-webpack-injector

0.0 1.0 0.0 20 KB

Plugin that simplifies injection of chunks into head and body using HtmlWebpackPlugin (with ability to provide async/defer)

JavaScript 100.00%

html-webpack-injector's Introduction

HTML Webpack Injector Plugin

Plugin that simplifies injection of chunks into head and body using HtmlWebpackPlugin (with ability to provide async/defer)

Installation

  npm i --save-dev html-webpack-injector
  yarn add --dev html-webpack-injector

This is a webpack plugin that simplifies injection of chunks in head and body tags of HTML files using HtmlWebpackPlugin to serve your webpack bundles. This is especially useful when you want to inject some chunks to head and some chunks to body using HtmlWebpackPlugin.

Usage

Suppose you want have `2 chunks` that you want to inject in the html document using HtmlWebpackPlugin. If you want to inject `one chunk in head` and `one chunk in body` of the same html document.

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInjector = require('html-webpack-injector');

module.exports = {
  entry: {
    index: "./index.ts",
    index_head: "./index.css" // add "_head" at the end to inject in head.
  },
  output: {
    path: "./dist",
    filename: "[name].bundle.js"
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html",
      filename: "./dist/index.html",
      chunks: ["index", "index_head"]
    }),
    new HtmlWebpackInjector()      // Initialize plugin
  ]
}

This will generate a file dist/index.html containing the following

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Archit's App</title>
    <script type="text/javascript" src="index_head.bundle.js"></script></head>
  </head>
  <body>
    <script src="index_bundle.js"></script>
  </body>
</html>

You have to add _head in the entry point chunk name and it will be automatically injected in the head.

Provide Async Defer (Optional)

You can specify scripts to be async or defer by adding additional `chunksConfig` option.
plugins: [
   new HtmlWebpackPlugin({
     template: "./index.html",
     filename: "./dist/index.html",
     chunks: ["index", "index_head"],
     chunksConfig: {             // Added option
       async: ["index_head"],    
       defer: ["index"]
     }
   }),
   new HtmlWebpackInjector()     
 ]

html-webpack-injector's People

Contributors

architgarg avatar thearchitgarg avatar

Watchers

 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.