Skip to content

Commit 39cfb23

Browse files
committed
Allow array of files (#6)
1 parent 2df3e5d commit 39cfb23

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ $ npm i add-asset-html-webpack-plugin -D
1414
```
1515

1616
## Basic Usage
17-
The plugin will add the given JS or CSS file to the files Webpack knows about, and put it into the list of assets
18-
`html-webpack-plugin` injects into the generated html. Add the plugin the your config, providing it a filename:
17+
The plugin will add the given JS or CSS file to the files Webpack knows about, and put it into the
18+
list of assets `html-webpack-plugin` injects into the generated html. Add the plugin the your
19+
config, providing it a filename:
1920

2021
```js
2122
var HtmlWebpackPlugin = require('html-webpack-plugin')
@@ -48,6 +49,16 @@ This will add a script tag to the HTML generated by `html-webpack-plugin`, and l
4849
</html>
4950
```
5051

52+
NOTE: You can also pass an array of assets to be added. Same API as mentioned below, just pass
53+
multiple objects as an array.
54+
55+
```js
56+
new AddAssetHtmlPlugin([
57+
{ filename: require.resolve('./some-file') },
58+
{ filename: require.resolve('./some-other-file') }
59+
])
60+
```
61+
5162
## Options
5263
Options are passed to the plugin during instantiation.
5364

addAssetHtmlPlugin.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path'
2+
import Promise from 'bluebird'
23

34
// Copied from html-webpack-plugin
45
function resolvePublicPath (compilation, filename) {
@@ -12,31 +13,28 @@ function resolvePublicPath (compilation, filename) {
1213
return publicPath
1314
}
1415

16+
function addFileToAssets (htmlPluginData, compilation, { filename, typeOfAsset = 'js', includeSourcemap = true } = {}) {
17+
if (!filename) return compilation.errors.push(new Error('No filename defined'))
18+
19+
return htmlPluginData.plugin.addFileToAssets(filename, compilation)
20+
.then((filename) => htmlPluginData.assets[typeOfAsset].unshift(`${resolvePublicPath(compilation, filename)}${filename}`))
21+
.then(() => {
22+
if (includeSourcemap) {
23+
return htmlPluginData.plugin.addFileToAssets(`${filename}.map`, compilation)
24+
}
25+
return null
26+
})
27+
}
28+
1529
export default class AddAssetHtmlPlugin {
16-
constructor ({
17-
filename,
18-
includeSourcemap = false,
19-
typeOfAsset = 'js'
20-
} = {}) {
21-
this.filename = filename
22-
this.includeSourcemap = includeSourcemap
23-
this.typeOfAsset = typeOfAsset
30+
constructor (assets = []) {
31+
this.assets = Array.isArray(assets) ? assets.slice().reverse() : [assets]
2432
}
2533

2634
apply (compiler) {
2735
compiler.plugin('compilation', (compilation) => {
28-
if (!this.filename) return compilation.errors.push(new Error('No filename defined'))
29-
30-
const publicPath = resolvePublicPath(compilation, this.filename)
31-
3236
compilation.plugin('html-webpack-plugin-before-html-generation', (htmlPluginData, callback) => {
33-
htmlPluginData.plugin.addFileToAssets(this.filename, compilation)
34-
.then((filename) => htmlPluginData.assets[this.typeOfAsset].unshift(`${publicPath}${filename}`))
35-
.then(() => {
36-
if (this.includeSourcemap) {
37-
return htmlPluginData.plugin.addFileToAssets(`${this.filename}.map`, compilation)
38-
}
39-
})
37+
Promise.mapSeries(this.assets, asset => addFileToAssets(htmlPluginData, compilation, asset))
4038
.then(() => callback(null, htmlPluginData))
4139
})
4240
})

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"scripts": {
99
"compile": "babel addAssetHtmlPlugin.js -o index.js",
1010
"lint": "standard",
11-
"prepublish": "not-in-install && npm run compile || echo this is npm install",
11+
"prepublish": "not-in-install && npm run compile || in-install",
12+
"postpublish": "git push --follow-tags",
1213
"test": "npm run lint"
1314
},
1415
"repository": "SimenB/add-asset-html-webpack-plugin",
@@ -23,11 +24,14 @@
2324
"url": "https://github.com/SimenB/add-asset-html-webpack-plugin/issues"
2425
},
2526
"homepage": "https://github.com/SimenB/add-asset-html-webpack-plugin#readme",
26-
"dependencies": {},
27+
"dependencies": {
28+
"bluebird": "^3.3.5"
29+
},
2730
"devDependencies": {
2831
"babel-cli": "^6.9.0",
2932
"babel-plugin-add-module-exports": "^0.2.1",
3033
"babel-preset-es2015": "^6.9.0",
34+
"html-webpack-plugin": "^2.10.0",
3135
"in-publish": "^2.0.0",
3236
"standard": "^7.1.1"
3337
},

0 commit comments

Comments
 (0)