Skip to content

Commit c8cd387

Browse files
authored
Apply changes from babel/pull/113
Url: babel#113
1 parent 46db7ee commit c8cd387

File tree

1 file changed

+74
-61
lines changed

1 file changed

+74
-61
lines changed

index.js

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,84 +7,97 @@ const replaceExt = require('replace-ext');
77
const babel = require('@babel/core');
88

99
function replaceExtension(fp) {
10-
return path.extname(fp) ? replaceExt(fp, '.js') : fp;
10+
return path.extname(fp) ? replaceExt(fp, '.js') : fp;
1111
}
1212

1313
module.exports = function (opts) {
14-
opts = opts || {};
14+
opts = opts || {};
1515

16-
return through.obj(function (file, enc, cb) {
17-
if (file.isNull()) {
18-
cb(null, file);
19-
return;
20-
}
16+
return through.obj(function (file, enc, cb) {
17+
if (file.isNull()) {
18+
cb(null, file);
19+
return;
20+
}
2121

22-
if (file.isStream()) {
23-
cb(new PluginError('gulp-babel', 'Streaming not supported'));
24-
return;
25-
}
22+
if (file.isStream()) {
23+
cb(new PluginError('gulp-babel', 'Streaming not supported'));
24+
return;
25+
}
2626

27-
if (!supportsCallerOption()) {
28-
cb(new PluginError('gulp-babel', '@babel/core@^7.0.0 is required'));
29-
return;
30-
}
27+
if (!supportsCallerOption()) {
28+
cb(new PluginError('gulp-babel', '@babel/core@^7.0.0 is required'));
29+
return;
30+
}
3131

32-
const fileOpts = Object.assign({}, opts, {
33-
filename: file.path,
34-
filenameRelative: file.relative,
35-
sourceMap: Boolean(file.sourceMap),
36-
sourceFileName: file.relative,
37-
caller: Object.assign(
38-
{name: 'babel-gulp'},
39-
opts.caller
40-
)
41-
});
4232

43-
babel.transformAsync(file.contents.toString(), fileOpts).then(res => {
44-
if (res) {
45-
if (file.sourceMap && res.map) {
46-
res.map.file = replaceExtension(file.relative);
47-
applySourceMap(file, res.map);
48-
}
33+
const isInputSourceMapPresent = Boolean(file.sourceMap);
34+
const defaultOpts = {
35+
filename: file.path,
36+
filenameRelative: file.relative,
37+
caller: Object.assign(
38+
{ name: 'babel-gulp' },
39+
opts.caller
40+
)
41+
};
4942

50-
file.contents = Buffer.from(res.code);
51-
file.path = replaceExtension(file.path);
43+
if (isInputSourceMapPresent) {
44+
defaultOpts.inputSourceMap = file.sourceMap;
45+
}
46+
else {
47+
defaultOpts.sourceFileName = file.relative;
48+
}
5249

53-
file.babel = res.metadata;
54-
}
50+
const fileOpts = Object.assign({}, opts, defaultOpts);
5551

56-
this.push(file);
57-
}).catch(error => {
58-
this.emit('error', new PluginError('gulp-babel', error, {
59-
fileName: file.path,
60-
showProperties: false
61-
}));
62-
}).then(
63-
() => cb(),
64-
() => cb()
65-
);
66-
});
52+
babel.transformAsync(file.contents.toString(), fileOpts).then(res => {
53+
if (res) {
54+
if (file.sourceMap && res.map) {
55+
if (isInputSourceMapPresent) {
56+
file.sourceMap = res.map;
57+
} else {
58+
res.map.file = replaceExtension(file.relative);
59+
applySourceMap(file, res.map);
60+
}
61+
}
62+
63+
file.contents = Buffer.from(res.code);
64+
file.path = replaceExtension(file.path);
65+
66+
file.babel = res.metadata;
67+
}
68+
69+
this.push(file);
70+
}).catch(error => {
71+
this.emit('error', new PluginError('gulp-babel', error, {
72+
fileName: file.path,
73+
showProperties: false
74+
}));
75+
}).then(
76+
() => cb(),
77+
() => cb()
78+
);
79+
});
6780
};
6881

6982
// Note: We can remove this eventually, I'm just adding it so that people have
7083
// a little time to migrate to the newer RCs of @babel/core without getting
7184
// hard-to-diagnose errors about unknown 'caller' options.
7285
let supportsCallerOptionFlag;
7386
function supportsCallerOption() {
74-
if (supportsCallerOptionFlag === undefined) {
75-
try {
76-
// Rather than try to match the Babel version, we just see if it throws
77-
// when passed a 'caller' flag, and use that to decide if it is supported.
78-
babel.loadPartialConfig({
79-
caller: undefined,
80-
babelrc: false,
81-
configFile: false
82-
});
83-
supportsCallerOptionFlag = true;
84-
} catch (_) {
85-
supportsCallerOptionFlag = false;
86-
}
87-
}
87+
if (supportsCallerOptionFlag === undefined) {
88+
try {
89+
// Rather than try to match the Babel version, we just see if it throws
90+
// when passed a 'caller' flag, and use that to decide if it is supported.
91+
babel.loadPartialConfig({
92+
caller: undefined,
93+
babelrc: false,
94+
configFile: false
95+
});
96+
supportsCallerOptionFlag = true;
97+
} catch (_) {
98+
supportsCallerOptionFlag = false;
99+
}
100+
}
88101

89-
return supportsCallerOptionFlag;
102+
return supportsCallerOptionFlag;
90103
}

0 commit comments

Comments
 (0)