├── .gitignore ├── .travis.yml ├── package.json ├── LICENSE ├── README.md ├── index.js ├── CONTRIBUTING.md ├── CODE_OF_CONDUCT.md └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | cache: 5 | directories: 6 | - node_modules 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-preset-github", 3 | "version": "3.2.1", 4 | "description": "GitHub.com's Babel configuration", 5 | "main": "index.js", 6 | "files": [], 7 | "dependencies": { 8 | "@babel/plugin-proposal-class-properties": "^7.1.0", 9 | "@babel/plugin-proposal-object-rest-spread": "^7.3.4", 10 | "@babel/plugin-syntax-dynamic-import": "^7.0.0", 11 | "@babel/plugin-syntax-import-meta": "^7.0.0", 12 | "@babel/plugin-transform-flow-strip-types": "^7.0.0", 13 | "@babel/preset-env": "^7.2.3", 14 | "babel-plugin-transform-invariant-location": "^1.0.0", 15 | "browserslist": "^4.4.2" 16 | }, 17 | "devDependencies": { 18 | "@babel/core": "^7.1.5" 19 | }, 20 | "repository": { 21 | "type": "git", 22 | "url": "git+https://github.com/github/babel-preset-github.git" 23 | }, 24 | "scripts": { 25 | "test": "node test.js" 26 | }, 27 | "author": "GitHub Inc", 28 | "license": "MIT" 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2018 GitHub Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-preset-github 2 | 3 | This is a Babel preset which GitHub.com uses to transpile our JavaScript. It supports ES2018, plus a few extra non-standard features which we use to make development easier. 4 | 5 | Pull requests that fix bugs are welcome. We will likely only add new plugins as they are needed for the GitHub.com codebase. 6 | 7 | 8 | To install you need node and npm: 9 | 10 | ```bash 11 | $ npm i -D babel-preset-github 12 | ``` 13 | 14 | Then use the preset in your `.babelrc` 15 | 16 | ```json 17 | { 18 | "presets": ["github"] 19 | } 20 | ``` 21 | 22 | This example only includes the polyfills and code transforms needed for a specific list of browsers that GitHub supports. You can change this by passing in a valid browserlist: 23 | 24 | ```json 25 | { 26 | "presets": [ 27 | ["github", { 28 | "targets": { 29 | "browsers": ["last 2 versions", "safari >= 7"] 30 | } 31 | }] 32 | ] 33 | } 34 | ``` 35 | 36 | ### LICENSE 37 | 38 | The project is available as open source under the terms of the [MIT License](LICENSE). 39 | 40 | When using the GitHub logos, be sure to follow the [GitHub logo guidelines](https://github.com/logos). 41 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const defaultBrowsers = [ 2 | 'last 8 Chrome versions', 3 | 'last 4 Firefox versions', 4 | 'last 3 Safari versions', 5 | 'last 4 Edge versions', 6 | 'Firefox ESR', 7 | 'Chrome >= 61' 8 | ] 9 | const mobileBrowsers = [ 10 | 'Chrome >= 55', 11 | 'Firefox >= 63', 12 | 'Safari >= 10.3', 13 | 'Edge >= 15', 14 | 'Opera >= 42' 15 | ] 16 | const browserslist = require('browserslist') 17 | module.exports = function babelPresetGitHub(api, { useBuiltIns = false, env = true, modules = false, targets = {} }) { 18 | targets = Object.assign({}, { browsers: defaultBrowsers }, targets) 19 | if (targets.browsers === 'mobile') targets.browsers = mobileBrowsers 20 | if (targets.browsers === 'default') targets.browsers = defaultBrowsers 21 | const presets = [] 22 | const plugins = [ 23 | // ES2019 24 | // Stage 3 with good signals for Stage 4 25 | // Chrome 64+, Firefox 62+, Safari TP42, Opera 51+ 26 | require('@babel/plugin-syntax-import-meta').default, 27 | // Chrome 63+, Firefox (https://mzl.la/2LMSnOf), Safari TP25, Opera 50+ 28 | require('@babel/plugin-syntax-dynamic-import').default, 29 | // Non-standard 30 | require('@babel/plugin-transform-flow-strip-types').default, 31 | // Custom 32 | require('babel-plugin-transform-invariant-location'), 33 | ] 34 | const fullBrowsers = browserslist(targets.browsers) 35 | const needsRestSpread = browserslist(['Edge > 0', 'Safari < 11.1']).some(browser => fullBrowsers.includes(browser)) 36 | if (env) { 37 | presets.push([require('@babel/preset-env').default, { modules, targets, useBuiltIns: useBuiltIns ? 'entry' : false }]) 38 | } else if (needsRestSpread) { 39 | plugins.push([require('@babel/plugin-proposal-object-rest-spread'), { useBuiltIns }]) 40 | } 41 | return { plugins, presets }; 42 | }; 43 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | [fork]: https://github.com/github/babel-preset-github/fork 4 | [pr]: https://github.com/github/babel-preset-github/compare 5 | [style]: https://styleguide.github.com/js/ 6 | [code-of-conduct]: CODE_OF_CONDUCT.md 7 | 8 | Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great. 9 | 10 | Contributions to this project are [released](https://help.github.com/articles/github-terms-of-service/#6-contributions-under-repository-license) to the public under the [project's open source license](LICENSE.md). 11 | 12 | Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms. 13 | 14 | ## Submitting a pull request 15 | 16 | 0. [Fork][fork] and clone the repository 17 | 0. Configure and install the dependencies: `npm i` 18 | 0. Create a new branch: `git checkout -b my-branch-name` 19 | 0. Make your change, add tests, and make sure the tests still pass 20 | 0. Push to your fork and [submit a pull request][pr] 21 | 0. Pat your self on the back and wait for your pull request to be reviewed and merged. 22 | 23 | Here are a few things you can do that will increase the likelihood of your pull request being accepted: 24 | 25 | - Follow the [style guide][style]. 26 | - Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests. 27 | - Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html). 28 | 29 | ## Resources 30 | 31 | - [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) 32 | - [Using Pull Requests](https://help.github.com/articles/about-pull-requests/) 33 | - [GitHub Help](https://help.github.com) 34 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at opensource+babel-preset-github@github.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 39 | 40 | ## Attribution 41 | 42 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 43 | 44 | [homepage]: http://contributor-covenant.org 45 | [version]: http://contributor-covenant.org/version/1/4/ 46 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const {loadOptions, transformSync} = require('@babel/core') 2 | const fail = process.stdout.isTTY ? '\x1b[0;31mx\x1b[0m' : 'x' 3 | const pass = process.stdout.isTTY ? '\x1b[0;32m✔\x1b[0m' : '✔' 4 | let exit = 0 5 | 6 | const test = (name, pre, expected, options = { presets: ["./"] }) => { 7 | try { 8 | const actual = transformSync(pre, options).code 9 | if (expected instanceof RegExp) { 10 | if (!expected.test(actual)) throw new Error(`No match ${expected}:\n ${actual}`) 11 | } else if (typeof expected === 'string') { 12 | if (actual !== expected) throw new Error(`Actual: \`\`\`\n${actual}\n\`\`\`\nExpected: \`\`\`\n${expected}\n\`\`\``) 13 | } else { 14 | throw new Error(`Actual: \`\`\`\n${actual}\n\`\`\``) 15 | } 16 | console.log(`${pass} ${name}`) 17 | } catch (e) { 18 | console.log(`${fail} ${name}`) 19 | console.log(e.stack) 20 | exit = 1 21 | } 22 | } 23 | const testFail = (name, pre, ctor, options = { presets: ["./"] }) => { 24 | try { 25 | const actual = transformSync(pre, options).code 26 | const err = new Error(`Actual: \`\`\`\n${actual}\n\`\`\`\n`) 27 | console.log(`${fail} ${name}`) 28 | console.log(`Expected transform to fail with ${ctor.name}`) 29 | console.log(err.stack) 30 | exit = 1 31 | } catch (err) { 32 | if (!(err instanceof ctor)) { 33 | console.log(`${fail} ${name}`) 34 | console.log(`Expected ${ctor.name} but got ${err.constructor.name}`) 35 | exit = 1 36 | } else { 37 | console.log(`${pass} ${name}`) 38 | } 39 | } 40 | } 41 | 42 | test( 43 | 'json-strings works', 44 | `"a
b"`, 45 | `"a\\u2028b";` 46 | ) 47 | 48 | test( 49 | 'json-strings transform is disabled with env=false', 50 | `"a
b"`, 51 | `"a
b";`, 52 | { presets: [["./", { env: false }]]} 53 | ) 54 | 55 | test( 56 | 'optional-catch works', 57 | `try { throw '' } catch {}`, 58 | `try { 59 | throw ''; 60 | } catch (_unused) {}`, 61 | ) 62 | 63 | test( 64 | 'optional-catch transform is disabled with env=false', 65 | `try { throw '' } catch {}`, 66 | `try { 67 | throw ''; 68 | } catch {}`, 69 | { presets: [["./", { env: false }]]} 70 | ) 71 | 72 | test( 73 | 'destructuring works', 74 | `const { f } = { f: 1 }`, 75 | `const _f = { 76 | f: 1 77 | }, 78 | f = _f.f;` 79 | ) 80 | test( 81 | 'destructuring transform is disabled with env=false', 82 | `const { f } = { f: 1 }`, 83 | `const { 84 | f 85 | } = { 86 | f: 1 87 | };`, 88 | { presets: [["./", { env: false }]]} 89 | ) 90 | test( 91 | 'import.meta works', 92 | `import.meta.foo`, 93 | `import.meta.foo;` 94 | ) 95 | 96 | test( 97 | 'object rest is transformed', 98 | `const { ...f } = {a:1}`, 99 | /f = _extends\({}, / 100 | ) 101 | 102 | test( 103 | 'object rest is transformed with env=false', 104 | `const { ...f } = {a:1}`, 105 | /f = _extends\({}, /, 106 | { presets: [["./", { env: false }]]} 107 | ) 108 | 109 | test( 110 | 'object rest is transformed with mobile browsers', 111 | `const { ...f } = {a:1}`, 112 | /f = _extends\({}, /, 113 | { presets: [["./", { targets: { browsers: 'mobile' } }]]} 114 | ) 115 | 116 | test( 117 | 'object rest is transformed with mobile browsers even with env=false', 118 | `const { ...f } = {a:1}`, 119 | /f = _extends\({}, /, 120 | { presets: [["./", { env: false, targets: { browsers: 'mobile' } }]]} 121 | ) 122 | 123 | test( 124 | 'object rest will use builtins if useBuiltIns=true', 125 | `const { ...f } = {a:1}`, 126 | /f = Object.assign/, 127 | { presets: [["./", { useBuiltIns: true }]]} 128 | ) 129 | 130 | test( 131 | 'object rest will use builtins if useBuiltIns=true with env=false', 132 | `const { ...f } = {a:1}`, 133 | /f = Object.assign/, 134 | { presets: [["./", { env: false, useBuiltIns: true }]]} 135 | ) 136 | 137 | test( 138 | 'object rest wont be transformed given modern browser set', 139 | `const { ...f } = {a:1}`, 140 | `const { ...f\n} = {\n a: 1\n};`, 141 | { presets: [["./", { targets: { browsers: ['Chrome > 60', 'Safari >= 11.1'] }}]]} 142 | ) 143 | 144 | test( 145 | 'object rest wont be transformed given modern browser set even with env=false', 146 | `const { ...f } = {a:1}`, 147 | `const { ...f\n} = {\n a: 1\n};`, 148 | { presets: [["./", { env: false, targets: { browsers: ['Chrome > 60', 'Safari >= 11.1'] }}]]} 149 | ) 150 | 151 | test( 152 | 'object spread is transformed', 153 | `const f = { ...a }`, 154 | /f = _objectSpread\({}, / 155 | ) 156 | 157 | test( 158 | 'object spread still works with env=false', 159 | `const f = { ...a }`, 160 | /f = _objectSpread\({}, /, 161 | { presets: [["./", { env: false }]]} 162 | ) 163 | 164 | test( 165 | 'object spread still works if useBuiltIns=true', 166 | `const f = { ...a }`, 167 | /f = _objectSpread\({}, /, 168 | { presets: [["./", { useBuiltIns: true }]]} 169 | ) 170 | 171 | test( 172 | 'object spread still works if env=false and useBuiltIns=true', 173 | `const f = { ...a }`, 174 | /f = _objectSpread\({}, /, 175 | { presets: [["./", { env: false, useBuiltIns: true }]]} 176 | ) 177 | 178 | test( 179 | 'object spread wont be transformed given modern browser set', 180 | `const f = { ...a }`, 181 | `const f = { ...a\n};`, 182 | { presets: [["./", { targets: { browsers: ['Chrome > 60', 'Safari >= 11.1'] }}]]} 183 | ) 184 | 185 | test( 186 | 'object spread wont be transformed given modern browser set even with env=false', 187 | `const f = { ...a }`, 188 | `const f = { ...a\n};`, 189 | { presets: [["./", { env: false, targets: { browsers: ['Chrome > 60', 'Safari >= 11.1'] }}]]} 190 | ) 191 | 192 | test( 193 | 'dynamic import works', 194 | `import('foo')`, 195 | `import('foo');` 196 | ) 197 | 198 | test( 199 | 'flow gets stripped', 200 | `var a: string = 'hello'`, 201 | `var a = 'hello';` 202 | ) 203 | 204 | test( 205 | 'babel-plugin-transform-invariant works', 206 | `invariant(false)`, 207 | `invariant(false, "null.js:1");` 208 | ) 209 | 210 | test( 211 | 'Imports are not transformed', 212 | `import foo from "bar"`, 213 | `import foo from "bar";` 214 | ) 215 | 216 | test( 217 | 'Imports can be transformed setting `modules`', 218 | `import {foo} from "bar"`, 219 | `"use strict"; 220 | 221 | var _bar = require("bar");`, 222 | { presets: [["./", { modules: 'commonjs' }]]} 223 | ) 224 | 225 | test( 226 | 'classes dont get transformed down on desktop', 227 | `class Foo {}`, 228 | `class Foo {}`, 229 | ) 230 | 231 | test( 232 | 'classes get transformed with custom profile', 233 | `class Foo {}`, 234 | /_classCallCheck/, 235 | { presets: [["./", { targets: { browsers: 'IE 11' } }]]} 236 | ) 237 | 238 | test( 239 | 'classes dont get transformed on mobile', 240 | `class Foo {}`, 241 | `class Foo {}`, 242 | { presets: [["./", { targets: { browsers: 'mobile' } }]]} 243 | ) 244 | 245 | test( 246 | 'async does not get transformed on desktop', 247 | `async function foo() {}`, 248 | `async function foo() {}`, 249 | ) 250 | 251 | test( 252 | 'async gets transformed with custom profile', 253 | `async function foo() {}`, 254 | /asyncGeneratorStep/, 255 | { presets: [["./", { targets: { browsers: 'IE 11' } }]]} 256 | ) 257 | 258 | test( 259 | 'async does not get transformed on mobile', 260 | `async function foo() {}`, 261 | `async function foo() {}`, 262 | { presets: [["./", { targets: { browsers: 'mobile' } }]]} 263 | ) 264 | 265 | testFail( 266 | 'class properties are not supported', 267 | `class Foo { x = 1; }`, 268 | SyntaxError 269 | ) 270 | 271 | testFail( 272 | 'class properties are not supported on mobile', 273 | `class Foo { x = 1; }`, 274 | SyntaxError, 275 | { presets: [["./", { targets: { browsers: 'mobile' } }]]} 276 | ) 277 | 278 | process.exit(exit) 279 | --------------------------------------------------------------------------------