move babel/register into a separate package
This commit is contained in:
@@ -25,6 +25,9 @@ export { t as types };
|
||||
import traverse from "babel-traverse";
|
||||
export { traverse };
|
||||
|
||||
import OptionManager from "../transformation/file/options/option-manager";
|
||||
export { OptionManager };
|
||||
|
||||
//
|
||||
|
||||
import Pipeline from "../transformation/pipeline";
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
// required to safely use babel/register within a browserify codebase
|
||||
|
||||
export default function () {}
|
||||
@@ -1,55 +0,0 @@
|
||||
/* @flow */
|
||||
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import homeOrTmp from "home-or-tmp";
|
||||
import pathExists from "path-exists";
|
||||
|
||||
const FILENAME = process.env.BABEL_CACHE_PATH || path.join(homeOrTmp, ".babel.json");
|
||||
let data = {};
|
||||
|
||||
/**
|
||||
* Write stringified cache to disk.
|
||||
*/
|
||||
|
||||
export function save() {
|
||||
let serialised = {};
|
||||
try {
|
||||
serialised = JSON.stringify(data, null, " ");
|
||||
} catch (err) {
|
||||
if (err.message === "Invalid string length") {
|
||||
err.message = "Cache too large so it's been cleared.";
|
||||
console.error(err.stack);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(FILENAME, serialised);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load cache from disk and parse.
|
||||
*/
|
||||
|
||||
export function load() {
|
||||
if (process.env.BABEL_DISABLE_CACHE) return;
|
||||
|
||||
process.on("exit", save);
|
||||
process.nextTick(save);
|
||||
|
||||
if (!pathExists.sync(FILENAME)) return;
|
||||
|
||||
try {
|
||||
data = JSON.parse(fs.readFileSync(FILENAME));
|
||||
} catch (err) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve data from cache.
|
||||
*/
|
||||
|
||||
export function get() {
|
||||
return data;
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
/* @flow */
|
||||
|
||||
import deepClone from "lodash/lang/cloneDeep";
|
||||
import sourceMapSupport from "source-map-support";
|
||||
import * as registerCache from "./cache";
|
||||
import OptionManager from "../../transformation/file/options/option-manager";
|
||||
import extend from "lodash/object/extend";
|
||||
import * as babel from "../node";
|
||||
import each from "lodash/collection/each";
|
||||
import * as util from "../../util";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
sourceMapSupport.install({
|
||||
handleUncaughtExceptions: false,
|
||||
retrieveSourceMap(source) {
|
||||
let map = maps && maps[source];
|
||||
if (map) {
|
||||
return {
|
||||
url: null,
|
||||
map: map
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
registerCache.load();
|
||||
let cache = registerCache.get();
|
||||
|
||||
let transformOpts = {};
|
||||
|
||||
let ignore;
|
||||
let only;
|
||||
|
||||
let oldHandlers = {};
|
||||
let maps = {};
|
||||
|
||||
let cwd = process.cwd();
|
||||
|
||||
function getRelativePath(filename){
|
||||
return path.relative(cwd, filename);
|
||||
}
|
||||
|
||||
function mtime(filename) {
|
||||
return +fs.statSync(filename).mtime;
|
||||
}
|
||||
|
||||
function compile(filename, opts = {}) {
|
||||
let result;
|
||||
|
||||
opts.filename = filename;
|
||||
|
||||
let optsManager = new OptionManager;
|
||||
optsManager.mergeOptions(deepClone(transformOpts));
|
||||
opts = optsManager.init(opts);
|
||||
|
||||
let cacheKey = `${JSON.stringify(opts)}:${babel.version}`;
|
||||
|
||||
let env = process.env.BABEL_ENV || process.env.NODE_ENV;
|
||||
if (env) cacheKey += `:${env}`;
|
||||
|
||||
if (cache) {
|
||||
let cached = cache[cacheKey];
|
||||
if (cached && cached.mtime === mtime(filename)) {
|
||||
result = cached;
|
||||
}
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
result = babel.transformFileSync(filename, extend(opts, {
|
||||
sourceMap: "both",
|
||||
ast: false
|
||||
}));
|
||||
}
|
||||
|
||||
if (cache) {
|
||||
cache[cacheKey] = result;
|
||||
result.mtime = mtime(filename);
|
||||
}
|
||||
|
||||
maps[filename] = result.map;
|
||||
|
||||
return result.code;
|
||||
}
|
||||
|
||||
function shouldIgnore(filename) {
|
||||
if (!ignore && !only) {
|
||||
return getRelativePath(filename).split(path.sep).indexOf("node_modules") >= 0;
|
||||
} else {
|
||||
return util.shouldIgnore(filename, ignore || [], only);
|
||||
}
|
||||
}
|
||||
|
||||
function loader(m, filename) {
|
||||
m._compile(compile(filename), filename);
|
||||
}
|
||||
|
||||
function registerExtension(ext) {
|
||||
let old = oldHandlers[ext] || oldHandlers[".js"] || require.extensions[".js"];
|
||||
|
||||
require.extensions[ext] = function (m, filename) {
|
||||
if (shouldIgnore(filename)) {
|
||||
old(m, filename);
|
||||
} else {
|
||||
loader(m, filename, old);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function hookExtensions(_exts) {
|
||||
each(oldHandlers, function (old, ext) {
|
||||
if (old === undefined) {
|
||||
delete require.extensions[ext];
|
||||
} else {
|
||||
require.extensions[ext] = old;
|
||||
}
|
||||
});
|
||||
|
||||
oldHandlers = {};
|
||||
|
||||
each(_exts, function (ext) {
|
||||
oldHandlers[ext] = require.extensions[ext];
|
||||
registerExtension(ext);
|
||||
});
|
||||
}
|
||||
|
||||
hookExtensions(util.canCompile.EXTENSIONS);
|
||||
|
||||
export default function (opts?: Object = {}) {
|
||||
if (opts.only != null) only = util.arrayify(opts.only, util.regexify);
|
||||
if (opts.ignore != null) ignore = util.arrayify(opts.ignore, util.regexify);
|
||||
|
||||
if (opts.extensions) hookExtensions(util.arrayify(opts.extensions));
|
||||
|
||||
if (opts.cache === false) cache = null;
|
||||
|
||||
delete opts.extensions;
|
||||
delete opts.ignore;
|
||||
delete opts.cache;
|
||||
delete opts.only;
|
||||
|
||||
extend(transformOpts, opts);
|
||||
}
|
||||
Reference in New Issue
Block a user