diff --git a/packages/gatsby-compression-plugin/.gitignore b/packages/gatsby-compression-plugin/.gitignore new file mode 100644 index 0000000000000..9ba488b6e0ef8 --- /dev/null +++ b/packages/gatsby-compression-plugin/.gitignore @@ -0,0 +1,2 @@ +*.DS_Store +node_modules/ diff --git a/packages/gatsby-compression-plugin/.npmignore b/packages/gatsby-compression-plugin/.npmignore new file mode 100644 index 0000000000000..e2929efae0b6b --- /dev/null +++ b/packages/gatsby-compression-plugin/.npmignore @@ -0,0 +1,2 @@ +src +.gitignore diff --git a/packages/gatsby-compression-plugin/LICENSE b/packages/gatsby-compression-plugin/LICENSE new file mode 100644 index 0000000000000..d339433d0b54d --- /dev/null +++ b/packages/gatsby-compression-plugin/LICENSE @@ -0,0 +1,5 @@ +Copyright 2017 Harrison C Bowden, Minneapolis, Minnesota + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/packages/gatsby-compression-plugin/README.md b/packages/gatsby-compression-plugin/README.md new file mode 100644 index 0000000000000..d156cfeab53b3 --- /dev/null +++ b/packages/gatsby-compression-plugin/README.md @@ -0,0 +1,53 @@ +# Gatsby plugin compression + +First install `gatsby-plugin-compression`. + +```bash + +npm install gatsby-plugin-compression --save + +``` + +Then add `gatsby-plugin-compression` to your app's `gatsby-config.js`. + +```javascript + +module.exports = { + plugins: [ + `gatsby-plugin-compression`, + ], +} + +``` + +That's it, when you build your app you will have gzipped versions of your files. + +# Nginx + +If your using nginx you can use `gzip_static on;` to serve your gzipped assets, here's a full example. + +```bash + +#user nobody; +worker_processes 1; + +events { + worker_connections 1024; +} + +pid /usr/local/nginx/logs/nginx.pid; + +server { + listen 80; + server_name localhost; + + #access_log logs/host.access.log main; + + location / { + gzip_static on; + root Users/nah/Desktop/web; + index index.html index.htm; + } +} + +``` diff --git a/packages/gatsby-compression-plugin/package.json b/packages/gatsby-compression-plugin/package.json new file mode 100644 index 0000000000000..4af24b1cf1174 --- /dev/null +++ b/packages/gatsby-compression-plugin/package.json @@ -0,0 +1,19 @@ +{ + "name": "gatsby-plugin-compression", + "version": "0.0.1", + "description": "Gatsby plugin for compressing assets.", + "main": "gatsby-node.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "gatsby", + "plugin", + "compression" + ], + "author": "Harrison Bowden", + "license": "ISC", + "dependencies": { + "compression-webpack-plugin": "^1.0.1" + } +} diff --git a/packages/gatsby-compression-plugin/src/gatsby-node.js b/packages/gatsby-compression-plugin/src/gatsby-node.js new file mode 100644 index 0000000000000..4d9506ca6e6da --- /dev/null +++ b/packages/gatsby-compression-plugin/src/gatsby-node.js @@ -0,0 +1,18 @@ +const CompressionPlugin = require(`compression-webpack-plugin`) + +exports.modifyWebpackConfig = ({ config, stage }, options) => { + switch (stage) { + case `build-html`: + case `build-javascript`: + case `build-css`: { + config.plugin(`compression`, + CompressionPlugin, + [{ asset: `[path].gz[query]`, algorithm: `gzip` }] + ) + return config + } + default: { + return config + } + } +}