Skip to content

Use the async transform API, add caller and drop dependencies on the beta/rc range. #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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