diff --git a/README.md b/README.md index 9ae55ad..67a832a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -37,7 +38,8 @@ module.exports = { " Referrer-Policy: strict-origin-when-cross-origin", "/assets/*", " Cache-Control: public, max-age:360000" - ] + ], + include: 'css' }) ] }; diff --git a/index.js b/index.js index 7ec4f98..7d38754 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,15 @@ 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) { @@ -16,7 +21,7 @@ class NetlifyPushWebpackPlugin { 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