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

chore(gatsby-core-utils): create-require-from-path to TS #22050

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions packages/gatsby-core-utils/src/create-require-from-path.js

This file was deleted.

22 changes: 22 additions & 0 deletions packages/gatsby-core-utils/src/create-require-from-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Module from "module"
import path from "path"

// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
const createRequire = (filename: string): NodeRequire => {
if (`createRequire` in Module) {
return Module.createRequire(filename)
}
if (`createRequireFromPath` in Module) {
return Module.createRequireFromPath(filename)
}

const mod = new Module(filename, null)

mod.filename = filename
mod.paths = Module._nodeModulePaths(path.dirname(filename))
mod._compile(`module.exports = require;`, filename)

return mod.exports
}

export default createRequire
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export default createRequire
export { createRequire as createRequireFromPath }

and adjust import in https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-core-utils/src/index.js to use named export