Skip to content

Commit

Permalink
Use the async transform API, add caller and drop dependencies on the …
Browse files Browse the repository at this point in the history
…beta/rc range. (#162)

* Use the async transform API and drop dependencies on the beta/rc range.

* Update the README for [email protected]
  • Loading branch information
loganfsmyth authored Aug 28, 2018
1 parent 32dee19 commit 7952cc2
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 385 deletions.
2 changes: 1 addition & 1 deletion .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'
- '8'
- '6'
- '4'
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
> This readme is for gulp-babel v8 + Babel v7
> Check the [7.x branch](https://github.com/babel/gulp-babel/tree/v7-maintenance) for docs with Babel v6 usage
# gulp-babel [![npm](https://img.shields.io/npm/v/gulp-babel.svg?maxAge=2592000)](https://www.npmjs.com/package/gulp-babel) [![Build Status](https://travis-ci.org/babel/gulp-babel.svg?branch=master)](https://travis-ci.org/babel/gulp-babel)

> Use next generation JavaScript, today, with [Babel](https://babeljs.io)
Expand All @@ -7,14 +10,14 @@

## Install

Install `gulp-babel@next` if you want to get the pre-release of the next version of `gulp-babel`.
Install `gulp-babel` if you want to get the pre-release of the next version of `gulp-babel`.

```
# Babel 7 (use exact dependencies while in beta, not `^`)
$ npm install --save-dev gulp-babel@next @babel/core @babel/preset-env
# Babel 7
$ npm install --save-dev gulp-babel @babel/core @babel/preset-env
# Babel 6
$ npm install --save-dev gulp-babel babel-core babel-preset-env
$ npm install --save-dev gulp-babel@7 babel-core babel-preset-env
```

## Usage
Expand Down Expand Up @@ -105,7 +108,8 @@ If you're attempting to use features such as generators, you'll need to add `tra
Install the runtime:

```
$ npm install --save-dev @babel/plugin-transform-runtime
$ npm install --save-dev @babel/plugin-transform-runtime
$ npm install --save @babel/runtime
```

Use it as plugin:
Expand Down
63 changes: 46 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,67 @@ module.exports = function (opts) {
return;
}

try {
const fileOpts = Object.assign({}, opts, {
filename: file.path,
filenameRelative: file.relative,
sourceMap: Boolean(file.sourceMap),
sourceFileName: file.relative
});
if (!supportsCallerOption()) {
cb(new PluginError('gulp-babel', '@babel/core@^7.0.0 is required'));
return;
}

const res = babel.transform(file.contents.toString(), fileOpts);
const fileOpts = Object.assign({}, opts, {
filename: file.path,
filenameRelative: file.relative,
sourceMap: Boolean(file.sourceMap),
sourceFileName: file.relative,
caller: Object.assign(
{name: 'babel-gulp'},
opts.caller
)
});

if (res !== null) {
babel.transformAsync(file.contents.toString(), fileOpts).then(res => {
if (res) {
if (file.sourceMap && res.map) {
res.map.file = replaceExtension(file.relative);
applySourceMap(file, res.map);
}

if (!res.ignored) {
file.contents = new Buffer(res.code); // eslint-disable-line unicorn/no-new-buffer
file.path = replaceExtension(file.path);
}
file.contents = new Buffer(res.code); // eslint-disable-line unicorn/no-new-buffer
file.path = replaceExtension(file.path);

file.babel = res.metadata;
}

this.push(file);
} catch (err) {
}).catch(err => {
this.emit('error', new PluginError('gulp-babel', err, {
fileName: file.path,
showProperties: false
}));
}

cb();
}).then(
() => cb(),
() => cb()
);
});
};

// Note: We can remove this eventually, I'm just adding it so that people have
// a little time to migrate to the newer RCs of @babel/core without getting
// hard-to-diagnose errors about unknown 'caller' options.
let supportsCallerOptionFlag;
function supportsCallerOption() {
if (supportsCallerOptionFlag === undefined) {
try {
// Rather than try to match the Babel version, we just see if it throws
// when passed a 'caller' flag, and use that to decide if it is supported.
babel.loadPartialConfig({
caller: undefined,
babelrc: false,
configFile: false
});
supportsCallerOptionFlag = true;
} catch (err) {
supportsCallerOptionFlag = false;
}
}

return supportsCallerOptionFlag;
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"Charles Samborski <[email protected]> (demurgos.net)"
],
"engines": {
"node": ">=4"
"node": ">=6"
},
"scripts": {
"test": "xo && mocha"
Expand Down Expand Up @@ -44,13 +44,13 @@
"vinyl-sourcemaps-apply": "^0.2.0"
},
"peerDependencies": {
"@babel/core": "7 || ^7.0.0-0"
"@babel/core": "^7.0.0"
},
"devDependencies": {
"@babel/core": "7.0.0-beta.41",
"@babel/plugin-transform-arrow-functions": "7.0.0-beta.41",
"@babel/plugin-transform-block-scoping": "7.0.0-beta.41",
"@babel/plugin-transform-classes": "7.0.0-beta.41",
"@babel/core": "^7.0.0",
"@babel/plugin-transform-arrow-functions": "^7.0.0",
"@babel/plugin-transform-block-scoping": "^7.0.0",
"@babel/plugin-transform-classes": "^7.0.0",
"gulp-sourcemaps": "^1.1.1",
"mocha": "^5.0.0",
"pre-commit": "^1.2.2",
Expand Down
Loading

0 comments on commit 7952cc2

Please sign in to comment.