-
-
Notifications
You must be signed in to change notification settings - Fork 451
/
transform.js
31 lines (24 loc) · 997 Bytes
/
transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const babel = require("@babel/core");
const { promisify } = require("util");
const LoaderError = require("./Error");
const transform = promisify(babel.transform);
module.exports = async function (source, options) {
let result;
try {
result = await transform(source, options);
} catch (err) {
throw err.message && err.codeFrame ? new LoaderError(err) : err;
}
if (!result) return null;
// We don't return the full result here because some entries are not
// really serializable. For a full list of properties see here:
// https://github.com/babel/babel/blob/main/packages/babel-core/src/transformation/index.js
// For discussion on this topic see here:
// https://github.com/babel/babel-loader/pull/629
const { ast, code, map, metadata, sourceType } = result;
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
map.sourcesContent = [source];
}
return { ast, code, map, metadata, sourceType };
};
module.exports.version = babel.version;