Skip to content

Commit

Permalink
Fix new code and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koole committed May 7, 2019
1 parent bf466ce commit b6fc2fa
Show file tree
Hide file tree
Showing 15 changed files with 5,237 additions and 19 deletions.
44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
# NetlifyPushWebpackPlugin

Generate HTTP2 Server Push `_headers` file for Netlify using HtmlWebpackPlugin.

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

## Installation

```
npm i netlify-push-webpack-plugin
```

or

or

```
yarn add netlify-push-webpack-plugin
```


## Usage

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')
| Option | Type | Description |
| ---------- | ------ | -------------------------------------------------- |
| `filename` | String | Name and path of the generated headers file |
| `headers` | Array | Other headers to be added to the file (optional) |
| `include` | String | Only include 'css', 'js' or 'all' (default: 'all') |

## Example

The following config

```js
const HtmlWebpackPlugin = require("html-webpack-plugin");
const NetlifyServerPushPlugin = require("netlify-push-webpack-plugin");

module.exports = {
plugins: [
new HtmlWebpackPlugin(),
Expand All @@ -39,8 +43,28 @@ module.exports = {
"/assets/*",
" Cache-Control: public, max-age:360000"
],
include: 'css'
include: "css"
})
]
};
```

will result in a headers file looking something like this:

```
/*
Link: <bundle.js>; rel=preload; as=script
Link: <main.css>; rel=preload; as=style
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
/assets/*
Cache-Control: public, max-age:360000
```

## Testing

Tests are ran using using Ava with the following command:

```
yarn run test
```
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ class NetlifyPushWebpackPlugin {
this.options = options;
}

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

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

if(include === "all")
return ["/*", ...scripts, ...styles, ...headers].join("\n")
if(include === "js")
return ["/*", ...scripts, ...headers].join("\n")
if(include === "css")
return ["/*", ...styles, ...headers].join("\n")
return ""
}

apply(compiler) {
Expand Down
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "netlify-push-webpack-plugin",
"version": "1.0.0",
"version": "1.1.0",
"description": "Generate HTTP2 Server Push headers for Netlify using HtmlWebpackPlugin",
"main": "index.js",
"files": [
Expand All @@ -17,14 +17,25 @@
"netlify",
"push"
],
"scripts": {
"test": "ava tests/test.js"
},
"author": "Leon Koole <[email protected]> (https://github.com/koole)",
"license": "MIT",
"bugs": {
"url": "https://github.com/koole/netlify-push-webpack-plugin/issues"
},
"homepage": "https://github.com/koole/netlify-push-webpack-plugin",
"peerDependencies": {
"html-webpack-plugin": "^4.0.0-alpha.2",
"html-webpack-plugin": "^4.0.0-beta.5",
"webpack": "^4.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"css-loader": "^2.1.1",
"html-webpack-plugin": "^4.0.0-beta.5",
"mini-css-extract-plugin": "^0.6.0",
"rimraf": "^2.6.3",
"webpack": "^4.0.0"
}
}
}
3 changes: 3 additions & 0 deletions tests/all/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
Link: <bundle.js>; rel=preload; as=script
Link: <main.css>; rel=preload; as=style
39 changes: 39 additions & 0 deletions tests/all/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const NetlifyServerPushPlugin = require("../../index.js");

module.exports = {
entry: "./tests/project/index.js",
output: {
path: __dirname + "/dist",
filename: "bundle.js"
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it uses publicPath in webpackOptions.output
publicPath: "../",
hmr: process.env.NODE_ENV === "development"
}
},
"css-loader"
]
}
]
},
plugins: [
new MiniCssExtractPlugin(),
new HtmlWebpackPlugin(),
new NetlifyServerPushPlugin({
filename: "_headers",
include: "all"
})
]
};
2 changes: 2 additions & 0 deletions tests/css/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*
Link: <main.css>; rel=preload; as=style
40 changes: 40 additions & 0 deletions tests/css/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const NetlifyServerPushPlugin = require("../../index.js");

module.exports = {
entry: "./tests/project/index.js",
output: {
path: __dirname + "/dist",
filename: "bundle.js"
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it uses publicPath in webpackOptions.output
publicPath: "../",
hmr: process.env.NODE_ENV === "development"
}
},
"css-loader"
]
}
]
},
plugins: [
new MiniCssExtractPlugin(),
new HtmlWebpackPlugin(),
new NetlifyServerPushPlugin({
filename: "_headers",
headers: [],
include: "css"
})
]
};
7 changes: 7 additions & 0 deletions tests/custom_headers/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Link: <bundle.js>; rel=preload; as=script
Link: <main.css>; rel=preload; as=style
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
/assets/*
Cache-Control: public, max-age:360000
45 changes: 45 additions & 0 deletions tests/custom_headers/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const NetlifyServerPushPlugin = require("../../index.js");

module.exports = {
entry: "./tests/project/index.js",
output: {
path: __dirname + "/dist",
filename: "bundle.js"
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it uses publicPath in webpackOptions.output
publicPath: "../",
hmr: process.env.NODE_ENV === "development"
}
},
"css-loader"
]
}
]
},
plugins: [
new MiniCssExtractPlugin(),
new HtmlWebpackPlugin(),
new NetlifyServerPushPlugin({
filename: "_headers",
headers: [
" X-Frame-Options: DENY",
" Referrer-Policy: strict-origin-when-cross-origin",
"/assets/*",
" Cache-Control: public, max-age:360000"
],
include: "all"
})
]
};
2 changes: 2 additions & 0 deletions tests/js/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*
Link: <bundle.js>; rel=preload; as=script
40 changes: 40 additions & 0 deletions tests/js/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const NetlifyServerPushPlugin = require("../../index.js");

module.exports = {
entry: "./tests/project/index.js",
output: {
path: __dirname + "/dist",
filename: "bundle.js"
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it uses publicPath in webpackOptions.output
publicPath: "../",
hmr: process.env.NODE_ENV === "development"
}
},
"css-loader"
]
}
]
},
plugins: [
new MiniCssExtractPlugin(),
new HtmlWebpackPlugin(),
new NetlifyServerPushPlugin({
filename: "_headers",
headers: [],
include: "js"
})
]
};
3 changes: 3 additions & 0 deletions tests/project/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "./style.css";

console.log("Test")
3 changes: 3 additions & 0 deletions tests/project/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: #000;
}
Loading

0 comments on commit b6fc2fa

Please sign in to comment.