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

Deploy assets or node_modules package assets with html webpack plugin

NotificationsYou must be signed in to change notification settings

jharris4/html-webpack-deploy-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Enhanceshtml-webpack-plugin allowing you tocopyassets ornode_modules package assets into your webpack build andinject them as tags into yourhtml.

Built on top of thehtml-webpack-tags andcopy-webpack plugins.

Installation

$ npm install --save-dev html-webpack-deploy-plugin
  • You must be runningNode 10.0 along withWebpack 5.0 or higher for version3.x of this plugin.

  • You must be runningNode 8.6 or higher for version2.x of this plugin.

  • This plugin wasrenamed fromhtml-webpack-deploy-assets-plugin tohtml-webpack-deploy-plugin in version2.x.

  • For use with theNode < 8.6 please use version1.x (old READMEhere)


Integration

ThechunksSortMode option ofhtml-webpack-plugin3.x has adefault value ofauto.

This option is known to cause issues withcode splitting orlazy loaded bundles (#981).

When using this plugin with version3.x you should set thischunksSortMode to'none', like this:

newHtmlWebpackPlugin({chunksSortMode:'none'})

Configuration

The plugin configuration is specified by an optionsobject passed to its constructor.

This object has anassets option for copying & injecting local files, and apackages option for copying & injecting packages from your localnode_modules directory.

newHtmlWebpackDeployPlugin({assets:{copy:[{from:'a',to:'b'}],links:['b/my.css',{hash:false,publicPath:false,path:'b/my.png',attributes:{rel:'icon'}}],scripts:'b/my.js'},packages:{'bootstrap':{copy:[{from:'dist/bootstrap.min.css',to:'bootstrap.min.css'}],links:{useCdn:true,path:'bootstrap.min.css',cdnPath:'dist/bootstrap.min.css'},},'react':{copy:[{from:'umd',to:''}],scripts:{variableName:'React',path:'react.production.min.js',cdnPath:'umd/react.production.min.js',}},'react-dom':{copy:[{from:'umd',to:''}],scripts:{variableName:'ReactDOM',path:'react-dom.production.min.js',cdnPath:'umd/react-dom.production.min.js',},useCdn:false}},useCdn:true,getCdnPath:(packageName,packageVersion,packagePath)=>`https://unpkg.com/${packageName}@${packageVersion}/${packagePath}`});

All options for this plugin arevalidated as soon as the plugin is instantiated.

Options areinherited by inner levels of the config and will be overridden if specified in a level.

The available options are described below, grouped by at whatlevel in the plugin config they may be used.

Root Options

These options are only available at the root level of the plugin config.

NameTypeDefaultDescription
assets{Object}undefinedThe local assets to copy into the webpack output directory and inject into the template html file
packages{Object}undefinedThenode_modules packages to copy into the webpack output directory and inject into the template html file
useAssetsPath{Boolean}trueWhether or not to prefix all assets with theassetsPath
addAssetsPath{Function}see belowThe function to call to get the output path for assets when copying and injecting them
assetsPath{Boolean|String|Function}undefinedShortcut for specifying bothuseAssetsPath andaddAssetsPath at the same time
usePackagesPath{Boolean}trueWhether or not to prefix all packages with thepackagesPath
addPackagesPath{Function}see belowThe function to call to get the output path forpackages when copying and injecting them
packagesPath{Boolean|String|Function}undefinedShortcut for specifying bothusePackagesPath andaddPackagesPath at the same time
getPackagePath{Function}see belowThe function to call to get the output path for apackage &version when copying and injecting it
findNodeModulesPath{Function}see belowThe function to call to find thenode_modules directory where packages to be deployed are installed so theirversion can be read from theirpackage.json file. The default is to search upwards in the current working directory
files{Array<String>}[]If specified this plugin will only inject tags into the html-webpack-plugin instances that are injecting into these files (usesminimatch)
prependExternals{Boolean}trueWhether to defaultappend tofalse for any<script>tag that has anexternal orvariableName option specified

All Level Options

Several options from thehtml-webpack-tags-plugin are available at all levels.

Options are passed down the levels, and overriden if specified at a lower level.

NameTypeDefaultDescription
append{Boolean}trueWhether to prepend or append the injected tags relative to any existing or webpack bundle tags (should be set tofalse when using anyscript tagexternal)
useHash{Boolean}falseWhether to inject the webpackcompilation.hash into the tag paths
addHash{Function(assetPath:String, hash:String):String}see belowThe function to call when injecting thehash into the tag paths
hash{Boolean|Function}undefinedShortcut to specifyinguseHash andaddHash
usePublicPath{Boolean}trueWhether to inject the (webpack)publicPath into the tag paths
addPublicPath{Function(assetPath:String, publicPath:String):String}see belowWhether to inject thepublicPath into the tag paths
publicPath{Boolean|String|Function}undefinedShortcut to specifyingusePublicPath andaddPublicPath

Assets and Packages Options

NameTypeDefaultDescription
links{String|Object|Array<String|Object>}[]The tags to inject as<link> html tags
scripts{String|Object|Array<String|Object>}[]The tags to inject as<script> html tags
copy`{ArrayObject}`[]

Packages Options

NameTypeDefaultDescription
copy.fromAbsolute{Boolean}falseWhen set to true, thecopy.from willnot be prefixed with the package'snode_modules relative path

Root and Packages and Tag-inside-Packages Options

NameTypeDefaultDescription
useCdn{Boolean}falseWhether or not to use thegetCdnPath to replace the tag paths with theircdn urls
getCdnPath{Function}see belowThe function to use when replacing tag paths withcdn urls

Tag Options

The availabletag options forlinks orscripts are defined by thehtml-webpack-tags-plugin.

Additionaltag options are defined by this plugin:

NameTypeDefaultDescription
devPath{String}optionalAlternative path to use for the tag whenwebpack.mode === "development". Only used when theuseCdn option inherited by the tag is false
cdnPath{String}optionalAlternative path to use for the tag when theuseCdn option inherited by the tag is true

Default Options

This plugin will run and do nothing if no options ({}) are provided.

The default options for this plugin are shown below:

constpath=require('path');constfindUp=require('find-up');constslash=require('slash');// fixes slashes in file paths for windowsconstDEFAULT_OPTIONS={assets:{},packages:{},useAssetsPath:true,addAssetsPath:assetPath=>path.join('assets',assetPath),usePackagesPath:true,addPackagesPath:packagePath=>path.join('packages',packagePath),getPackagePath:(packageName,packageVersion,packagePath)=>path.join(packageName+'-'+packageVersion,packagePath),findNodeModulesPath:(cwd,packageName)=>findUp.sync(slash(path.join('node_modules',packageName)),{ cwd}),useCdn:false,getCdnPath:(packageName,packageVersion,packagePath)=>`https://unpkg.com/${packageName}@${packageVersion}/${packagePath}`};

Option Details

Theassets option can be used to specify local assets that should be copied to the webpack output directory and injected into theindex.html as tags.

This option requires an object with any of thecopy,links, orscripts properties.

The settings for these are based on thecopy-webpack-plugin and thehtml-webpack-tags-plugin

For example, to copy some assets to webpack, and insert a<link> and<script> tag:

constpluginOptions={assets:{copy:[{from:'src-path/assets',to:'dst-path/assets'},{from:'src-path/js',to:'dst-path/js'},{from:'src-path/css/src-file.png',to:'dst-path/dst-file.png'}],links:[{path:'dst-path/dst-file.png',attributes:{rel:'icon'}],scripts:[{path:'dst-path/js/script.js',}]}};

The above example will generate something like the following html:

<head><linkhref="${webpack.publicPath}dst-path/dst-file.png"rel="icon"></head><body><scriptsrc="${webpack.publicPath}dst-path/js/script.js"></script></body>

Thepackages option can be used to specify package assets that should be copied to the webpack output directory and injected into theindex.html as tags.

This option requires an object with any of thecopy,links, orscripts properties.

The settings for these are based on thecopy-webpack-plugin and thehtml-webpack-tags-plugin

For example, to copy some assets frombootstrap to webpack, and insert a<link> and<script> tag for bootstrap:

constpluginOptions={packages:{'bootstrap':{copy:[{from:'dist/css',to:'css/'},{from:'dist/js',to:'js/'}],links:['css/bootstrap.min.css'],scripts:{variableName:'Bootstrap',path:'js/bootstrap.bundle.min.js'}}}};

ThevariableName can be used to tellwebpack to stop bundling a package, and instead load it from the injected<script>.

The above example will generate something like the following html:

<head><linkhref="${webpack.publicPath}css/bootstrap.min.css"></head><body><scriptsrc="${webpack.publicPath}js/bootstrap.bundle.min.js"></script></body>

Examples

DeployingBootstrap css and fonts and an assets directory from local files:

plugins:[newHtmlWebpackPlugin(),newHtmlWebpackDeployAssetsPlugin({packages:{'bootstrap':{copy:[{from:'dist/css',to:'css/'},{from:'dist/fonts',to:'fonts/'}],links:['css/bootstrap.min.css','css/bootstrap-theme.min.css']}},assets:{copy:[{from:'src/assets',to:'assets/'}],links:{path:'/assets/icon.png',attributes:{rel:'icon'}}}})]

This will generate aindex.html something like:

<!DOCTYPE html><html><head><metacharset="UTF-8"><title>Webpack App</title><linkhref="any-webpack-generated-styles.css"rel="stylesheet"><linkhref="bootstrap-3.3.7/css/bootstrap.min.css"rel="stylesheet"><linkhref="bootstrap-3.3.7/css/bootstrap-theme.min.css"rel="stylesheet"><linkhref="/assets/icon.png"rel="icon"></head><body><scriptsrc="any-webpack-generated-bundles.js"></script></body></html>

Note that additionally, the contents of the following directories will be copied:

node_modules/bootstrap/dist/css ->dist/bootstrap-3.3.7/cssnode_modules/bootstrap/dist/fonts ->dist/bootstrap-3.3.7/fontssrc/assets ->dist/assets


DeployingReact from aCDN:

plugins:[newHtmlWebpackPlugin(),newHtmlWebpackDeployAssetsPlugin({packages:{'react':{copy:[{from:'umd',to:''}],scripts:{variableName:'React',path:'react.production.min.js',cdnPath:'umd/react.production.min.js',}},'react-dom':{copy:[{from:'umd',to:''}],scripts:{variableName:'ReactDOM',path:'react-dom.production.min.js',cdnPath:'umd/react-dom.production.min.js',}}}useCdn:true,getCdnPath:(packageName,packageVersion,packagePath)=>`https://unpkg.com/${packageName}@${packageVersion}/${packagePath}`})]

This will generate aindex.html with your webpack bundled outputand the following:

<!DOCTYPE html><html><head><metacharset="UTF-8"><title>Webpack App</title><linkhref="any-webpack-generated-styles.css"rel="stylesheet"></head><body><scriptsrc="https://unpkg.com/react@16.8.6/umd/react.production.js"></script><scriptsrc="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.js"></script><scriptsrc="any-webpack-generated-bundles.js"></script><!-- react & react-dom were removed from webpack bundles automatically --></body></html>

DeployingReact fromLocal UMD Bundles:

plugins:[newHtmlWebpackPlugin(),newHtmlWebpackDeployAssetsPlugin({packages:{'react':{copy:[{from:'umd',to:''}],scripts:{variableName:'React',path:'react.production.min.js',devPath:'react.development.js'}},'react-dom':{copy:[{from:'umd',to:''}],scripts:{variableName:'ReactDOM',path:'react-dom.production.min.js',devPath:'react-dom.development.js'}}}})]

This copiesreact andreact-dom into webpack's output directory, versioning the directory automatically based on their installed version. They can now be referenced from the tag paths in the html.

Webpack is instructed thatreact andreact-dom areexternal so they areno longer bundled by webpack.

The generatedindex.html looks like:

<!DOCTYPE html><html><head><metacharset="UTF-8"><title>Webpack App</title><linkhref="any-webpack-generated-styles.css"rel="stylesheet"></head><body><scriptsrc="my-public-path/packages/react-16.8.6/react.production.min.js"></script><scriptsrc="my-public-path/packages/react-dom-16.8.6/react-dom.production.min.js"></script><scriptsrc="any-webpack-generated-bundles.js"></script><!-- react & react-dom were removed from webpack bundles automatically --></body></html>

Or indevelopment mode because thedevPath option was specified, the generatedindex.html looks like:

<!DOCTYPE html><html><head><metacharset="UTF-8"><title>Webpack App</title><linkhref="any-webpack-generated-styles.css"rel="stylesheet"></head><body><scriptsrc="my-public-path/packages/react-16.8.6/react.development.js"></script><scriptsrc="my-public-path/packages/react-dom-16.8.6/react-dom.development.js"></script><scriptsrc="any-webpack-generated-bundles.js"></script><!-- react & react-dom were removed from webpack bundles automatically --></body></html>

Non CDN Options

When doing custom deployment without a CDN it can be useful to configure the directories that all assets and packages are copied to and served from.

The following examples show some useful settings for such situations.


Disabling the grouping of assets into theassets directory:

newHtmlWebpackDeployAssetsPlugin({useAssetsPath:false});
<linkhref="my-public-path/assets/file.css"><scriptsrc="my-public-path/assets/file.js"></script>

becomes:

<linkhref="my-public-path/file.css"><scriptsrc="my-public-path/file.js"></script>

Custom grouping of assets into aconfigurable directory:

newHtmlWebpackDeployAssetsPlugin({assetsPath:'my-assets-path'});
<linkhref="my-public-path/assets/file.css"><scriptsrc="my-public-path/assets/file.js"></script>

becomes:

<linkhref="my-public-path/my-assets-path/file.css"><scriptsrc="my-public-path/my-assets-path/file.js"></script>

Disabling the grouping of packages into thepackages directory:

newHtmlWebpackDeployAssetsPlugin({usePackagesPath:false});
<linkhref="my-public-path/packages/bootstrap-4.3.1/bootstrap.min.css"><scriptsrc="my-public-path/packages/react-16.8.6/react.production.min.js"></script>

becomes:

<linkhref="my-public-path/bootstrap-4.3.1/bootstrap.min.css"><scriptsrc="my-public-path/react-16.8.6/react.production.min.js"></script>

Custom grouping of packages into aconfigurable directory:

newHtmlWebpackDeployAssetsPlugin({packagesPath:'my-packages-path'});
<linkhref="my-public-path/packages/bootstrap-4.3.1/bootstrap.min.css"><scriptsrc="my-public-path/packages/react-16.8.6/react.production.min.js"></script>

becomes:

<linkhref="my-public-path/my-packages-path/bootstrap-4.3.1/bootstrap.min.css"><scriptsrc="my-public-path/my-packages-path/react-16.8.6/react.production.min.js"></script>

About

Deploy assets or node_modules package assets with html webpack plugin

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp