Skip to content

Commit b6fc2fa

Browse files
committed
Fix new code and add tests
1 parent bf466ce commit b6fc2fa

15 files changed

+5237
-19
lines changed

README.md

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
# NetlifyPushWebpackPlugin
2+
23
Generate HTTP2 Server Push `_headers` file for Netlify using HtmlWebpackPlugin.
34

4-
For use with Webpack 4 and HtmlWebpackPlugin 4.0.0-alpha.2
5+
For use with Webpack 4 and HtmlWebpackPlugin 4.0.0-beta.5
56

67
## Installation
78

89
```
910
npm i netlify-push-webpack-plugin
1011
```
1112

12-
or
13-
13+
or
1414

1515
```
1616
yarn add netlify-push-webpack-plugin
1717
```
1818

19-
2019
## Usage
2120

22-
Option | Type | Description
23-
--- | --- | ---
24-
`filename` | String | Name and path of the generated headers file
25-
`headers` | Array | Other headers to be added to the file
26-
`include` | String | Only include 'css', 'js' or 'all' (default: 'all')
21+
| Option | Type | Description |
22+
| ---------- | ------ | -------------------------------------------------- |
23+
| `filename` | String | Name and path of the generated headers file |
24+
| `headers` | Array | Other headers to be added to the file (optional) |
25+
| `include` | String | Only include 'css', 'js' or 'all' (default: 'all') |
2726

2827
## Example
2928

29+
The following config
30+
3031
```js
32+
const HtmlWebpackPlugin = require("html-webpack-plugin");
33+
const NetlifyServerPushPlugin = require("netlify-push-webpack-plugin");
34+
3135
module.exports = {
3236
plugins: [
3337
new HtmlWebpackPlugin(),
@@ -39,8 +43,28 @@ module.exports = {
3943
"/assets/*",
4044
" Cache-Control: public, max-age:360000"
4145
],
42-
include: 'css'
46+
include: "css"
4347
})
4448
]
4549
};
4650
```
51+
52+
will result in a headers file looking something like this:
53+
54+
```
55+
/*
56+
Link: <bundle.js>; rel=preload; as=script
57+
Link: <main.css>; rel=preload; as=style
58+
X-Frame-Options: DENY
59+
Referrer-Policy: strict-origin-when-cross-origin
60+
/assets/*
61+
Cache-Control: public, max-age:360000
62+
```
63+
64+
## Testing
65+
66+
Tests are ran using using Ava with the following command:
67+
68+
```
69+
yarn run test
70+
```

index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ class NetlifyPushWebpackPlugin {
55
this.options = options;
66
}
77

8-
buildHeaders({ js, css }, options = {}) {
9-
const { headers } = this.options || [];
10-
const { include } = this.options || 'all';
8+
buildHeaders({ js, css }) {
9+
const headers = this.options.headers || [];
10+
const include = this.options.include || "all";
1111

1212
const scripts = js.map(f => ` Link: <${f}>; rel=preload; as=script`);
1313
const styles = css.map(f => ` Link: <${f}>; rel=preload; as=style`);
14-
return include === 'all' ? ["/*", ...scripts, ...styles, ...headers].join("\n")
15-
: include === 'js' ? ["/*", ...scripts, ...headers].join("\n")
16-
: include === 'css' ? ["/*", ...styles, ...headers].join("\n");
14+
15+
if(include === "all")
16+
return ["/*", ...scripts, ...styles, ...headers].join("\n")
17+
if(include === "js")
18+
return ["/*", ...scripts, ...headers].join("\n")
19+
if(include === "css")
20+
return ["/*", ...styles, ...headers].join("\n")
21+
return ""
1722
}
1823

1924
apply(compiler) {

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "netlify-push-webpack-plugin",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Generate HTTP2 Server Push headers for Netlify using HtmlWebpackPlugin",
55
"main": "index.js",
66
"files": [
@@ -17,14 +17,25 @@
1717
"netlify",
1818
"push"
1919
],
20+
"scripts": {
21+
"test": "ava tests/test.js"
22+
},
2023
"author": "Leon Koole <[email protected]> (https://github.com/koole)",
2124
"license": "MIT",
2225
"bugs": {
2326
"url": "https://github.com/koole/netlify-push-webpack-plugin/issues"
2427
},
2528
"homepage": "https://github.com/koole/netlify-push-webpack-plugin",
2629
"peerDependencies": {
27-
"html-webpack-plugin": "^4.0.0-alpha.2",
30+
"html-webpack-plugin": "^4.0.0-beta.5",
31+
"webpack": "^4.0.0"
32+
},
33+
"devDependencies": {
34+
"ava": "^1.4.1",
35+
"css-loader": "^2.1.1",
36+
"html-webpack-plugin": "^4.0.0-beta.5",
37+
"mini-css-extract-plugin": "^0.6.0",
38+
"rimraf": "^2.6.3",
2839
"webpack": "^4.0.0"
2940
}
30-
}
41+
}

tests/all/_headers

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
Link: <bundle.js>; rel=preload; as=script
3+
Link: <main.css>; rel=preload; as=style

tests/all/webpack.config.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const HtmlWebpackPlugin = require("html-webpack-plugin");
2+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
3+
4+
const NetlifyServerPushPlugin = require("../../index.js");
5+
6+
module.exports = {
7+
entry: "./tests/project/index.js",
8+
output: {
9+
path: __dirname + "/dist",
10+
filename: "bundle.js"
11+
},
12+
module: {
13+
rules: [
14+
{
15+
test: /\.css$/,
16+
use: [
17+
{
18+
loader: MiniCssExtractPlugin.loader,
19+
options: {
20+
// you can specify a publicPath here
21+
// by default it uses publicPath in webpackOptions.output
22+
publicPath: "../",
23+
hmr: process.env.NODE_ENV === "development"
24+
}
25+
},
26+
"css-loader"
27+
]
28+
}
29+
]
30+
},
31+
plugins: [
32+
new MiniCssExtractPlugin(),
33+
new HtmlWebpackPlugin(),
34+
new NetlifyServerPushPlugin({
35+
filename: "_headers",
36+
include: "all"
37+
})
38+
]
39+
};

tests/css/_headers

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/*
2+
Link: <main.css>; rel=preload; as=style

tests/css/webpack.config.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const HtmlWebpackPlugin = require("html-webpack-plugin");
2+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
3+
4+
const NetlifyServerPushPlugin = require("../../index.js");
5+
6+
module.exports = {
7+
entry: "./tests/project/index.js",
8+
output: {
9+
path: __dirname + "/dist",
10+
filename: "bundle.js"
11+
},
12+
module: {
13+
rules: [
14+
{
15+
test: /\.css$/,
16+
use: [
17+
{
18+
loader: MiniCssExtractPlugin.loader,
19+
options: {
20+
// you can specify a publicPath here
21+
// by default it uses publicPath in webpackOptions.output
22+
publicPath: "../",
23+
hmr: process.env.NODE_ENV === "development"
24+
}
25+
},
26+
"css-loader"
27+
]
28+
}
29+
]
30+
},
31+
plugins: [
32+
new MiniCssExtractPlugin(),
33+
new HtmlWebpackPlugin(),
34+
new NetlifyServerPushPlugin({
35+
filename: "_headers",
36+
headers: [],
37+
include: "css"
38+
})
39+
]
40+
};

tests/custom_headers/_headers

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Link: <bundle.js>; rel=preload; as=script
3+
Link: <main.css>; rel=preload; as=style
4+
X-Frame-Options: DENY
5+
Referrer-Policy: strict-origin-when-cross-origin
6+
/assets/*
7+
Cache-Control: public, max-age:360000
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const HtmlWebpackPlugin = require("html-webpack-plugin");
2+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
3+
4+
const NetlifyServerPushPlugin = require("../../index.js");
5+
6+
module.exports = {
7+
entry: "./tests/project/index.js",
8+
output: {
9+
path: __dirname + "/dist",
10+
filename: "bundle.js"
11+
},
12+
module: {
13+
rules: [
14+
{
15+
test: /\.css$/,
16+
use: [
17+
{
18+
loader: MiniCssExtractPlugin.loader,
19+
options: {
20+
// you can specify a publicPath here
21+
// by default it uses publicPath in webpackOptions.output
22+
publicPath: "../",
23+
hmr: process.env.NODE_ENV === "development"
24+
}
25+
},
26+
"css-loader"
27+
]
28+
}
29+
]
30+
},
31+
plugins: [
32+
new MiniCssExtractPlugin(),
33+
new HtmlWebpackPlugin(),
34+
new NetlifyServerPushPlugin({
35+
filename: "_headers",
36+
headers: [
37+
" X-Frame-Options: DENY",
38+
" Referrer-Policy: strict-origin-when-cross-origin",
39+
"/assets/*",
40+
" Cache-Control: public, max-age:360000"
41+
],
42+
include: "all"
43+
})
44+
]
45+
};

tests/js/_headers

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/*
2+
Link: <bundle.js>; rel=preload; as=script

tests/js/webpack.config.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const HtmlWebpackPlugin = require("html-webpack-plugin");
2+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
3+
4+
const NetlifyServerPushPlugin = require("../../index.js");
5+
6+
module.exports = {
7+
entry: "./tests/project/index.js",
8+
output: {
9+
path: __dirname + "/dist",
10+
filename: "bundle.js"
11+
},
12+
module: {
13+
rules: [
14+
{
15+
test: /\.css$/,
16+
use: [
17+
{
18+
loader: MiniCssExtractPlugin.loader,
19+
options: {
20+
// you can specify a publicPath here
21+
// by default it uses publicPath in webpackOptions.output
22+
publicPath: "../",
23+
hmr: process.env.NODE_ENV === "development"
24+
}
25+
},
26+
"css-loader"
27+
]
28+
}
29+
]
30+
},
31+
plugins: [
32+
new MiniCssExtractPlugin(),
33+
new HtmlWebpackPlugin(),
34+
new NetlifyServerPushPlugin({
35+
filename: "_headers",
36+
headers: [],
37+
include: "js"
38+
})
39+
]
40+
};

tests/project/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "./style.css";
2+
3+
console.log("Test")

tests/project/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: #000;
3+
}

0 commit comments

Comments
 (0)