Skip to content

Commit

Permalink
Don't include "resolve" in @babel/standalone (#11432)
Browse files Browse the repository at this point in the history
* Don't include "resolve" in @babel/standalone

* Also alias src
  • Loading branch information
nicolo-ribaudo committed Apr 16, 2020
1 parent 6b8f6ab commit d9eb943
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
4 changes: 4 additions & 0 deletions packages/babel-plugin-transform-runtime/package.json
Expand Up @@ -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",
Expand Down
@@ -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.",
);
}
@@ -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,
},
);
}
}
30 changes: 2 additions & 28 deletions packages/babel-plugin-transform-runtime/src/index.js
@@ -1,31 +1,11 @@
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";

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);
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit d9eb943

Please sign in to comment.