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

ESM: CLI ESM-mode runtime #29317

Closed
wants to merge 12 commits into from
Closed

ESM: CLI ESM-mode runtime #29317

wants to merge 12 commits into from

Conversation

ndelangen
Copy link
Member

@ndelangen ndelangen commented Oct 10, 2024

What I did

I made a step towards ESM-only.

  • The CLI run the ESM dist.
  • The CLI now compiles to ESM only.
  • Applied a fix to storybook/core-path, it was faux-ESM
  • I had to apply a fix to builders because they were using CJS version of storybook/core-path.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 77.7 MB 77.7 MB 0 B -1.53 0%
initSize 146 MB 146 MB -4.37 kB -1.54 0%
diffSize 68.5 MB 68.5 MB -4.37 kB 1.45 0%
buildSize 7.1 MB 7.1 MB 0 B 1.53 0%
buildSbAddonsSize 1.79 MB 1.79 MB 0 B 1.53 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.85 MB 1.85 MB 0 B 1.5 0%
buildSbPreviewSize 271 kB 271 kB 0 B 1.51 0%
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 4.1 MB 4.1 MB 0 B 1.53 0%
buildPreviewSize 3 MB 3 MB 0 B 1.52 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 24s 6.8s -17s -226ms -0.99 -252.8%
generateTime 23s 21.4s -1s -634ms -0.39 -7.6%
initTime 16.2s 14.6s -1s -581ms -0.27 -10.8%
buildTime 9.6s 14.9s 5.2s 5.72 🔺35.4%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 7.5s 6.5s -954ms 0.3 -14.5%
devManagerResponsive 4.9s 4.4s -450ms 0.75 -10%
devManagerHeaderVisible 604ms 709ms 105ms 1.87 🔺14.8%
devManagerIndexVisible 659ms 752ms 93ms 1.87 🔺12.4%
devStoryVisibleUncached 1.2s 1s -134ms 0.37 -12.6%
devStoryVisible 632ms 745ms 113ms 1.89 🔺15.2%
devAutodocsVisible 659ms 669ms 10ms 1.98 1.5%
devMDXVisible 585ms 595ms 10ms 1.34 1.7%
buildManagerHeaderVisible 643ms 613ms -30ms 0.75 -4.9%
buildManagerIndexVisible 657ms 633ms -24ms 0.63 -3.8%
buildStoryVisible 645ms 600ms -45ms 0.15 -7.5%
buildAutodocsVisible 585ms 558ms -27ms 0.64 -4.8%
buildMDXVisible 520ms 524ms 4ms 0.52 0.8%

Greptile Summary

This pull request transitions the Storybook CLI to an ESM-only runtime, modifying key files to support ES modules and asynchronous imports.

  • Updated code/lib/cli/bin/index.cjs to use asynchronous import() instead of require()
  • Modified code/lib/cli/package.json to specify 'esm' as the only format under bundler configuration
  • Refactored code/lib/cli/src/proxy.ts to use dynamic import() for '@storybook/core/cli/bin'
  • Added error handling for asynchronous imports in both index.cjs and proxy.ts
  • These changes may impact backwards compatibility and introduce new error scenarios

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 file(s) reviewed, 5 comment(s)
Edit PR Review Bot Settings | Greptile

code/lib/cli/bin/index.cjs Show resolved Hide resolved
code/lib/cli/package.json Show resolved Hide resolved
code/lib/cli/src/proxy.ts Show resolved Hide resolved
Copy link

nx-cloud bot commented Oct 10, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit d7feafb. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@ndelangen ndelangen self-assigned this Oct 10, 2024
@ndelangen ndelangen changed the title CLI: Node ESM-mode runtime ESM: CLI ESM-mode runtime Oct 10, 2024
@@ -61,7 +60,7 @@ export const start: ViteBuilder['start'] = async ({
}) => {
server = await createViteServer(options as Options, devServer);

const previewResolvedDir = join(corePath, 'dist/preview');
const previewResolvedDir = join((await import('storybook/core-path')).corePath, 'dist/preview');
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 could be reverted if the builder when loaded had it's ESM dist loaded instead of it's CJS.

This is to ensure 'storybook/core-path' is resolved to ESM.

Comment on lines -26 to +29
require('../dist/bin/index.cjs');
import('../dist/bin/index.js').catch((error) => {
console.error(error);
process.exit(1);
});
Copy link
Member Author

Choose a reason for hiding this comment

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

By loading the dist this way, the ESM file is picked/loaded.

Comment on lines 30 to +31
"types": "./dist/index.d.ts",
"node": "./dist/index.cjs",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
"import": "./dist/index.js"
Copy link
Member Author

Choose a reason for hiding this comment

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

The CJS files are no longer generated.

@ndelangen ndelangen added the maintenance User-facing maintenance tasks label Oct 10, 2024
@ndelangen ndelangen marked this pull request as draft October 11, 2024 08:55
@ndelangen ndelangen closed this Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:normal maintenance User-facing maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant