Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Option | Type | Description
--- | --- | ---
`filename` | String | Name and path of the generated headers file
`headers` | Array | Other headers to be added to the file
`include` | String | Only include 'css', 'js' or 'all' (default: 'all')

## Example

Expand All @@ -37,7 +38,8 @@ module.exports = {
" Referrer-Policy: strict-origin-when-cross-origin",
"/assets/*",
" Cache-Control: public, max-age:360000"
]
],
include: 'css'
})
]
};
Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ class NetlifyPushWebpackPlugin {
this.options = options;
}

buildHeaders({ js, css }, headers = []) {
buildHeaders({ js, css }, options = {}) {
const { headers } = this.options || [];
const { include } = this.options || 'all';

const scripts = js.map(f => ` Link: <${f}>; rel=preload; as=script`);
const styles = css.map(f => ` Link: <${f}>; rel=preload; as=style`);
return ["/*", ...scripts, ...styles, ...headers].join("\n");
return include === 'all' ? ["/*", ...scripts, ...styles, ...headers].join("\n")
: include === 'js' ? ["/*", ...scripts, ...headers].join("\n")
: include === 'css' ? ["/*", ...styles, ...headers].join("\n");
}

apply(compiler) {
compiler.hooks.compilation.tap("NetlifyPushWebpackPlugin", compilation => {
HtmlWebpackPlugin.getHooks(compilation).beforeAssetTagGeneration.tapAsync(
"NetlifyPushWebpackPlugin",
(data, cb) => {
const filedata = this.buildHeaders(data.assets, this.options.headers);
const filedata = this.buildHeaders(data.assets, this.options);
compilation.assets[`${this.options.filename}`] = {
source: () => filedata,
size: () => filedata.length
Expand Down