Skip to content

Commit

Permalink
Babel 7 alpha (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo authored Aug 4, 2017
1 parent 2d63fb4 commit 21bf286
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 63 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ sudo: false
language: node_js
node_js:
- 'stable'
- '0.12'
- '0.10'
- '6'
- '4'
37 changes: 19 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,27 +25,29 @@ module.exports = function (opts) {
}

try {
var fileOpts = objectAssign({}, opts, {
const fileOpts = Object.assign({}, opts, {
filename: file.path,
filenameRelative: file.relative,
sourceMap: Boolean(file.sourceMap),
sourceFileName: file.relative,
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) {
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "xo && mocha"
Expand All @@ -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": "*"
Expand Down
74 changes: 37 additions & 37 deletions test.js
Original file line number Diff line number Diff line change
@@ -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');
});
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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']);
});

Expand All @@ -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)
Expand Down

0 comments on commit 21bf286

Please sign in to comment.