Skip to content

Commit

Permalink
Fully resolve absolute imports (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 12, 2021
1 parent 0de8ac6 commit c7ea736
Show file tree
Hide file tree
Showing 35 changed files with 310 additions and 122 deletions.
Expand Up @@ -18,32 +18,15 @@ export function resolve(
basedir = path.resolve(basedir, absoluteImports);
}

let modulePackage, moduleNestedPath;

let slash = moduleName.indexOf("/");
if (moduleName[0] === "@") {
slash = moduleName.indexOf("/", slash + 1);
}

if (slash === -1) {
modulePackage = moduleName;
moduleNestedPath = "";
} else {
modulePackage = moduleName.slice(0, slash);
moduleNestedPath = moduleName.slice(slash);
}

try {
let pkg;
if (nativeRequireResolve) {
// $FlowIgnore
pkg = require.resolve(`${modulePackage}/package.json`, {
return require.resolve(moduleName, {
paths: [basedir],
});
} else {
pkg = requireResolve.sync(`${modulePackage}/package.json`, { basedir });
return requireResolve.sync(moduleName, { basedir });
}
return path.dirname(pkg) + moduleNestedPath;
} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") throw err;

Expand Down
@@ -1,4 +1,4 @@
import "<CWD>/fixtures/absoluteImports/string/nested/node_modules/polyfill-a";
import "<CWD>/fixtures/absoluteImports/string/node_modules/polyfill-b";
import "<CWD>/fixtures/absoluteImports/string/nested/node_modules/polyfill-a/index.js";
import "<CWD>/fixtures/absoluteImports/string/node_modules/polyfill-b/index.js";
a;
b;

This file was deleted.

This file was deleted.

@@ -1,4 +1,4 @@
import "<CWD>/fixtures/absoluteImports/subpath/node_modules/polyfill-a/auto";
import "<CWD>/fixtures/absoluteImports/subpath/node_modules/@polyfill/b/auto";
import "<CWD>/fixtures/absoluteImports/subpath/node_modules/polyfill-a/auto.js";
import "<CWD>/fixtures/absoluteImports/subpath/node_modules/@polyfill/b/auto.js";
a;
b;
@@ -1,2 +1,2 @@
import "<CWD>/fixtures/absoluteImports/true/node_modules/polyfill-a";
import "<CWD>/fixtures/absoluteImports/true/node_modules/polyfill-a/index.js";
a;
@@ -1,4 +1,4 @@
import "<CWD>/fixtures/absoluteImports/true/node_modules/polyfill-b";
import "<CWD>/fixtures/absoluteImports/true/node_modules/polyfill-a";
import "<CWD>/fixtures/absoluteImports/true/node_modules/polyfill-b/index.js";
import "<CWD>/fixtures/absoluteImports/true/node_modules/polyfill-a/index.js";
a;
b;
@@ -1,4 +1,4 @@
import "<CWD>/fixtures/absoluteImports/true/nested/node_modules/polyfill-b";
import "<CWD>/fixtures/absoluteImports/true/node_modules/polyfill-a";
import "<CWD>/fixtures/absoluteImports/true/nested/node_modules/polyfill-b/index.js";
import "<CWD>/fixtures/absoluteImports/true/node_modules/polyfill-a/index.js";
a;
b;

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 2 additions & 1 deletion packages/babel-plugin-polyfill-corejs3/package.json
Expand Up @@ -37,7 +37,8 @@
"@babel/plugin-transform-for-of": "^7.10.4",
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
"@babel/plugin-transform-spread": "^7.13.0",
"core-js-pure": "^3.8.1"
"core-js": "^3.9.1",
"core-js-pure": "^3.9.1"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-plugin-polyfill-es-shims/package.json
Expand Up @@ -31,7 +31,8 @@
"devDependencies": {
"@babel/core": "^7.13.0",
"@babel/helper-plugin-test-runner": "^7.10.4",
"array.from": "^1.1.0"
"array.from": "^1.1.0",
"math.clz32": "^1.0.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
Expand Down
@@ -1 +1,2 @@
Array.from;
Math.clz32;
@@ -1,2 +1,4 @@
import "<CWD>/array.from/auto";
import "<CWD>/array.from/auto.js";
import "<CWD>/math.clz32/auto.js";
Array.from;
Math.clz32;
@@ -1 +1,2 @@
Array.from;
Math.clz32;
@@ -1,2 +1,4 @@
import _ArrayFrom from "<CWD>/array.from";
import _ArrayFrom from "<CWD>/array.from/index.js";
import _MathClz from "<CWD>/math.clz32/index.js";
_ArrayFrom;
_MathClz;
@@ -0,0 +1,2 @@
NOTE: <CWD> is the top-level node_modules folder
(that's how `@babel/helper-plugin-test-runner` works).
@@ -0,0 +1 @@
regeneratorRuntime.wrap(function() {});
@@ -0,0 +1,11 @@
{
"plugins": [
[
"@@/polyfill-regenerator",
{
"method": "usage-global",
"absoluteImports": true
}
]
]
}
@@ -0,0 +1,2 @@
import "<CWD>/regenerator-runtime/runtime.js";
regeneratorRuntime.wrap(function () {});
@@ -0,0 +1 @@
regeneratorRuntime.wrap(function() {});
@@ -0,0 +1,14 @@
{
"plugins": [
[
"@@/polyfill-regenerator",
{
"method": "usage-pure",
"absoluteImports": true,
"#__secret_key__@babel/runtime__compatibility": {
"useBabelRuntime": "@babel/runtime"
}
}
]
]
}
@@ -0,0 +1,3 @@
import _regeneratorRuntime from "<CWD>/@babel/runtime/regenerator/index.js";

_regeneratorRuntime.wrap(function () {});
@@ -0,0 +1 @@
regeneratorRuntime.wrap(function() {});
@@ -0,0 +1,11 @@
{
"plugins": [
[
"@@/polyfill-regenerator",
{
"method": "usage-pure",
"absoluteImports": true
}
]
]
}
@@ -0,0 +1,3 @@
import _regeneratorRuntime from "<CWD>/regenerator-runtime/runtime.js";

_regeneratorRuntime.wrap(function () {});

0 comments on commit c7ea736

Please sign in to comment.