/
transform.js
129 lines (101 loc) 路 3.22 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"use strict";
const cloneDeep = require("clone-deep");
const path = require("path");
const fs = require("fs");
const babel = require("./babel-core");
const registerCache = require("../cache");
const nmRE = escapeRegExp(path.sep + "node_modules" + path.sep);
function escapeRegExp(string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
}
let cache;
let transformOpts;
exports.setOptions = function (opts) {
if (opts.cache === false && cache) {
registerCache.clear();
cache = null;
} else if (opts.cache !== false && !cache) {
registerCache.load();
cache = registerCache.get();
}
delete opts.cache;
delete opts.extensions;
transformOpts = {
...opts,
caller: {
name: "@babel/register",
...(opts.caller || {}),
},
};
let { cwd = "." } = transformOpts;
// Ensure that the working directory is resolved up front so that
// things don't break if it changes later.
cwd = transformOpts.cwd = path.resolve(cwd);
if (transformOpts.ignore === undefined && transformOpts.only === undefined) {
const cwdRE = escapeRegExp(cwd);
// Only compile things inside the current working directory.
transformOpts.only = [new RegExp("^" + cwdRE, "i")];
// Ignore any node_modules inside the current working directory.
transformOpts.ignore = [
new RegExp(`^${cwdRE}(?:${path.sep}.*)?${nmRE}`, "i"),
];
}
};
exports.transform = async function (input, filename) {
const opts = await babel.loadOptionsAsync({
// sourceRoot can be overwritten
sourceRoot: path.dirname(filename) + path.sep,
...cloneDeep(transformOpts),
filename,
});
// Bail out ASAP if the file has been ignored.
if (opts === null) return null;
const { cached, store } = cacheLookup(opts, filename);
if (cached) return cached;
const { code, map } = await babel.transformAsync(input, {
...opts,
sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps,
ast: false,
});
return store({ code, map });
};
if (!process.env.BABEL_8_BREAKING) {
exports.transformSync = function (input, filename) {
const opts = new babel.OptionManager().init({
// sourceRoot can be overwritten
sourceRoot: path.dirname(filename) + path.sep,
...cloneDeep(transformOpts),
filename,
});
// Bail out ASAP if the file has been ignored.
if (opts === null) return null;
const { cached, store } = cacheLookup(opts, filename);
if (cached) return cached;
const { code, map } = babel.transformSync(input, {
...opts,
sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps,
ast: false,
});
return store({ code, map });
};
}
const id = value => value;
function cacheLookup(opts, filename) {
if (!cache) return { cached: null, store: id };
let cacheKey = `${JSON.stringify(opts)}:${babel.version}`;
const env = babel.getEnv();
if (env) cacheKey += `:${env}`;
const cached = cache[cacheKey];
const fileMtime = +fs.statSync(filename).mtime;
if (cached && cached.mtime === fileMtime) {
return { cached: cached.value, store: id };
}
return {
cached: null,
store(value) {
cache[cacheKey] = { value, mtime: fileMtime };
registerCache.setDirty();
return value;
},
};
}