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

Use require.resolve instead of the "resolve" package #11125

Merged
merged 5 commits into from Feb 12, 2020

Conversation

nicolo-ribaudo
Copy link
Member

Q                       A
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? Yes, removed
License MIT

The paths option is supported starting from node 8.9.0 (https://nodejs.org/api/modules.html#modules_require_resolve_request_options).

The upgrade was done using this small codemod (probably it's overkill for this PR, but I expected many more resolve.sync usages 😂)

function removeResolve({ types: t }) {
  return {
    visitor: {
      Program(path) {
        for (const el of path.get("body")) {
          if (
            el.isImportDeclaration() &&
            el.get("source").isStringLiteral({ value: "resolve" })
          ) {
            el.remove();
          }
        }
      },
      CallExpression(path) {
        if (!path.get("callee").matchesPattern("resolve.sync")) return;

        const args = [path.get("arguments.0").node];

        if (path.get("arguments").length > 1) {
          // unsupported
          if (!path.get("arguments.1").isObjectExpression()) return;

          const opts = t.objectExpression([]);
          for (const prop of path.get("arguments.1.properties")) {
            // unsupported
            if (!prop.isObjectProperty({ computed: false })) return;

            switch (prop.node.key.name) {
              case "basedir":
                opts.properties.push(
                  t.objectProperty(
                    t.identifier("paths"),
                    t.arrayExpression([prop.node.value])
                  )
                );
                break;

              // unsupported
              default: return;
            }
          }

          args.push(opts);
        }

        path.replaceWith(
          t.callExpression(
            t.memberExpression(
              t.identifier("require"),
              t.identifier("resolve")
            ),
            args
          )
        );
      }
    }
  };
}

@nicolo-ribaudo nicolo-ribaudo added the PR: Internal 🏠 A type of pull request used for our changelog categories label Feb 11, 2020
@@ -135,7 +134,7 @@ export function* loadConfig(
envName: string,
caller: CallerMetadata | void,
): Handler<ConfigFile> {
const filepath = yield* resolve(name, { basedir: dirname });
const filepath = require.resolve(name, { paths: [dirname] });
Copy link
Member Author

Choose a reason for hiding this comment

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

This is now always synchronous, but I don't think that it's a real problem.

sync: (string, {| basedir: string |}) => string;
};
}
declare var require: {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need this as require is Node.js builtin package?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, flow doesn't support require.resolve.

@nicolo-ribaudo nicolo-ribaudo merged commit aa53d90 into babel:next-8-dev Feb 12, 2020
@nicolo-ribaudo nicolo-ribaudo deleted the remove-resolve branch February 12, 2020 20:15
nicolo-ribaudo added a commit that referenced this pull request Feb 13, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Feb 13, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Feb 13, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Feb 20, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Feb 20, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Feb 23, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Feb 23, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
JLHwung pushed a commit to JLHwung/babel that referenced this pull request Feb 27, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Feb 27, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Feb 27, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Mar 3, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Mar 3, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Mar 10, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Mar 10, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Mar 11, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Mar 11, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Mar 13, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Mar 13, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Mar 22, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Mar 22, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Mar 22, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Mar 22, 2020
In #11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Apr 11, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Apr 11, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Apr 20, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
arku pushed a commit to arku/babel that referenced this pull request Apr 20, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
arku pushed a commit to arku/babel that referenced this pull request Apr 20, 2020
In babel#11125 we replaced the "require" package with the
require.resolve builtin Node.js function.
They have a slightly different behavior: `require.resolve` uses the
real file path, while "resolve" keeps symlinks as normal directories.
nicolo-ribaudo added a commit that referenced this pull request Apr 21, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Apr 21, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Apr 21, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request Apr 22, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request May 8, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
nicolo-ribaudo added a commit that referenced this pull request May 9, 2020
* Remove resolve from package.json

* Add flow typings for require.resolve

* Run codemod

* Remove other usages

* Update tests
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 14, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Internal 🏠 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants