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

fix: remove createRequire for package json #978

Merged
merged 8 commits into from
Oct 21, 2024

Conversation

tylersayshi
Copy link
Contributor

follow up to this: #974 (comment)

Copy link

vercel bot commented Oct 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
waku ✅ Ready (Inspect) Visit Preview Oct 21, 2024 1:07am

Copy link

codesandbox-ci bot commented Oct 19, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link
Contributor

@ojj1123 ojj1123 left a comment

Choose a reason for hiding this comment

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

👍👍

Comment on lines 10 to 11
banner: {
js: `import { createRequire } from 'module'; const require = createRequire(import.meta.url);`,
Copy link
Contributor

Choose a reason for hiding this comment

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

You removed createRequire, but Is it needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

prompts dynamically requires readline causing this error when the banner is omitted:

Error: Dynamic require of "readline" is not supported

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could look into moving to inquirer.js from prompts:

https://github.com/SBoudrias/Inquirer.js

It seems like it is more actively maintained, so I'd imagine the esm support is more up to date and we could then remove this banner.

Copy link
Contributor

@ojj1123 ojj1123 Oct 19, 2024

Choose a reason for hiding this comment

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

Yeah It feels like prompts is not almost maintained. But both of them have similar features. I wonder @dai-shi thoughts :)

Copy link
Owner

Choose a reason for hiding this comment

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

Previously, I rejected the idea #339 by @himself65 because I thought it was too early.
But now that we have more active contributors, I think the time is right.
I don't think prompts is too bad, but if you guys have a better alternative, and feel like maintaining it, I'm fine of changing the dependency library, or completely re-implementing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried both enquirer and inquirer and each seems to use dynamic requires of their dependencies, so we'd need to keep this banner even if switching.

So, I don't really see a strong motivator for changing off prompts either yet.

This issue: terkelg/prompts#393 if it get's worked on should be enough to solve any of the current concerns we have now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, That's enough since create-waku doesn't have the logic of complex CLI yet.
So I don't have enough motivation on changing other deps.

// keep original require to avoid
// bundling the whole package.json by `@vercel/ncc`
const packageJson = createRequire(import.meta.url)('../package.json');
const packageJson = await import('../package.json');
Copy link
Owner

Choose a reason for hiding this comment

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

So, is this syntax supported by tsup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep! I think esbuild is technically what supports it under the hood, but still the answer is yes, it's supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually I was wrong here ^

This errors because of the json import without the attribute, so I just changed it to use require which is defined in the banner

the error:

TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///.../waku/packages/create-waku/package.json" needs an import attribute of type "json"

// bundling the whole package.json by `@vercel/ncc`
const packageJson = createRequire(import.meta.url)('../package.json');
// eslint-disable-next-line @typescript-eslint/no-require-imports
const packageJson = require('../package.json');
Copy link
Contributor

Choose a reason for hiding this comment

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

you can use JSON.parse(fs.readFileSync(..)) if wanna remove require here

Copy link
Contributor Author

@tylersayshi tylersayshi Oct 20, 2024

Choose a reason for hiding this comment

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

Tried updating this from my phone. I think it's right but I'll need to correct the prettier issues from my laptop later haha

Thanks for the tip though!

Copy link
Owner

Choose a reason for hiding this comment

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

I thought there was a motivation of preferring require to readFileSync? @himself65

Copy link
Contributor

Choose a reason for hiding this comment

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

Nope, I just think it’s not a familiar way to use require in ESM

Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

Thanks guys!!

@dai-shi dai-shi merged commit ef95f93 into dai-shi:main Oct 21, 2024
24 checks passed
@dai-shi
Copy link
Owner

dai-shi commented Oct 21, 2024

Got an error:

$ npm create waku@latest            
Need to install the following packages:
  [email protected]
Ok to proceed? (y) 
✔ Project Name … waku-project
Setting up project...

Installing dependencies by running npm install...
Error: ENOENT: no such file or directory, open '../package.json'
    at Object.openSync (node:fs:603:3)
    at readFileSync (node:fs:471:35)
    at Fx (file:///Users/daishi/.npm/_npx/a0cd0342f4ca0990/node_modules/create-waku/dist/index.js:119:41) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '../package.json'
}

I think we need to use import.meta.url.

dai-shi pushed a commit that referenced this pull request Oct 21, 2024
fix for #978 (comment)

Also fixed an issue where the default folder name was not showing on
conflict

---------

Co-authored-by: Tyler <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants