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

[Babel 8] Use more native fs methods #16459

Merged
merged 5 commits into from May 2, 2024

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented May 1, 2024

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

In this PR we use the following fs native methods available on Node.js 18+ for Babel 8.

  • fs.mkdirSync(_, { recursive: true }). The polyfill is already available, in this PR we replaced more make-dir usage
  • fs.rmSync(_, { force: true, recursive: true }), a polyfill is also added.
  • fs.readdirSync(_, { recursive: true, withFileTypes: true })

@JLHwung JLHwung added the PR: Internal (next major) 🏠 A type of pull request used for our changelog categories for next major release label May 1, 2024
@@ -119,9 +119,7 @@ export default async function ({

const files = util.readdir(dirname, cliOptions.includeDotfiles);
for (const filename of files) {
const src = path.join(dirname, filename);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

util.readdir now returns the joined path, so we don't have to join it here.

@babel-bot
Copy link
Collaborator

babel-bot commented May 1, 2024

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/56803

},
// @ts-expect-error improve @types/fs-readdir-recursive typings
[],
dirname,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use dirname as the prefix argument so that readdirRecursive will return the joined path without extra loop or .map. Unfortunately the typing here
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/d152bf80d3c7df9104dbac9e369cdd64110601a7/types/fs-readdir-recursive/index.d.ts#L1-L4
only documented the first two parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +43 to +44
// readdirSyncRecursive conducts BFS, sort the entries so we can match the DFS behaviour of fs-readdir-recursive
// https://github.com/nodejs/node/blob/d6b12f5b77e35c58a611d614cf0aac674ec2c3ed/lib/fs.js#L1421
Copy link
Member

Choose a reason for hiding this comment

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

Q: does this matter in practice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ordering are different as files in sub folders will be visited after files in top level folders, see #16459 (comment)

(!filter || filter(filename))
);
})
.map(dirent => path.join(dirent.path, dirent.name))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Dirent#path has been deprecated in favour of Dirent#parentPath, however to use parentPath we will have to further bump Node.js requirements to ^18.20.0 || ^20.12.0 || >= 21.4.0. I would like to hear about your opinions.

Copy link
Member

Choose a reason for hiding this comment

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

This all seems ok to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I will open a new PR to bump node and replace Dirent#path usage once the node typings are ready: DefinitelyTyped/DefinitelyTyped#69490

.map(dirent => path.join(dirent.path, dirent.name))
// readdirSyncRecursive conducts BFS, sort the entries so we can match the DFS behaviour of fs-readdir-recursive
// https://github.com/nodejs/node/blob/d6b12f5b77e35c58a611d614cf0aac674ec2c3ed/lib/fs.js#L1421
.sort(alphasort)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Babel cli does not rely on the filenames ordering. The sort calls is meant to preserve the test log order.
If we want to change the ordering in Babel 8, we can remove this and the glob.sync usage in options.ts.

@JLHwung JLHwung merged commit fed85fb into babel:main May 2, 2024
51 checks passed
@JLHwung JLHwung deleted the use-native-fs-methods-babel-8 branch May 2, 2024 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Internal (next major) 🏠 A type of pull request used for our changelog categories for next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants