| 1 |
"use strict"; |
| 2 |
|
| 3 |
const cloneDeep = require("clone-deep"); |
| 4 |
const path = require("path"); |
| 5 |
const fs = require("fs"); |
| 6 |
|
| 7 |
const babel = require("./babel-core"); |
| 8 |
const registerCache = require("../cache"); |
| 9 |
|
| 10 |
const nmRE = escapeRegExp(path.sep + "node_modules" + path.sep); |
| 11 |
|
| 12 |
function escapeRegExp(string) { |
| 13 |
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&"); |
| 14 |
} |
| 15 |
|
| 16 |
let cache; |
| 17 |
let transformOpts; |
| 18 |
exports.setOptions = function (opts) { |
| 19 |
if (opts.cache === false && cache) { |
| 20 |
registerCache.clear(); |
| 21 |
cache = null; |
| 22 |
} else if (opts.cache !== false && !cache) { |
| 23 |
registerCache.load(); |
| 24 |
cache = registerCache.get(); |
| 25 |
} |
| 26 |
|
| 27 |
delete opts.cache; |
| 28 |
delete opts.extensions; |
| 29 |
|
| 30 |
transformOpts = { |
| 31 |
...opts, |
| 32 |
caller: { |
| 33 |
name: "@babel/register", |
| 34 |
...(opts.caller || {}), |
| 35 |
}, |
| 36 |
}; |
| 37 |
|
| 38 |
let { cwd = "." } = transformOpts; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
cwd = transformOpts.cwd = path.resolve(cwd); |
| 43 |
|
| 44 |
if (transformOpts.ignore === undefined && transformOpts.only === undefined) { |
| 45 |
const cwdRE = escapeRegExp(cwd); |
| 46 |
|
| 47 |
|
| 48 |
transformOpts.only = [new RegExp("^" + cwdRE, "i")]; |
| 49 |
|
| 50 |
transformOpts.ignore = [ |
| 51 |
new RegExp(`^${cwdRE}(?:${path.sep}.*)?${nmRE}`, "i"), |
| 52 |
]; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
exports.transform = async function (input, filename) { |
| 57 |
const opts = await babel.loadOptionsAsync({ |
| 58 |
|
| 59 |
sourceRoot: path.dirname(filename) + path.sep, |
| 60 |
...cloneDeep(transformOpts), |
| 61 |
filename, |
| 62 |
}); |
| 63 |
|
| 64 |
|
| 65 |
if (opts === null) return null; |
| 66 |
|
| 67 |
const { cached, store } = cacheLookup(opts, filename); |
| 68 |
if (cached) return cached; |
| 69 |
|
| 70 |
const { code, map } = await babel.transformAsync(input, { |
| 71 |
...opts, |
| 72 |
sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps, |
| 73 |
ast: false, |
| 74 |
}); |
| 75 |
|
| 76 |
return store({ code, map }); |
| 77 |
}; |
| 78 |
|
| 79 |
if (!process.env.BABEL_8_BREAKING) { |
| 80 |
exports.transformSync = function (input, filename) { |
| 81 |
const opts = new babel.OptionManager().init({ |
| 82 |
|
| 83 |
sourceRoot: path.dirname(filename) + path.sep, |
| 84 |
...cloneDeep(transformOpts), |
| 85 |
filename, |
| 86 |
}); |
| 87 |
|
| 88 |
|
| 89 |
if (opts === null) return null; |
| 90 |
|
| 91 |
const { cached, store } = cacheLookup(opts, filename); |
| 92 |
if (cached) return cached; |
| 93 |
|
| 94 |
const { code, map } = babel.transformSync(input, { |
| 95 |
...opts, |
| 96 |
sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps, |
| 97 |
ast: false, |
| 98 |
}); |
| 99 |
|
| 100 |
return store({ code, map }); |
| 101 |
}; |
| 102 |
} |
| 103 |
|
| 104 |
const id = value => value; |
| 105 |
|
| 106 |
function cacheLookup(opts, filename) { |
| 107 |
if (!cache) return { cached: null, store: id }; |
| 108 |
|
| 109 |
let cacheKey = `${JSON.stringify(opts)}:${babel.version}`; |
| 110 |
|
| 111 |
const env = babel.getEnv(); |
| 112 |
if (env) cacheKey += `:${env}`; |
| 113 |
|
| 114 |
const cached = cache[cacheKey]; |
| 115 |
const fileMtime = +fs.statSync(filename).mtime; |
| 116 |
|
| 117 |
if (cached && cached.mtime === fileMtime) { |
| 118 |
return { cached: cached.value, store: id }; |
| 119 |
} |
| 120 |
|
| 121 |
return { |
| 122 |
cached: null, |
| 123 |
store(value) { |
| 124 |
cache[cacheKey] = { value, mtime: fileMtime }; |
| 125 |
registerCache.setDirty(); |
| 126 |
return value; |
| 127 |
}, |
| 128 |
}; |
| 129 |
} |