├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── example.json ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | npm-debug.log* 5 | .DS_Store 6 | package-lock.json 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "4" 3 | - "6" 4 | sudo: false 5 | language: node_js 6 | script: "npm run test:cov" 7 | after_script: "npm i -g codecov.io && cat ./coverage/lcov.info | codecov" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 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 | # es2040 [![stability][0]][1] 2 | [![npm version][2]][3] [![downloads][8]][9] [![js-standard-style][10]][11] 3 | 4 | `browserify` transform that compiles a selection of ES6 features to valid ES5: 5 | - `fat arrows` - make inline functions cute-looking 6 | - `template strings` / `tagged templates` - enable DSLs inside of JS 7 | - `const` - using `const` by default makes it easy to spot where values are 8 | being redeclared 9 | - `object destructuring` - `const { a, b } = { a: 1, b: 2 }` 10 | - `array destructuring` - `const [a, b] = [1, 2]` 11 | - `default parameters` - `({ a = 1, b = 2 } = {}) => a` 12 | - `rest parameters` - `(a, b, ...args) => {}` 13 | - `spread literals` - `f(a, b, ...args)` 14 | - `short-hand properties` - `return { a, b }` 15 | - `computed properties` - `return { [a]: b }` 16 | 17 | Because, in hindsight, we can do without most of ES6. 18 | 19 | Forked from [`es2020`](https://github.com/yoshuawuyts/es2020) for those of us with slightly worse vision. 20 | 21 | ## Usage 22 | __Via `package.json` (recommended):__ 23 | ```json 24 | { 25 | "browserify": { 26 | "transform": [ 27 | "es2040" 28 | ] 29 | } 30 | } 31 | ``` 32 | 33 | __Via CLI:__ 34 | ```js 35 | $ browserify client.js -t es2040 36 | ``` 37 | 38 | __Via Node API:__ 39 | ```js 40 | const browserify = require('browserify') 41 | browserify('./client.js') 42 | .transform('es2040') 43 | .bundle() 44 | .pipe(process.stdout) 45 | ``` 46 | 47 | ## FAQ 48 | ### Is this a joke? 49 | Not really. The TC39 does not represent my interests, and the features they 50 | introduce are not useful for the stuff I'm doing. 51 | [I'm](https://github.com/whatwg/streams) 52 | [bloody](https://docs.google.com/presentation/d/1H3E2ToJ8VHgZS8eS6bRv-vg5OksObj5wv6gyzJJwOK0/edit) 53 | [serious](https://github.com/whatwg/loader). A few good things have been 54 | introduced, so that's what we're backporting to older browsers. 55 | 56 | ## Can you not? 57 | If the TC39 had an open standards process perhaps this wasn't needed. But as it 58 | stands they're an unwelcoming club, so I get to poke fun at this situation that 59 | otherwise fills me with sadness. Feel free to poke fun at me too. Or if you're 60 | angry that someone would make fun of _the hard work the TC39 has done_, feel 61 | free to ignore this project. Do whatever, I'm doing the same. 62 | 63 | ## Can you include feature X? 64 | Maybe. Open an issue, make a case and we can discuss it. Just remember that this 65 | project is not democratically governed. 66 | 67 | ## Installation 68 | ```sh 69 | $ npm install es2040 70 | ``` 71 | 72 | ## See Also 73 | - [babel-preset-es2040](https://github.com/ahdinosaur/babel-preset-es2040) 74 | - [es2020](https://github.com/yoshuawuyts/es2020) 75 | 76 | ## License 77 | [MIT](https://tldrlegal.com/license/mit-license) 78 | 79 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 80 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 81 | [2]: https://img.shields.io/npm/v/es2040.svg?style=flat-square 82 | [3]: https://npmjs.org/package/es2040 83 | [4]: https://img.shields.io/travis/ahdinosaur/es2040/master.svg?style=flat-square 84 | [5]: https://travis-ci.org/ahdinosaur/es2040 85 | [6]: https://img.shields.io/codecov/c/github/ahdinosaur/es2040/master.svg?style=flat-square 86 | [7]: https://codecov.io/github/ahdinosaur/es2040 87 | [8]: http://img.shields.io/npm/dm/es2040.svg?style=flat-square 88 | [9]: https://npmjs.org/package/es2040 89 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 90 | [11]: https://github.com/feross/standard 91 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const foo = () => 'hello world' 2 | 3 | const string = foo` 4 | there 5 | ` 6 | 7 | const bar = 'sup' 8 | 9 | module.exports = [foo, string, bar] 10 | -------------------------------------------------------------------------------- /example.json: -------------------------------------------------------------------------------- 1 | { "same": "as it ever was" } 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const preset = require('babel-preset-es2040') 2 | const through = require('through2') 3 | const babel = require('babel-core') 4 | 5 | module.exports = es2040 6 | 7 | // In hindsight we can do without most of ES6 8 | // (str, obj) -> transformStream 9 | function es2040 (filename, options) { 10 | if (/\.json$/i.test(filename)) return through() 11 | 12 | const debug = options._flags && options._flags.debug 13 | 14 | const bufs = [] 15 | const transformStream = through(write, end) 16 | return transformStream 17 | 18 | function write (buf, enc, next) { 19 | bufs.push(buf) 20 | next() 21 | } 22 | 23 | function end () { 24 | const src = Buffer.concat(bufs).toString('utf8') 25 | try { 26 | var res = babel.transform(src, { 27 | plugins: preset.plugins, 28 | sourceMaps: debug ? 'inline' : false, 29 | filename: filename, 30 | compact: false, 31 | babelrc: false 32 | }) 33 | } catch (err) { 34 | this.emit('error', err) 35 | return 36 | } 37 | this.push(res.code) 38 | this.push(null) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "es2040", 3 | "version": "1.2.6", 4 | "description": "browserify transform that compiles a selection of ES6 to valid ES5", 5 | "main": "index.js", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "test": "standard && npm run deps && NODE_ENV=test node test", 9 | "test:cov": "standard && npm run deps && NODE_ENV=test istanbul cover test.js" 10 | }, 11 | "repository": "ahdinosaur/es2040", 12 | "keywords": [ 13 | "es6", 14 | "babel", 15 | "transform", 16 | "fat-arrows", 17 | "template-strings", 18 | "tagged-templates", 19 | "const", 20 | "var", 21 | "esnext", 22 | "browserify" 23 | ], 24 | "license": "MIT", 25 | "dependencies": { 26 | "babel-core": "^6.9.1", 27 | "babel-preset-es2040": "^1.1.0", 28 | "through2": "^2.0.1" 29 | }, 30 | "devDependencies": { 31 | "browserify": "~13.1.0", 32 | "convert-source-map": "~1.3.0", 33 | "dependency-check": "^2.5.1", 34 | "istanbul": "^0.4.3", 35 | "standard": "^8.0.0", 36 | "tape": "^4.5.1" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const test = require('tape') 2 | const browserify = require('browserify') 3 | const sourceMap = require('convert-source-map') 4 | const fs = require('fs') 5 | const path = require('path') 6 | const es2020 = require('./') 7 | 8 | test('transpilation', function (t) { 9 | t.plan(4) 10 | 11 | browserify() 12 | .add('example.js') 13 | .transform(es2020) 14 | .bundle(function (err, code) { 15 | if (err) return t.end(err) 16 | code = String(code) 17 | 18 | t.notOk(code.includes('=>'), 'transpiles arrow functions') 19 | t.notOk(code.includes('`'), 'transpiles template literals') 20 | t.notOk(code.includes('`'), 'transpiles const assignments') 21 | t.notOk(code.includes('sourceMap'), 'does not build source maps by default') 22 | }) 23 | }) 24 | 25 | test('source maps', function (t) { 26 | t.plan(2) 27 | 28 | browserify({ 29 | debug: true 30 | }) 31 | .add('example.js') 32 | .transform(es2020) 33 | .bundle(function (err, code) { 34 | if (err) return t.end(err) 35 | code = String(code) 36 | 37 | const data = sourceMap.fromSource(code) 38 | const original = { 39 | code: data.getProperty('sourcesContent')[1], 40 | path: data.getProperty('sources')[1] 41 | } 42 | 43 | t.equal(original.code, fs.readFileSync(path.resolve(__dirname, 'example.js'), 'utf8')) 44 | t.equal(original.path, 'example.js') 45 | }) 46 | }) 47 | 48 | test('JSON files', function (t) { 49 | t.plan(2) 50 | 51 | browserify() 52 | .add('example.json') 53 | .transform(es2020) 54 | .bundle(function (err, code) { 55 | if (err) return t.end(err) 56 | code = String(code) 57 | 58 | t.assert(code.includes('same'), 'preserves key') 59 | t.assert(code.includes('as it ever was'), 'preserves value') 60 | }) 61 | }) 62 | 63 | test('handle errors', function (t) { 64 | t.plan(1) 65 | 66 | browserify({ 67 | debug: true 68 | }) 69 | .add('LICENSE') 70 | .transform(es2020) 71 | .bundle(function (err, code) { 72 | t.ok(err, 'handles error') 73 | }) 74 | }) 75 | --------------------------------------------------------------------------------