├── .editorconfig ├── .gitignore ├── .travis.yml ├── .vscode └── launch.json ├── CHANGELOG.md ├── LICENSE ├── README.V1.md ├── README.md ├── index.js ├── package.json ├── spec ├── api.spec.js ├── browser.spec.js ├── end_to_end.spec.js ├── fixtures │ ├── a-dir │ │ ├── file-a.css │ │ ├── file-a.js │ │ ├── file-b.css │ │ └── file-b.js │ ├── app.css │ ├── astyle.css │ ├── entry.js │ ├── exclude.css │ ├── external │ │ ├── external-entry.js │ │ ├── external-style.css │ │ ├── index.html │ │ ├── node_modules │ │ │ ├── fake-a-package │ │ │ │ ├── fake-a-bundle.js │ │ │ │ ├── fake-a-entry.js │ │ │ │ └── package.json │ │ │ ├── fake-b-package │ │ │ │ ├── fake-b-bundle.js │ │ │ │ ├── fake-b-entry.js │ │ │ │ └── package.json │ │ │ ├── fake-c-package │ │ │ │ ├── fake-c-bundle.js │ │ │ │ ├── fake-c-entry.js │ │ │ │ └── package.json │ │ │ └── fake-other-package │ │ │ │ ├── fake-other-bundle.js │ │ │ │ └── package.json │ │ └── package.json │ ├── glob-a.css │ ├── glob-a.js │ ├── glob-b.css │ ├── glob-b.js │ ├── glob.css │ ├── glob.js │ ├── index-no-inject.html │ ├── index.html │ └── other ├── option_validation.spec.js └── support │ └── jasmine.json ├── typings.d.ts └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | insert_final_newline = true 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | 10 | [*.md] 11 | trim_trailing_whitespace = false 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /node_modules/ 3 | /dist/ 4 | npm-debug.log 5 | /.idea/ 6 | *.iml 7 | spec/**/dist 8 | 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "10" 4 | - "12" 5 | - "14" 6 | script: 7 | - npm test -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Yarn Test", 11 | "program": "${workspaceFolder}/node_modules/.bin/jasmine" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | https://github.com/jharris4/html-webpack-tags-plugin 5 | 6 | 7 | # [3.0.2](https://github.com/jharris4/html-webpack-tags-plugin/compare/3.0.1...3.0.2) (2021-10-30) 8 | 9 | ### Bug Fixes 10 | 11 | * Fix script attributes not being copied when scripts are injected in the head of the html document [[#79](https://github.com/jharris4/html-webpack-tags-plugin/issues/79)]. 12 | 13 | 14 | # [3.0.1](https://github.com/jharris4/html-webpack-tags-plugin/compare/3.0.0...3.0.1) (2021-04-07) 15 | 16 | ### Features 17 | 18 | * Added `webpack` & `html-webpack-plugin` to peerDependencies. 19 | 20 | 21 | # [3.0.0](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.17...3.0.0) (2021-02-03) 22 | 23 | ### Features 24 | 25 | * Updated to support `webpack` & `html-webpack-plugin` version **`5`**. 26 | 27 | ### BREAKING CHANGES 28 | 29 | * webpack` & `html-webpack-plugin` version **`5`** are now required. 30 | * Node version **`>=10`** is now required. 31 | 32 | 33 | 34 | # [2.0.17](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.16...2.0.17) (2019-09-23) 35 | 36 | ### Bug Fixes 37 | 38 | * Add typings.d.ts to `files` in `package.json` (oops) [[#52](https://github.com/jharris4/html-webpack-tags-plugin/issues/52)]. 39 | 40 | 41 | # [2.0.16](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.15...2.0.16) (2019-09-23) 42 | 43 | ### Features 44 | 45 | * Add TypeScript definitions [[#52](https://github.com/jharris4/html-webpack-tags-plugin/issues/52)]. 46 | 47 | 48 | # [2.0.15](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.14...2.0.15) (2019-08-20) 49 | 50 | ### Features 51 | 52 | * Renamed the meta option to **`metas`**. The plural version is more consistent with the **`tags`**, **`scripts`** and **`links`** options. 53 | 54 | 55 | # [2.0.14](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.13...2.0.14) (2019-08-20) 56 | 57 | ### Features 58 | 59 | * Add new **`meta`** option (default **undefined**) that allows `` tags to be injected. 60 | 61 | 62 | # [2.0.13](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.12...2.0.13) (2019-06-18) 63 | 64 | ### Bug Fixes 65 | 66 | * Use `url.resolve` instead of `path.join` to fix a bug when the publicPath contains `//`. [[#47](https://github.com/jharris4/html-webpack-tags-plugin/issues/47)]. 67 | 68 | ### Features 69 | 70 | * Update all dependency packages to latest. 71 | 72 | 73 | # [2.0.12](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.11...2.0.12) (2019-05-03) 74 | 75 | ### Features 76 | 77 | * Update `slash` package to `3.0.0`. 78 | 79 | 80 | # [2.0.11](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.10...2.0.11) (2019-05-03) 81 | 82 | ### Features 83 | 84 | * Add new **`globFlatten`** tag option (default **false**) that allows paths to be stripped from glob matched file paths. 85 | 86 | 87 | # [2.0.10](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.9...2.0.10) (2019-04-27) 88 | 89 | ### Features 90 | 91 | * Add new **`prependExternals`** option (default **true**) that auto-prepends (**`append`**: false) any scripts with the **`external`** option specified. 92 | 93 | 94 | # [2.0.9](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.8...2.0.9) (2019-04-24) 95 | 96 | ### Bug Fixes 97 | 98 | * More robust validation logic for all options across the board. 99 | 100 | ### Features 101 | 102 | * Adds support for all `top` level options to be specified at the `tag` level. 103 | * `HtmlWebpackTagsPlugin.api` now ready for use by any plugins wanting to extend this plugin's options. 104 | 105 | 106 | # [2.0.8](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.7...2.0.8) (2019-04-23) 107 | 108 | ### Features 109 | 110 | * Adds an `api` property to the plugin constructor, allowing reuse of option validation by other plugins. 111 | 112 | 113 | # [2.0.7](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.6...2.0.7) (2019-04-23) 114 | 115 | ### Features 116 | 117 | * Adds stricter/better option validation. 118 | 119 | 120 | # [2.0.6](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.5...2.0.6) (2019-04-23) 121 | 122 | ### Bug Fixes 123 | 124 | * Fixes use of this plugin with [html-webpack-plugin@4.x](https://github.com/jantimon/html-webpack-plugin). [[#45](https://github.com/jharris4/html-webpack-tags-plugin/issues/45)]. 125 | 126 | 127 | # [2.0.5](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.4...2.0.5) (2019-04-23) 128 | 129 | ### Features 130 | 131 | * Adds support for specifying the append option at the tag level. 132 | 133 | 134 | # [2.0.4](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.3...2.0.4) (2019-04-22) 135 | 136 | ### Features 137 | 138 | * Added browser tests to this package (using [puppeteer](https://github.com/GoogleChrome/puppeteer)). 139 | 140 | ### Bug Fixes 141 | 142 | * Fix **windows** `path` formatting issues introduced in the `2.x` version rewrite. [[#44](https://github.com/jharris4/html-webpack-tags-plugin/issues/44)]. 143 | 144 | 145 | # [2.0.3](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.2...2.0.3) (2019-04-19) 146 | 147 | ### Features 148 | 149 | * Adds support for new **external** script tag options that can control webpack's external config. 150 | 151 | 152 | # [2.0.2](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.1...2.0.2) (2019-04-19) 153 | 154 | ### Bug Fixes 155 | 156 | * Fix bugs related to renaming the **assets** option name to **tags**. 157 | 158 | 159 | # [2.0.1](https://github.com/jharris4/html-webpack-tags-plugin/compare/2.0.0...2.0.1) (2019-04-19) 160 | 161 | Version `2.0.1` renamed this package from `html-webpack-include-assets-plugin` to `html-webpack-tags-plugin`. 162 | 163 | ### BREAKING CHANGES 164 | 165 | * The **assets** option was renamed to the **tags** option 166 | * The **asset.assetPath** option was renamed to **asset.sourcePath** 167 | 168 | 169 | # [2.0.0](https://github.com/jharris4/html-webpack-tags-plugin/compare/1.0.10...2.0.0) (2019-04-18) 170 | 171 | Version `2.0.0` is a full rewrite of this plugin using ES6 instead of ES5 source code. 172 | 173 | ### Bug Fixes 174 | 175 | * More robust logic for separating `script` vs `link` tags compared with version `1.0.x 176 | * More robust logic for injecting attributes into `link` tags 177 | * Fix inconsistencies with the `hash` and `publicPath` options from version `1.0.x` 178 | 179 | 180 | ### Features 181 | 182 | * New `links` and `scripts` plugin options added as shortcuts for injecting `assets` without worrying about `type` or `file extension` 183 | 184 | 185 | ### BREAKING CHANGES 186 | 187 | * **Node >= 8.6** is now required due to the use of `object spread` syntax in the plugin source code 188 | * **append** option now defaults to **true** 189 | 190 | 191 | 192 | # [1.0.10](https://github.com/jharris4/html-webpack-tags-plugin/compare/1.0.9...1.0.10) (2018-04-12) 193 | 194 | This is the last `1.0.x` version which supports **Node < 8.6**. 195 | 196 | * Rename links to cssAssets and improve test coverage ([7e78bec](https://github.com/jharris4/html-webpack-tags-plugin/commit/7e78bec)) 197 | * Add selfClosingTag and voidTag to links ([97ac502](https://github.com/jharris4/html-webpack-tags-plugin/commit/97ac502)) 198 | * misc cleanup ([6ca39ac](https://github.com/jharris4/html-webpack-tags-plugin/commit/6ca39ac)) 199 | 200 | 201 | # [1.0.9](https://github.com/jharris4/html-webpack-tags-plugin/compare/1.0.8...1.0.9) (2018-04-12) 202 | 203 | This version adds support for the `links` option, similar to the option in version `2.x` except `href` is used instead of `path`. 204 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jon Harris 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.V1.md: -------------------------------------------------------------------------------- 1 | Include Assets extension for the HTML Webpack Plugin 2 | ======================================== 3 | [](https://badge.fury.io/js/html-webpack-include-assets-plugin) [](https://travis-ci.org/jharris4/html-webpack-include-assets-plugin) [](https://github.com/Flet/semistandard) 4 | 5 | This is the `README.md` from **version 1.x** which provides support for ** Node < 8.6 **. 6 | 7 | This **version is deprecated** in favour of [https://github.com/jharris4/html-webpack-include-assets-plugin](html-webpack-include-assets-plugin version 2). 8 | 9 | Enhances [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin) 10 | functionality by allowing you to specify js or css assets to be included. 11 | 12 | When using a plugin such as [copy-webpack-plugin](https://github.com/webpack-contrib/copy-webpack-plugin) you may have assets output to your build directory that are not detected/output by the html-webpack-plugin. 13 | 14 | This plugin allows you to force some of these assets to be included in the output from html-webpack-plugin. 15 | 16 | Installation 17 | ------------ 18 | You must be running webpack on node 8.x or higher 19 | 20 | Install the plugin with npm: 21 | ```shell 22 | $ npm install --save-dev html-webpack-include-assets-plugin 23 | ``` 24 | 25 | 26 | Basic Usage 27 | ----------- 28 | Require the plugin in your webpack config: 29 | 30 | ```javascript 31 | var HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin'); 32 | ``` 33 | 34 | Add the plugin to your webpack config: 35 | 36 | ```javascript 37 | output: { 38 | publicPath: '/abc/' 39 | }, 40 | plugins: [ 41 | new HtmlWebpackPlugin(), 42 | new HtmlWebpackIncludeAssetsPlugin({ assets: ['a.js', 'b.css'], append: true }) 43 | ] 44 | ``` 45 | 46 | Which will generate html like this: 47 | 48 | ```html 49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | ``` 58 | 59 | 60 | Options 61 | ------- 62 | The available options are: 63 | 64 | - `jsExtensions`: `string` or `array` 65 | 66 | Specifies the file extensions to use to determine if assets are script assets. Default is `['.js']`. 67 | 68 | - `cssExtensions`: `string` or `array` 69 | 70 | Specifies the file extensions to use to determine if assets are style assets. Default is `['.css']`. 71 | 72 | - `assets`: `string` or `array` or `object` 73 | 74 | Assets that will be output into your html-webpack-plugin template. 75 | 76 | To specify just one asset, simply pass a string or object. To specify multiple, pass an array of strings or objects. 77 | 78 | If the asset path is static and ends in one of the `jsExtensions` or `cssExtensions` values, simply use a string value. 79 | 80 | If the asset is not static or does not have a valid extension, you can instead pass an object with properties `path` (required) and `type` or `glob` or `globPath` or `attributes` (optional). In this case `path` is the asset href/src, `type` is one of `js` or `css`, and `glob` is a wildcard to use to match all files in the path (uses the [glob](https://github.com/isaacs/node-glob) package). The `globPath` can be used to specify the directory from which the `glob` should search for filename matches (the default is to use `path` within webpack's output directory). 81 | 82 | The `attributes` property may be used to add additional attributes to the link or script element that is injected. The keys of this object are attribute names and the values are the attribute values (string or boolean key values are allowed). 83 | 84 | The `assetPath` property may be used to specify the full path to the included asset. This can be useful as it will trigger a recompilation after the assets have changed when using `webpack-dev-server` or `webpack-dev-middleware` in development mode. 85 | 86 | - `append`: `boolean` 87 | 88 | Specifying whether the assets should be prepended (`false`) before any existing assets, or appended (`true`) after them. 89 | 90 | - `resolvePaths`: `boolean` 91 | 92 | Specifying whether the asset paths should be resolved with `path.resolve` (i.e. made absolute). 93 | 94 | - `publicPath`: `boolean` or `string` 95 | 96 | Specifying whether the assets should be prepended with webpack's public path or a custom publicPath (`string`). 97 | 98 | A value of `false` may be used to disable prefixing with webpack's publicPath, or a value like `myPublicPath/` may be used to prefix all assets with the given string. Default is `true`. 99 | 100 | - `hash`: `boolean` or `function(assetName, hash)` 101 | 102 | Specifying whether the assets should be appended with webpack's compilation hash. This is useful for cache busting. Default is `false`. 103 | 104 | - `files`: `string` or `array` 105 | 106 | Files that the assets will be added to. 107 | 108 | By default the assets will be included in all files. If files are defined, the assets will only be included in specified file globs (uses the [minimatch](https://github.com/isaacs/minimatch) package). 109 | 110 | - `cssAssets`: `array` 111 | 112 | Optional shortcut for adding css assets. An array of css asset objects. 113 | 114 | See the cssAssets example below for the syntax of css asset object. 115 | 116 | 117 | Example 118 | ------- 119 | 120 | _____ 121 | 122 | Using `HtmlWebpackIncludeAssetsPlugin` and `CopyWebpackPlugin` to include assets to `html-webpack-plugin` template : 123 | 124 | ```javascript 125 | plugins: [ 126 | new CopyWebpackPlugin([ 127 | { from: 'node_modules/bootstrap/dist/css', to: 'css/'}, 128 | { from: 'node_modules/bootstrap/dist/fonts', to: 'fonts/'} 129 | ]), 130 | new HtmlWebpackPlugin(), 131 | new HtmlWebpackIncludeAssetsPlugin({ 132 | assets: ['css/bootstrap.min.css', 'css/bootstrap-theme.min.css'], 133 | append: false 134 | }) 135 | ] 136 | ``` 137 | 138 | _____ 139 | 140 | Appending and prepending at the same time : 141 | 142 | ```javascript 143 | plugins: [ 144 | new CopyWebpackPlugin([ 145 | { from: 'node_modules/bootstrap/dist/css', to: 'css/'}, 146 | { from: 'node_modules/bootstrap/dist/fonts', to: 'fonts/'} 147 | ]), 148 | new HtmlWebpackPlugin(), 149 | new HtmlWebpackIncludeAssetsPlugin({ 150 | assets: ['css/bootstrap.min.css', 'css/bootstrap-theme.min.css'], 151 | append: false 152 | }), 153 | new HtmlWebpackIncludeAssetsPlugin({ 154 | assets: ['css/custom.css'], 155 | append: true 156 | }) 157 | ] 158 | ``` 159 | 160 | _____ 161 | 162 | Using custom `jsExtensions` : 163 | 164 | ```javascript 165 | plugins: [ 166 | new HtmlWebpackPlugin(), 167 | new HtmlWebpackIncludeAssetsPlugin({ 168 | assets: ['dist/output.js', 'lib/content.jsx'], 169 | append: false, 170 | jsExtensions: ['.js', '.jsx'] 171 | }) 172 | ] 173 | ``` 174 | 175 | _____ 176 | 177 | Using custom `publicPath` : 178 | 179 | ```javascript 180 | plugins: [ 181 | new CopyWebpackPlugin([ 182 | { from: 'node_modules/bootstrap/dist/css', to: 'css/'}, 183 | { from: 'node_modules/bootstrap/dist/fonts', to: 'fonts/'} 184 | ]), 185 | new HtmlWebpackPlugin(), 186 | new HtmlWebpackIncludeAssetsPlugin({ 187 | assets: ['css/bootstrap.min.css', 'css/bootstrap-theme.min.css'], 188 | append: false, 189 | publicPath: 'myPublicPath/' 190 | }) 191 | ] 192 | ``` 193 | 194 | _____ 195 | 196 | Or to include assets without prepending the `publicPath`: 197 | 198 | ```javascript 199 | plugins: [ 200 | new HtmlWebpackPlugin(), 201 | new HtmlWebpackIncludeAssetsPlugin({ 202 | assets: ['css/no-public-path.min.css', 'http://some.domain.com.js'], 203 | append: false, 204 | publicPath: false 205 | }) 206 | ] 207 | ``` 208 | 209 | _____ 210 | 211 | Manually specifying asset types : 212 | 213 | ```javascript 214 | plugins: [ 215 | new CopyWebpackPlugin([ 216 | { from: 'node_modules/bootstrap/dist/css', to: 'css/'}, 217 | { from: 'node_modules/bootstrap/dist/fonts', to: 'fonts/'} 218 | ]), 219 | new HtmlWebpackPlugin(), 220 | new HtmlWebpackIncludeAssetsPlugin({ 221 | assets: [ 222 | '/css/bootstrap.min.css', 223 | '/css/bootstrap-theme.min.css', 224 | { path: 'https://fonts.googleapis.com/css?family=Material+Icons', type: 'css' } 225 | ], 226 | append: false, 227 | publicPath: '' 228 | }) 229 | ] 230 | ``` 231 | 232 | _____ 233 | 234 | Adding custom attributes to asset tags : 235 | 236 | The bootstrap-theme link tag will be given an id="bootstrapTheme" attribute. 237 | 238 | ```javascript 239 | plugins: [ 240 | new CopyWebpackPlugin([ 241 | { from: 'node_modules/bootstrap/dist/css', to: 'css/'}, 242 | { from: 'node_modules/bootstrap/dist/fonts', to: 'fonts/'} 243 | ]), 244 | new HtmlWebpackPlugin(), 245 | new HtmlWebpackIncludeAssetsPlugin({ 246 | assets: [ 247 | '/css/bootstrap.min.css', 248 | { path: '/css/bootstrap-theme.min.css', attributes: { id: 'bootstrapTheme' } } 249 | ], 250 | append: false, 251 | publicPath: '' 252 | }) 253 | ] 254 | ``` 255 | 256 | _____ 257 | 258 | Using `hash` option : 259 | 260 | When the hash option is set to `true`, asset paths will be appended with a hash query parameter (`?hash=