Don't include "resolve" in @babel/standalone (#11432)

* Don't include "resolve" in @babel/standalone

* Also alias src
This commit is contained in:
Nicolò Ribaudo 2020-04-16 20:19:22 +02:00 committed by GitHub
parent 6b8f6ab2de
commit d9eb94327a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 28 deletions

View File

@ -11,6 +11,10 @@
"keywords": [
"babel-plugin"
],
"browser": {
"./lib/get-runtime-path/index.js": "./lib/get-runtime-path/browser.js",
"./src/get-runtime-path/index.js": "./src/get-runtime-path/browser.js"
},
"dependencies": {
"@babel/helper-module-imports": "^7.8.3",
"@babel/helper-plugin-utils": "^7.8.3",

View File

@ -0,0 +1,7 @@
export default function(moduleName, dirname, absoluteRuntime) {
if (absoluteRuntime === false) return moduleName;
throw new Error(
"The 'absoluteRuntime' option is not supported when using @babel/standalone.",
);
}

View File

@ -0,0 +1,30 @@
import path from "path";
import resolve from "resolve";
export default function(moduleName, dirname, absoluteRuntime) {
if (absoluteRuntime === false) return moduleName;
return resolveAbsoluteRuntime(
moduleName,
path.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime),
);
}
function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
try {
return path
.dirname(resolve.sync(`${moduleName}/package.json`, { basedir: dirname }))
.replace(/\\/g, "/");
} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") throw err;
throw Object.assign(
new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`),
{
code: "BABEL_RUNTIME_NOT_FOUND",
runtime: moduleName,
dirname,
},
);
}
}

View File

@ -1,5 +1,3 @@
import path from "path";
import resolve from "resolve";
import { declare } from "@babel/helper-plugin-utils";
import { addDefault, isModule } from "@babel/helper-module-imports";
import { types as t } from "@babel/core";
@ -7,25 +5,7 @@ import { types as t } from "@babel/core";
import getCoreJS2Definitions from "./runtime-corejs2-definitions";
import getCoreJS3Definitions from "./runtime-corejs3-definitions";
import { typeAnnotationToString } from "./helpers";
function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
try {
return path
.dirname(resolve.sync(`${moduleName}/package.json`, { basedir: dirname }))
.replace(/\\/g, "/");
} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") throw err;
throw Object.assign(
new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`),
{
code: "BABEL_RUNTIME_NOT_FOUND",
runtime: moduleName,
dirname,
},
);
}
}
import getRuntimePath from "./get-runtime-path";
function supportsStaticESM(caller) {
return !!(caller && caller.supportsStaticESM);
@ -196,13 +176,7 @@ export default declare((api, options, dirname) => {
const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
let modulePath = moduleName;
if (absoluteRuntime !== false) {
modulePath = resolveAbsoluteRuntime(
moduleName,
path.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime),
);
}
const modulePath = getRuntimePath(moduleName, dirname, absoluteRuntime);
return {
name: "transform-runtime",