fix: babel-register transform internal dependencies (#12665)

Closes #11964, #12662.
This commit is contained in:
overlookmotel
2021-01-22 10:08:20 +00:00
committed by GitHub
parent f1314a1683
commit 22eb99bea4
6 changed files with 97 additions and 15 deletions

View File

@@ -9,7 +9,7 @@ exports = module.exports = function (...args) {
};
exports.__esModule = true;
const node = require("./node");
const node = require("./nodeWrapper");
const register = node.default;
Object.assign(exports, node);

View File

@@ -7,6 +7,7 @@ import { OptionManager, DEFAULT_EXTENSIONS } from "@babel/core";
import { addHook } from "pirates";
import fs from "fs";
import path from "path";
import Module from "module";
const maps = {};
let transformOpts = {};
@@ -82,15 +83,19 @@ function compile(code, filename) {
}
let compiling = false;
const internalModuleCache = Module._cache;
function compileHook(code, filename) {
if (compiling) return code;
const globalModuleCache = Module._cache;
try {
compiling = true;
Module._cache = internalModuleCache;
return compile(code, filename);
} finally {
compiling = false;
Module._cache = globalModuleCache;
}
}

View File

@@ -0,0 +1,21 @@
/**
* This file wraps the implementation of register so all modules `require()`-ed
* internally within register are stored in a separate module cache.
* This prevents un-transformed modules being stored in global module cache,
* and allows register to transform these modules if they are loaded externally.
*/
const Module = require("module");
const globalModuleCache = Module._cache;
const internalModuleCache = Object.create(null);
Module._cache = internalModuleCache;
const node = require("./node");
Module._cache = globalModuleCache;
// Add source-map-support to global cache as it's stateful
const smsPath = require.resolve("source-map-support");
globalModuleCache[smsPath] = internalModuleCache[smsPath];
module.exports = node;