├── .gitignore ├── .prettierrc ├── LICENSE ├── README.md ├── gatsby-browser.js ├── gatsby-node.js ├── gatsby-ssr.js ├── index.js ├── package.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "endOfLine": "lf", 3 | "semi": false, 4 | "singleQuote": true, 5 | "tabWidth": 2, 6 | "printWidth": 80 7 | } 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 James Vickery 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gatsby-plugin-prettier-build 2 | 3 | prettify gatsby build output 4 | 5 | [![npm version](https://badge.fury.io/js/gatsby-plugin-prettier-build.svg)](https://badge.fury.io/js/gatsby-plugin-prettier-build) 6 | 7 | ## Why? 8 | 9 | [![wesbos-tweet](https://user-images.githubusercontent.com/14852491/78827410-02ccdb00-79db-11ea-9369-74c0ada99cf4.png)](https://twitter.com/wesbos/status/1247903517051768839) 10 | 11 | ## Install 12 | 13 | ```bash 14 | npm install --save gatsby-plugin-prettier-build 15 | # or 16 | yarn add gatsby-plugin-prettier-build 17 | ``` 18 | 19 | ## Usage 20 | 21 | In `gatsby-config.js` plugins array: 22 | 23 | To stick to default options (see defaults below) add: 24 | 25 | 26 | ```js 27 | `gatsby-plugin-prettier-build` 28 | ``` 29 | 30 | Or to provide custom options: 31 | 32 | ```js 33 | { 34 | resolve: `gatsby-plugin-prettier-build`, 35 | options: { 36 | // default values 37 | types: ['html'], 38 | concurrency: 20, 39 | verbose: true 40 | } 41 | } 42 | ``` 43 | 44 | ### Options 45 | 46 | #### `types` 47 | 48 | > default: `['html']` 49 | 50 | Array of filetypes to be prettified after build. Currently supports: 51 | 52 | - `js` 53 | - `html` 54 | - `css` 55 | 56 | For example, to prettify all supported types: `['html', 'js', 'css']` 57 | 58 | #### `verbose` 59 | 60 | > default: `true` 61 | 62 | Whether or not to log progress to the console 63 | 64 | #### `concurrency` 65 | 66 | > default: `20` 67 | 68 | How many files to prettify at once 69 | 70 | You can remove the concurrency limit with `concurrency: Infinity` although this isn't recommended 71 | 72 | ## Changelog 73 | 74 | #### `0.4.4` 75 | 76 | - Stop plugin from breaking on prettier failure 77 | 78 | #### `0.4.3` 79 | 80 | - Add `gatsby-plugin` to package keywords [#2](https://github.com/jmsv/gatsby-plugin-prettier-build/pull/2) (thanks [@KyleAMathews](https://github.com/KyleAMathews)) 81 | - Minor docs fix 82 | 83 | #### `0.4.2` 84 | 85 | - Fix bug where undefined `types` option throws error on `onPreInit` options validation 86 | 87 | #### `0.4.1` 88 | 89 | - Docs fix 90 | 91 | #### `0.4.0` 92 | 93 | - Limit prettify concurrency with new `concurrency` option (defaults to `20`) 94 | - Fix issue with counting number of prettified files 95 | 96 | #### `0.3.0` 97 | 98 | - Prettify files asynchronously with `Promise.all` 99 | 100 | #### `0.2.0` 101 | 102 | - Replace `glob` with `tiny-glob` (smaller and faster) 103 | - Validate options on `onPreInit` to avoid hitting config problems post-build (saves time if there's a predictable problem) 104 | - `fs.stat` filepaths to make sure they're files - avoids trying to read directories (`page-data` etc.) as files 105 | 106 | #### `0.1.0` 107 | 108 | Initial version 109 | -------------------------------------------------------------------------------- /gatsby-browser.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsv/gatsby-plugin-prettier-build/c194bf7fe2543cb93b149c92d8a65dc0dcb3651b/gatsby-browser.js -------------------------------------------------------------------------------- /gatsby-node.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs').promises 2 | const path = require('path') 3 | const prettier = require('prettier') 4 | const glob = require('tiny-glob') 5 | const pLimit = require('p-limit') 6 | 7 | const extParsers = { 8 | js: 'babel', 9 | html: 'html', 10 | css: 'css', 11 | } 12 | 13 | exports.onPreInit = (_, opts = {}) => { 14 | return new Promise((resolve, reject) => { 15 | if (!opts.types) return resolve() 16 | 17 | for (const type of opts.types) { 18 | if (!extParsers[type]) 19 | return reject( 20 | `gatsby-plugin-prettier-build doesn't support '${type}' files\n` + 21 | `raise an issue? https://github.com/jmsv/gatsby-plugin-prettier-build/issues/new` 22 | ) 23 | } 24 | 25 | return resolve() 26 | }) 27 | } 28 | 29 | exports.onPostBuild = async (_, opts = {}) => { 30 | const fileTypesToFormat = opts.types || ['html'] 31 | const verbose = opts.verbose !== false 32 | 33 | const [prettierOpts, files] = await Promise.all([ 34 | prettier.resolveConfig(process.cwd()), 35 | glob(`public/**/*.{${fileTypesToFormat.join(',')}}`), 36 | ]) 37 | 38 | const limit = pLimit(opts.concurrency || 20) 39 | let filesPrettified = 0 40 | 41 | return Promise.all( 42 | files.map((filePath) => 43 | limit(() => 44 | prettifyFile(filePath, prettierOpts).then((done) => { 45 | if (done === true) { 46 | filesPrettified += 1 47 | if (verbose) console.log('✔ prettified', filePath) 48 | } else if (done === false) { 49 | if (verbose) console.log('✘ failed to prettify', filePath) 50 | } 51 | }) 52 | ) 53 | ) 54 | ).then(() => { 55 | if (verbose) { 56 | console.log( 57 | `✨ finished prettifying ${filesPrettified} Gatsby build file${ 58 | filesPrettified ? 's' : '' 59 | }` 60 | ) 61 | } 62 | }) 63 | } 64 | 65 | const prettifyFile = async (filePath, prettierOpts) => { 66 | // Don't attempt format if not a file 67 | if (!(await fs.lstat(filePath)).isFile()) return null 68 | 69 | const parser = extParsers[path.extname(filePath).slice(1)] 70 | 71 | const fileBuffer = await fs.readFile(filePath) 72 | 73 | try { 74 | const formatted = prettier.format( 75 | fileBuffer.toString(), 76 | Object.assign({ parser }, prettierOpts) 77 | ) 78 | 79 | await fs.writeFile(filePath, formatted) 80 | } catch { 81 | return false 82 | } 83 | 84 | return true 85 | } 86 | -------------------------------------------------------------------------------- /gatsby-ssr.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsv/gatsby-plugin-prettier-build/c194bf7fe2543cb93b149c92d8a65dc0dcb3651b/gatsby-ssr.js -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // noop 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gatsby-plugin-prettier-build", 3 | "version": "0.4.4", 4 | "description": "prettify gatsby build output", 5 | "main": "index.js", 6 | "scripts": { 7 | "format": "prettier --write *.js", 8 | "postversion": "git push && git push --tags", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/jmsv/gatsby-plugin-prettier-build.git" 14 | }, 15 | "keywords": [ 16 | "prettier", 17 | "gatsby", 18 | "gatsbyjs", 19 | "plugin", 20 | "gatsby-plugin" 21 | ], 22 | "author": { 23 | "name": "James Vickery", 24 | "email": "dev@jmsv.me", 25 | "url": "https://jmsv.dev" 26 | }, 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/jmsv/gatsby-plugin-prettier-build/issues" 30 | }, 31 | "homepage": "https://github.com/jmsv/gatsby-plugin-prettier-build#readme", 32 | "dependencies": { 33 | "p-limit": "^2.3.0", 34 | "prettier": "^2.0.4", 35 | "tiny-glob": "^0.2.6" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | globalyzer@^0.1.0: 6 | version "0.1.4" 7 | resolved "https://registry.yarnpkg.com/globalyzer/-/globalyzer-0.1.4.tgz#bc8e273afe1ac7c24eea8def5b802340c5cc534f" 8 | integrity sha512-LeguVWaxgHN0MNbWC6YljNMzHkrCny9fzjmEUdnF1kQ7wATFD1RHFRqA1qxaX2tgxGENlcxjOflopBwj3YZiXA== 9 | 10 | globrex@^0.1.1: 11 | version "0.1.2" 12 | resolved "https://registry.yarnpkg.com/globrex/-/globrex-0.1.2.tgz#dd5d9ec826232730cd6793a5e33a9302985e6098" 13 | integrity sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg== 14 | 15 | p-limit@^2.3.0: 16 | version "2.3.0" 17 | resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.3.0.tgz#3dd33c647a214fdfffd835933eb086da0dc21db1" 18 | integrity sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w== 19 | dependencies: 20 | p-try "^2.0.0" 21 | 22 | p-try@^2.0.0: 23 | version "2.2.0" 24 | resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6" 25 | integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ== 26 | 27 | prettier@^2.0.4: 28 | version "2.0.4" 29 | resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.4.tgz#2d1bae173e355996ee355ec9830a7a1ee05457ef" 30 | integrity sha512-SVJIQ51spzFDvh4fIbCLvciiDMCrRhlN3mbZvv/+ycjvmF5E73bKdGfU8QDLNmjYJf+lsGnDBC4UUnvTe5OO0w== 31 | 32 | tiny-glob@^0.2.6: 33 | version "0.2.6" 34 | resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.6.tgz#9e056e169d9788fe8a734dfa1ff02e9b92ed7eda" 35 | integrity sha512-A7ewMqPu1B5PWwC3m7KVgAu96Ch5LA0w4SnEN/LbDREj/gAD0nPWboRbn8YoP9ISZXqeNAlMvKSKoEuhcfK3Pw== 36 | dependencies: 37 | globalyzer "^0.1.0" 38 | globrex "^0.1.1" 39 | --------------------------------------------------------------------------------