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

Add a JavaScript or CSS asset to the HTML generated by html-webpack-plugin

License

NotificationsYou must be signed in to change notification settings

SimenB/add-asset-html-webpack-plugin

Repository files navigation

Add a JavaScript or CSS asset to the HTML generated byhtml-webpack-plugin

NPM VersionCI Build StatusCode Coverage branch

Installation

Install the plugin withnpm:

$ npm i add-asset-html-webpack-plugin -D

NOTE: This plugin requireshtml-webpack-plugin@^3,html-webpack-plugin@^4,orhtml-webpack-plugin@^5.

Basic Usage

The plugin will add the given JS or CSS file to the files Webpack knows about,and put it into the list of assetshtml-webpack-plugin injects into thegenerated html. Add the plugin to your config, providing it a filepath:

constHtmlWebpackPlugin=require('html-webpack-plugin');constAddAssetHtmlPlugin=require('add-asset-html-webpack-plugin');constwebpackConfig={entry:'index.js',output:{path:'dist',filename:'index_bundle.js',},plugins:[newHtmlWebpackPlugin(),newAddAssetHtmlPlugin({filepath:require.resolve('./some-file')}),],};

This will add a script tag to the HTML generated byhtml-webpack-plugin, andlook like:

<!DOCTYPE html><html><head><metacharset="UTF-8"/><title>Webpack App</title></head><body><scriptsrc="index_bundle.js"></script><scriptsrc="some-file.js"></script></body></html>

NOTE: You can also pass an array of assets to be added. Same API as mentionedbelow, just pass multiple objects as an array.

newAddAssetHtmlPlugin([{filepath:require.resolve('./some-file')},{filepath:require.resolve('./some-other-file')},// Glob to match all of the dll file, make sure to use forward slashes on Windows{glob:require.resolve('./**/*.dll.js')},]);

Options

Options are passed to the plugin during instantiation.

newAddAssetHtmlPlugin({filepath:require.resolve('./some-file')});

filepath

Type:string, mandatory unlessglob is defined

The absolute path of the file you want to add to the compilation, and resultingHTML file.

glob

Type:string, mandatory unlessfilepath is defined

A glob used to locate files to add to the compilation. Seeglobby's docs for how to use it.

files

Type:string|Array<string>, default `[]

Files that the assets will be added to.

By default the assets will be included in all files. If files are defined, theassets will only be included in specified file globs.

hash

Type:boolean, default:false

Iftrue, will append a unique hash of the file to the filename. This is usefulfor cache busting.

includeRelatedFiles

Type:boolean, default:true

Iftrue, will addfilepath + '.*' to the compilation as well. E.gfilepath.map andfilepath.gz.

outputPath

Type:string

If set, will be used as the output directory of the file.

publicPath

Type:string

If set, will be used as the public path of the script or link tag.

typeOfAsset

Type:string, default:js

Can be set tocss to create alink-tag instead of ascript-tag.

attributes

Type:object, default:{}

Extra attributes to be added to the generated tag. Useful to for instance addnomodule to a polyfill script. Theattributes object uses the key as thename of the attribute, and the value as the value of it. If value is simplytrue no value will be added.

An example of this is included in the repository.

Currently only supports script tags.

Examples

When adding assets, it's added to the start of the array, so whenhtml-webpack-plugin injects the assets, it's before other assets. If youdepend on some order for the assets beyond that, and ordering the pluginsdoesn't cut it, you'll have to create a custom template and add the tagsyourself.

Add a DLL file fromwebpack.DllPlugin

Note: Remember to build the DLL file in a separate build.

Seeexample for an example of how to set it up. You can run itby cloning this repo, runningyarn followed byyarn run example.

Webpack config

constpath=require('path');constwebpack=require('webpack');constwebpackConfig={entry:{vendor:['react','redux','react-router'],},devtool:'#source-map',output:{path:path.join(__dirname,'build'),filename:'[name].dll.js',library:'[name]_[hash]',},plugins:[newwebpack.DllPlugin({path:path.join(__dirname,'build','[name]-manifest.json'),name:'[name]_[hash]',}),],};

Your main build:

constpath=require('path');constwebpack=require('webpack');constHtmlWebpackPlugin=require('html-webpack-plugin');constAddAssetHtmlPlugin=require('add-asset-html-webpack-plugin');constwebpackConfig={entry:'index.js',output:{path:'dist',filename:'index_bundle.js',},plugins:[newwebpack.DllReferencePlugin({context:path.join(__dirname),manifest:require('./build/vendor-manifest.json'),}),newHtmlWebpackPlugin(),newAddAssetHtmlPlugin({filepath:path.resolve(__dirname,'./build/*.dll.js'),}),],};

Add a polyfill file you have locally

Seeexample for an example of how to use it. You can run itby cloning this repo, runningyarn followed byyarn run example.

Webpack config

constpath=require('path');constHtmlWebpackPlugin=require('html-webpack-plugin');constAddAssetHtmlPlugin=require('../../');constwebpackConfig={entry:'entry.js',devtool:'#source-map',mode:'development',output:{path:'dist',filename:'index_bundle.js',},plugins:[newHtmlWebpackPlugin(),newAddAssetHtmlPlugin({filepath:path.resolve(__dirname,'./polyfill.js'),attributes:{nomodule:true,},}),],};

About

Add a JavaScript or CSS asset to the HTML generated by html-webpack-plugin

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors17


[8]ページ先頭

©2009-2025 Movatter.jp