Skip to content

Commit 479e4f2

Browse files
committed
Initial commit 👋
0 parents  commit 479e4f2

File tree

6 files changed

+2506
-0
lines changed

6 files changed

+2506
-0
lines changed

.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2017, Vinay Puppal.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# preact-cli-plugin-netlify
2+
3+
Preact cli plugin for generating h2push headers and redirect(for SPA) rules for [netlify](https://www.netlify.com/)
4+
5+
[![NPM version](https://img.shields.io/npm/v/preact-cli-plugin-netlify.svg)](https://www.npmjs.com/package/preact-cli-plugin-netlify)
6+
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
7+
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
8+
9+
## Installation
10+
11+
```bash
12+
yarn add preact-cli-plugin-netlify --dev
13+
```
14+
15+
Alternatively using npm:
16+
17+
```bash
18+
npm i preact-cli-plugin-netlify --save-dev
19+
```
20+
21+
## Usage
22+
23+
And include in your project by creating a `preact.config.js`:
24+
25+
```js
26+
const netlifyPlugin = require('preact-cli-plugin-netlify');
27+
28+
export default function (config) {
29+
netlifyPlugin(config);
30+
}
31+
```
32+
33+
## Generated files
34+
35+
This plugin genererates `_headers` and `_redirects` files inside build folder
36+
37+
### Example of genererated files
38+
39+
```txt
40+
# _headers
41+
/*
42+
Cache-Control: public, max-age=3600, no-cache
43+
Access-Control-Max-Age: 600
44+
/sw.js
45+
Cache-Control: private, no-cache
46+
/*.chunk.*.js
47+
Cache-Control: public, max-age=31536000
48+
/
49+
Link: </style.4f36e.css>; rel=preload; as=style
50+
Link: </bundle.10e55.js>; rel=preload; as=script
51+
Link: </route-home.chunk.47478.js>; rel=preload; as=script
52+
/profile
53+
Link: </style.4f36e.css>; rel=preload; as=style
54+
Link: </bundle.10e55.js>; rel=preload; as=script
55+
Link: </route-profile.chunk.e4eea.js>; rel=preload; as=script
56+
```
57+
58+
```txt
59+
# _redirects
60+
/* /index.html 200
61+
```
62+
63+
You can verify these rules at [netlify playground](https://play.netlify.com/)
64+
65+
## Deploying to netlify
66+
67+
- Install netlify cli
68+
```bash
69+
npm install netlify-cli -g
70+
```
71+
- Deploy build folder
72+
```bash
73+
netlify deploy -p build
74+
```
75+
76+
## License
77+
78+
MIT © [VinayPuppal](https://www.vinaypuppal.com)

index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Generates Netlify HTTP2 Server Push headers and redirect rules for preact-cli
3+
* A lot of this code is borrowed from https://github.com/developit/preact-cli/blob/master/src/lib/webpack/push-manifest.js
4+
*/
5+
6+
class NetlifyServerPushPlugin {
7+
apply(compiler) {
8+
compiler.plugin('emit', (compilation, callback) => {
9+
const routes = [];
10+
let mainJs;
11+
let mainCss;
12+
13+
for (const filename in compilation.assets) {
14+
if (!/\.map$/.test(filename)) {
15+
if (/route-/.test(filename)) {
16+
routes.push(filename);
17+
} else if (/^style(.+)\.css$/.test(filename)) {
18+
mainCss = `Link: </${filename}>; rel=preload; as=style`;
19+
} else if (/^bundle(.+)\.js$/.test(filename)) {
20+
mainJs = `Link: </${filename}>; rel=preload; as=script`;
21+
}
22+
}
23+
}
24+
25+
let headers =
26+
'/*\n\tCache-Control: public, max-age=3600, no-cache\n\tAccess-Control-Max-Age: 600\n/sw.js\n\tCache-Control: private, no-cache\n/*.chunk.*.js\n\tCache-Control: public, max-age=31536000';
27+
28+
const redirects = `/* /index.html 200`;
29+
30+
routes.forEach(filename => {
31+
const path = filename
32+
.replace(/route-/, '/')
33+
.replace(/\.chunk(\.\w+)?\.js$/, '')
34+
.replace(/\/home/, '/');
35+
const routeJs = `Link: </${filename}>; rel=preload; as=script`;
36+
headers = `${headers}\n${path}\n\t${mainCss}\n\t${mainJs}\n\t${routeJs}`;
37+
});
38+
39+
compilation.assets._headers = {
40+
source() {
41+
return headers;
42+
},
43+
size() {
44+
return headers.length;
45+
},
46+
};
47+
48+
compilation.assets._redirects = {
49+
source() {
50+
return redirects;
51+
},
52+
size() {
53+
return redirects.length;
54+
},
55+
};
56+
57+
callback();
58+
});
59+
}
60+
}
61+
62+
module.exports = function(config) {
63+
if (!config || !config.plugins) {
64+
throw new Error('You need to pass the webpack config to preact-cli-plugin-netlify!');
65+
}
66+
config.plugins.push(new NetlifyServerPushPlugin());
67+
const plugins = config.plugins;
68+
for (let pluginIndex = 0; pluginIndex < plugins.length; pluginIndex++) {
69+
const plugin = plugins[pluginIndex];
70+
if (plugin && plugin.options && plugin.options.cacheId) {
71+
// Ignore genearted _headers and _redirects files from SW precaching
72+
Object.assign(plugin.options, {
73+
staticFileGlobsIgnorePatterns: [
74+
...plugin.options.staticFileGlobsIgnorePatterns,
75+
/_headers/,
76+
/_redirects/,
77+
],
78+
});
79+
}
80+
}
81+
};

package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "preact-cli-plugin-netlify",
3+
"version": "1.0.0",
4+
"description": "Preact cli plugin for generating h2push headers and redirect rules for netlify",
5+
"main": "index.js",
6+
"scripts": {
7+
"prepublish": "npm run lint",
8+
"precommit": "lint-staged",
9+
"lint": "xo",
10+
"test": "npm run lint"
11+
},
12+
"keywords": ["preact", "plugin", "netlify", "h2push"],
13+
"author": "Vinay Puppal <me@vinaypuppal.com> (https://www.vinaypuppal.com/)",
14+
"license": "MIT",
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/vinaypuppal/preact-cli-plugin-netlify.git"
18+
},
19+
"bugs": {
20+
"url": "https://github.com/vinaypuppal/preact-cli-plugin-netlify/issues"
21+
},
22+
"homepage": "https://github.com/vinaypuppal/preact-cli-plugin-netlify#readme",
23+
"devDependencies": {
24+
"babel-eslint": "^8.0.2",
25+
"eslint-config-prettier": "^2.7.0",
26+
"husky": "^0.14.3",
27+
"lint-staged": "^4.3.0",
28+
"prettier": "^1.8.2",
29+
"xo": "^0.18.2"
30+
},
31+
"xo": {
32+
"parser": "babel-eslint",
33+
"extends": ["prettier"],
34+
"env": ["node"],
35+
"rules": {
36+
"linebreak-style": 0
37+
},
38+
"space:": 2
39+
},
40+
"lint-staged": {
41+
"*.js": [
42+
"prettier --write --single-quote --print-width=80 --trailing-comma=all",
43+
"xo",
44+
"git add"
45+
]
46+
}
47+
}

0 commit comments

Comments
 (0)