From 21bf286ab4917ffa1e5423d3d35cec621826431f Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Thu, 3 Aug 2017 20:20:22 -0400 Subject: [PATCH] Babel 7 alpha (#112) --- .travis.yml | 4 +-- index.js | 37 +++++++++++++------------- package.json | 11 ++++---- test.js | 74 ++++++++++++++++++++++++++-------------------------- 4 files changed, 63 insertions(+), 63 deletions(-) diff --git a/.travis.yml b/.travis.yml index 52ba159..52be004 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,5 +2,5 @@ sudo: false language: node_js node_js: - 'stable' - - '0.12' - - '0.10' + - '6' + - '4' diff --git a/index.js b/index.js index 73b2806..af8143b 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,10 @@ 'use strict'; -var path = require('path'); -var gutil = require('gulp-util'); -var through = require('through2'); -var applySourceMap = require('vinyl-sourcemaps-apply'); -var objectAssign = require('object-assign'); -var replaceExt = require('replace-ext'); -var babel = require('babel-core'); +const path = require('path'); +const gutil = require('gulp-util'); +const through = require('through2'); +const applySourceMap = require('vinyl-sourcemaps-apply'); +const replaceExt = require('replace-ext'); +const babel = require('babel-core'); function replaceExtension(fp) { return path.extname(fp) ? replaceExt(fp, '.js') : fp; @@ -26,7 +25,7 @@ module.exports = function (opts) { } try { - var fileOpts = objectAssign({}, opts, { + const fileOpts = Object.assign({}, opts, { filename: file.path, filenameRelative: file.relative, sourceMap: Boolean(file.sourceMap), @@ -34,19 +33,21 @@ module.exports = function (opts) { sourceMapTarget: file.relative }); - var res = babel.transform(file.contents.toString(), fileOpts); + const res = babel.transform(file.contents.toString(), fileOpts); - if (file.sourceMap && res.map) { - res.map.file = replaceExtension(res.map.file); - applySourceMap(file, res.map); - } + if (res !== null) { + if (file.sourceMap && res.map) { + res.map.file = replaceExtension(res.map.file); + applySourceMap(file, res.map); + } - if (!res.ignored) { - file.contents = new Buffer(res.code); - file.path = replaceExtension(file.path); - } + if (!res.ignored) { + file.contents = new Buffer(res.code); // eslint-disable-line unicorn/no-new-buffer + file.path = replaceExtension(file.path); + } - file.babel = res.metadata; + file.babel = res.metadata; + } this.push(file); } catch (err) { diff --git a/package.json b/package.json index a44abd6..1ec7d75 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=0.10.0" + "node": ">=4" }, "scripts": { "test": "xo && mocha" @@ -34,17 +34,16 @@ "compiler" ], "dependencies": { - "babel-core": "^6.23.1", + "babel-core": "7.0.0-alpha.18", "gulp-util": "^3.0.0", - "object-assign": "^4.0.1", "replace-ext": "0.0.1", "through2": "^2.0.0", "vinyl-sourcemaps-apply": "^0.2.0" }, "devDependencies": { - "babel-plugin-transform-es2015-arrow-functions": "^6.0.2", - "babel-plugin-transform-es2015-block-scoping": "^6.0.9", - "babel-plugin-transform-es2015-classes": "^6.0.8", + "babel-plugin-transform-es2015-arrow-functions": "7.0.0-alpha.18", + "babel-plugin-transform-es2015-block-scoping": "7.0.0-alpha.18", + "babel-plugin-transform-es2015-classes": "7.0.0-alpha.18", "gulp-sourcemaps": "^1.1.1", "mocha": "*", "xo": "*" diff --git a/test.js b/test.js index 6c2c807..33a84db 100644 --- a/test.js +++ b/test.js @@ -1,16 +1,16 @@ 'use strict'; -var path = require('path'); -var assert = require('assert'); -var gutil = require('gulp-util'); -var sourceMaps = require('gulp-sourcemaps'); -var babel = require('./'); - -it('should transpile with Babel', function (cb) { - var stream = babel({ +const path = require('path'); +const assert = require('assert'); +const gutil = require('gulp-util'); +const sourceMaps = require('gulp-sourcemaps'); +const babel = require('./'); + +it('should transpile with Babel', cb => { + const stream = babel({ plugins: ['transform-es2015-block-scoping'] }); - stream.on('data', function (file) { + stream.on('data', file => { assert(/var foo/.test(file.contents.toString()), file.contents.toString()); assert.equal(file.relative, 'fixture.js'); }); @@ -21,25 +21,25 @@ it('should transpile with Babel', function (cb) { cwd: __dirname, base: path.join(__dirname, 'fixture'), path: path.join(__dirname, 'fixture/fixture.jsx'), - contents: new Buffer('let foo;') + contents: Buffer.from('let foo;') })); stream.end(); }); -it('should generate source maps', function (cb) { - var init = sourceMaps.init(); - var write = sourceMaps.write(); +it('should generate source maps', cb => { + const init = sourceMaps.init(); + const write = sourceMaps.write(); init .pipe(babel({ plugins: ['transform-es2015-arrow-functions'] })) .pipe(write); - write.on('data', function (file) { + write.on('data', file => { assert.deepEqual(file.sourceMap.sources, ['fixture.es2015']); assert.strictEqual(file.sourceMap.file, 'fixture.js'); - var contents = file.contents.toString(); + const contents = file.contents.toString(); assert(/function/.test(contents)); assert(/sourceMappingURL/.test(contents)); cb(); @@ -49,26 +49,26 @@ it('should generate source maps', function (cb) { cwd: __dirname, base: path.join(__dirname, 'fixture'), path: path.join(__dirname, 'fixture/fixture.es2015'), - contents: new Buffer('[].map(v => v + 1)'), + contents: Buffer.from('[].map(v => v + 1)'), sourceMap: '' })); init.end(); }); -it('should generate source maps for file in nested folder', function (cb) { - var init = sourceMaps.init(); - var write = sourceMaps.write(); +it('should generate source maps for file in nested folder', cb => { + const init = sourceMaps.init(); + const write = sourceMaps.write(); init .pipe(babel({ plugins: ['transform-es2015-arrow-functions'] })) .pipe(write); - write.on('data', function (file) { + write.on('data', file => { assert.deepEqual(file.sourceMap.sources, ['nested/fixture.es2015']); assert.strictEqual(file.sourceMap.file, 'nested/fixture.js'); - var contents = file.contents.toString(); + const contents = file.contents.toString(); assert(/function/.test(contents)); assert(/sourceMappingURL/.test(contents)); cb(); @@ -78,19 +78,19 @@ it('should generate source maps for file in nested folder', function (cb) { cwd: __dirname, base: path.join(__dirname, 'fixture'), path: path.join(__dirname, 'fixture/nested/fixture.es2015'), - contents: new Buffer('[].map(v => v + 1)'), + contents: Buffer.from('[].map(v => v + 1)'), sourceMap: '' })); init.end(); }); -it('should list used helpers in file.babel', function (cb) { - var stream = babel({ +it('should list used helpers in file.babel', cb => { + const stream = babel({ plugins: ['transform-es2015-classes'] }); - stream.on('data', function (file) { + stream.on('data', file => { assert.deepEqual(file.babel.usedHelpers, ['classCallCheck']); }); @@ -100,48 +100,48 @@ it('should list used helpers in file.babel', function (cb) { cwd: __dirname, base: path.join(__dirname, 'fixture'), path: path.join(__dirname, 'fixture/fixture.js'), - contents: new Buffer('class MyClass {};') + contents: Buffer.from('class MyClass {};') })); stream.end(); }); -it('should not rename ignored files', function (cb) { - var stream = babel({ - ignore: /fixture/ +it('should not rename ignored files', cb => { + const stream = babel({ + ignore: [/fixture/] }); - var inputFile = { + const inputFile = { cwd: __dirname }; inputFile.base = path.join(inputFile.cwd, 'fixture'); inputFile.basename = 'fixture.jsx'; inputFile.path = path.join(inputFile.base, inputFile.basename); - inputFile.contents = new Buffer(';'); + inputFile.contents = Buffer.from(';'); stream - .on('data', function (file) { + .on('data', file => { assert.equal(file.relative, inputFile.basename); }) .on('end', cb) .end(new gutil.File(inputFile)); }); -it('should not rename files without an extension', function (cb) { - var stream = babel(); +it('should not rename files without an extension', cb => { + const stream = babel(); - var inputFile = { + const inputFile = { cwd: __dirname }; inputFile.base = path.join(inputFile.cwd, 'bin'); inputFile.basename = 'app'; inputFile.path = path.join(inputFile.base, inputFile.basename); - inputFile.contents = new Buffer(';'); + inputFile.contents = Buffer.from(';'); stream - .on('data', function (file) { + .on('data', file => { assert.equal(file.relative, inputFile.basename); }) .on('end', cb)