Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully resolve absolute imports #79

Merged
merged 5 commits into from Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR: we need to update nativeRequireResolve to use cheap semver so versions like 8.10 does not fall to requireResolve.

// $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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clz32 is different from .from because it specifies package.json#exports. This test (correctly) shows that it's ignored, because Node.js doesn't consider it when loading absolute paths.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correctly shows that what is ignored? I’m a bit confused here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I’m pretty sure package.json exports aren’t considered by node unless you’re using a bare identifier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full output is

import "<CWD>/array.from/auto.js";
import "<CWD>/math.clz32/auto.js";
Array.from;
Math.clz32;

I added clz32 to the test to check that it doesn't inject import "<CWD>/math.clz32/auto" (because of the exports map which includes "/auto" and not "/auto.js"), since exports should be ignored when using absolute paths.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh ok. why is this preferable over bare identifiers? Absolute paths aren’t portable across machines - doesn’t this increase the likelihood they’ll end up in a published package?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can indeed rely on that, and using peerDeps, you should.

Copy link

@merceyz merceyz Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really can't, If I were to tell next.js to transpile @material-ui/core with the core-js version it has access to webpack will throw because it can't find the files in core-js@2

.
└── node_modules
    ├── next.js
    ├── core-js@3.9
    └── @material-ui/core
        └── node_modules
            └── core-js@2

For your own project sure, you can do it with peer dependencies but that would force all projects using Next.js, create-react-app, and storybook etc. to explicitly depend on @babel/runtime and/or core-js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s because core-js should be a peer dep of material-ui. I’d file a bug there.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@material-ui/core doesn't actually depend on core-js, I used that name as an example because people usually don't do well with abstracts. If it was a peer dependency then it would be even worse because the root couldn't satisfy both core-js@3.9 and core-js@2 at the same time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the point - if it peer depends on core-js 2, even implicitly, then it's incompatible with your project and you simply can't use it.

@@ -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 () {});