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(cli): remove yarn install output and show spinner #236

Conversation

eunjae-lee
Copy link
Contributor

@eunjae-lee eunjae-lee commented Jul 26, 2023

What?

create command runs npm install, and we don't have to show its result as long as successful. However, it'd be nice to show a spinner to tell that it's in progress.

Why?

JIRA: EXT-1194

How to test? (optional)

Screenshot.2023-07-26.at.11.39.48.mp4

@vercel
Copy link

vercel bot commented Jul 26, 2023

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

Name Status Preview Comments Updated (UTC)
plugin-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 1, 2023 9:49am

Copy link
Contributor

Choose a reason for hiding this comment

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

These errors thrown by GitHub actions were a matter of format/eslint?

Copy link
Contributor

@demetriusfeijoo demetriusfeijoo Jul 28, 2023

Choose a reason for hiding this comment

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

@eunjae-lee, as we discussed in our call, both readFileSync and writeFileSync will be removed in another PR, so there is no need to remove it in this one.

@eunjae-lee eunjae-lee requested a review from BibiSebi July 31, 2023 08:56
@eunjae-lee
Copy link
Contributor Author

I'd love to get another eye on here @BibiSebi :)

command,
{ spinnerMessage, ...options } = {},
) => {
const ora = (await import('ora')).default
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to import in this way? I suppose there is a reason I am not aware of.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good question!

You know how we support both CJS and ESM with @storyblok/field-plugin.
At packages/field-plugin/package.json, you can see this:

Screenshot 2023-07-31 at 13 57 34

In case of the CLI, we're transpiling our code to CJS only. (But this reminds me that we need to re-research and see if ESM output is good enough for the CLI distribution)

Anyway, if you run yarn build:cli, at packages/cli/dist/main.cjs, you get something like:

...
.... = require('ora');

And this is problematic. The library ora is providing ESM only. Whereas @storyblok/field-plugin provides both CJS and ESM, ora has decided to provide ESM only. There are some library authors who are doing this intentionally, in order to push the JS ecosystem to transit from CJS to ESM more quickly.

Back to require('ora')... so we're requireing the ESM code, which doesn't make sense. That's why it failed. The bundling was okay, but once you run npx field-plugin, node.js will fail to require it with ERR_REQUIRE_ESM error code.

So, how do we solve this issue? I'm doing this:

const ora = (await import('ora')).default

This is called "dynamic import". It's different from the topmost static import statements like this:

import ora from 'ora'

This dynamic import is supported in Node.js (probably since v14). Although it looks similar, but you can use dynamic import within CJS code. So that's the workaround.

The ideal situation would be for us to bundle our CLI code into ESM (instead of CJS), and we would be able to import ora from 'ora'.

Let me know if anything is unclear!

Copy link
Contributor

Choose a reason for hiding this comment

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

Everything super clear, I guess I do not have too much knowledge on the differences between the modules (CJS vs ESM) so I would be glad to support you in the re-research :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BibiSebi BibiSebi self-requested a review July 31, 2023 14:12
Copy link
Contributor

@BibiSebi BibiSebi left a comment

Choose a reason for hiding this comment

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

thank you for your effort on this 💯 looking nice 🥇 and thank you for the long explanation on CJS and ESM. I think for now it's is fine and we can create a ticket to see if there is a way to switch to ESM only

@eunjae-lee eunjae-lee merged commit c1dbd85 into main Aug 1, 2023
@eunjae-lee eunjae-lee deleted the EXT-1194-hide-yarn-install-output-when-adding-single-field-plugin branch August 1, 2023 09:53
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.

3 participants