From 4a31db7f235a7175266ad5d929c958acabf6779b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 13 Nov 2021 12:22:35 +0100 Subject: [PATCH] Fix imports resolution --- .../src/get-runtime-path/browser.ts | 4 ++++ .../src/get-runtime-path/index.ts | 4 ++++ .../src/index.ts | 23 ++++++++++--------- .../@babel/runtime/helpers/classCallCheck.js | 1 + 4 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck.js diff --git a/packages/babel-plugin-transform-runtime/src/get-runtime-path/browser.ts b/packages/babel-plugin-transform-runtime/src/get-runtime-path/browser.ts index 508eada79778..94af79f4ce4d 100644 --- a/packages/babel-plugin-transform-runtime/src/get-runtime-path/browser.ts +++ b/packages/babel-plugin-transform-runtime/src/get-runtime-path/browser.ts @@ -1,6 +1,10 @@ export default function (moduleName, dirname, absoluteRuntime) { if (absoluteRuntime === false) return moduleName; + resolveFSPath(); +} + +export function resolveFSPath() { throw new Error( "The 'absoluteRuntime' option is not supported when using @babel/standalone.", ); diff --git a/packages/babel-plugin-transform-runtime/src/get-runtime-path/index.ts b/packages/babel-plugin-transform-runtime/src/get-runtime-path/index.ts index 56e3a5a1ed49..28f6d7a82197 100644 --- a/packages/babel-plugin-transform-runtime/src/get-runtime-path/index.ts +++ b/packages/babel-plugin-transform-runtime/src/get-runtime-path/index.ts @@ -32,3 +32,7 @@ function resolveAbsoluteRuntime(moduleName: string, dirname: string) { ); } } + +export function resolveFSPath(path) { + return require.resolve(path); +} diff --git a/packages/babel-plugin-transform-runtime/src/index.ts b/packages/babel-plugin-transform-runtime/src/index.ts index f1c48fb588a4..ebb54cbf11eb 100644 --- a/packages/babel-plugin-transform-runtime/src/index.ts +++ b/packages/babel-plugin-transform-runtime/src/index.ts @@ -3,7 +3,7 @@ import { addDefault, isModule } from "@babel/helper-module-imports"; import { types as t } from "@babel/core"; import { hasMinVersion } from "./helpers"; -import getRuntimePath from "./get-runtime-path"; +import getRuntimePath, { resolveFSPath } from "./get-runtime-path"; import _pluginCorejs2 from "babel-plugin-polyfill-corejs2"; import _pluginCorejs3 from "babel-plugin-polyfill-corejs3"; @@ -165,8 +165,6 @@ export default declare((api, options, dirname) => { }; } - const corejsExt = absoluteRuntime ? ".js" : ""; - return { name: "transform-runtime", @@ -175,14 +173,16 @@ export default declare((api, options, dirname) => { pluginCorejs2, { method: "usage-pure", + absoluteImports: absoluteRuntime ? modulePath : false, [pluginsCompat]: { runtimeVersion, useBabelRuntime: modulePath, - ext: corejsExt, + ext: "", }, }, createRegeneratorPlugin({ method: "usage-pure", + absoluteImports: absoluteRuntime ? modulePath : false, [pluginsCompat]: { useBabelRuntime: modulePath }, }), ) @@ -193,15 +193,18 @@ export default declare((api, options, dirname) => { method: "usage-pure", version: 3, proposals, - [pluginsCompat]: { useBabelRuntime: modulePath, ext: corejsExt }, + absoluteImports: absoluteRuntime ? modulePath : false, + [pluginsCompat]: { useBabelRuntime: modulePath, ext: "" }, }, createRegeneratorPlugin({ method: "usage-pure", + absoluteImports: absoluteRuntime ? modulePath : false, [pluginsCompat]: { useBabelRuntime: modulePath }, }), ) : createRegeneratorPlugin({ method: "usage-pure", + absoluteImports: absoluteRuntime ? modulePath : false, [pluginsCompat]: { useBabelRuntime: modulePath }, }), @@ -232,12 +235,10 @@ export default declare((api, options, dirname) => { ? "helpers/esm" : "helpers"; - return addDefaultImport( - `${modulePath}/${helpersDir}/${name}`, - name, - blockHoist, - true, - ); + let helperPath = `${modulePath}/${helpersDir}/${name}`; + if (absoluteRuntime) helperPath = resolveFSPath(helperPath); + + return addDefaultImport(helperPath, name, blockHoist, true); }); const cache = new Map(); diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck.js b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck.js new file mode 100644 index 000000000000..8b1a393741c9 --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck.js @@ -0,0 +1 @@ +// empty