Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

NotificationsYou must be signed in to change notification settings

architgarg/html-webpack-injector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plugin that simplifies injection of chunks intohead andbody usingHtmlWebpackPlugin (with ability to provide async/defer)

Installation

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

This is awebpack plugin that simplifies injection of chunks inhead andbody tags of HTML files usingHtmlWebpackPlugin to serve yourwebpack 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

constHtmlWebpackPlugin=require('html-webpack-plugin');constHtmlWebpackInjector=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:[newHtmlWebpackPlugin({template:"./index.html",filename:"./dist/index.html",chunks:["index","index_head"]}),newHtmlWebpackInjector()// Initialize plugin]}

This will generate a filedist/index.html containing the following

<!DOCTYPE html><html><head><metacharset="utf-8"><title>Archit's App</title><scripttype="text/javascript"src="index_head.bundle.js"></script></head></head><body><scriptsrc="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:[newHtmlWebpackPlugin({template:"./index.html",filename:"./dist/index.html",chunks:["index","index_head"],chunksConfig:{// Added optionasync:["index_head"],defer:["index"]}}),newHtmlWebpackInjector()]

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp