UNPKG

es2040

Version:

browserify transform that compiles a selection of ES6 to valid ES5

41 lines (34 loc) 933 B
const preset = require('babel-preset-es2040') const through = require('through2') const babel = require('babel-core') module.exports = es2040 // In hindsight we can do without most of ES6 // (str, obj) -> transformStream function es2040 (filename, options) { if (/\.json$/i.test(filename)) return through() const debug = options._flags && options._flags.debug const bufs = [] const transformStream = through(write, end) return transformStream function write (buf, enc, next) { bufs.push(buf) next() } function end () { const src = Buffer.concat(bufs).toString('utf8') try { var res = babel.transform(src, { plugins: preset.plugins, sourceMaps: debug ? 'inline' : false, filename: filename, compact: false, babelrc: false }) } catch (err) { this.emit('error', err) return } this.push(res.code) this.push(null) } }