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

prebundle the preview #19718

Merged

Conversation

ndelangen
Copy link
Member

@ndelangen ndelangen commented Nov 2, 2022

cleanup up version of #19678

Split the work required to get the preview to run into steps, where we can do some of the work ahead of time, so the user's builder has to do less work.

The way we slit the work is by taking a bunch of packages and pre-bundle them together, into a single module.

At the builder stage, we then swap packages to shims. These shims exports everything that the package is supposed to export, but it does so by de-structuring it off the global namespace.

This causes the builder to have to do less work, because the dependency graph is more shallow.

@ndelangen ndelangen self-assigned this Nov 2, 2022
@ndelangen ndelangen added the maintenance User-facing maintenance tasks label Nov 2, 2022
Copy link
Contributor

@JReinhold JReinhold left a comment

Choose a reason for hiding this comment

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

Based on your walkthrough it looks good, but honestly I'm not really able to scan through this code and spot errors.

Copy link
Member

@tmeasday tmeasday left a comment

Choose a reason for hiding this comment

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

I think we need to do the vite bit before we can merge this, no? Also would like to talk through the storyshots changes maybe

code/lib/builder-webpack5/src/index.ts Outdated Show resolved Hide resolved
@ndelangen ndelangen added the ci:merged Run the CI jobs that normally run when merged. label Nov 11, 2022
ndelangen and others added 8 commits November 18, 2022 15:03
…-out-plan-for-package-structure-rework

# Conflicts:
#	code/addons/links/package.json
#	code/addons/storyshots/storyshots-puppeteer/package.json
#	code/lib/api/package.json
#	code/lib/builder-vite/package.json
#	code/lib/builder-vite/src/index.ts
#	code/lib/builder-webpack5/package.json
#	code/lib/client-api/package.json
#	code/lib/codemod/package.json
#	code/lib/core-client/package.json
#	code/lib/core-server/package.json
#	code/lib/csf-tools/package.json
#	code/lib/source-loader/package.json
#	code/lib/store/package.json
#	code/lib/types/package.json
#	code/ui/blocks/package.json
#	code/ui/components/package.json
#	code/yarn.lock
@IanVS
Copy link
Member

IanVS commented Nov 22, 2022

@ndelangen, I took a slightly different approach to generate exports.ts for @storybook/preview, using the files in /global as a basis, rather than a separate mapping file (though I still needed a map for package name to global variable name).

I think this is working in the vite builder. I verified that everything works great when building a sandbox. In dev, though, I'm seeing a case where the browser opens before the preview builder is finished building, and that same strange behavior we saw initially where requests would hang for a minute or two before finishing.

I didn't remove the extra re-exports in preview, though I think they're no longer needed if we take this approach.

<script type="module">
/* eslint-disable import/no-absolute-path, import/extensions, import/no-unresolved */

import '/sb-preview/runtime.mjs';
Copy link
Member Author

@ndelangen ndelangen Nov 22, 2022

Choose a reason for hiding this comment

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

@IanVS I think this should really be:

import './sb-preview/runtime.mjs';

WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

In this case I think the two urls are equivalent since iframe.html is at the root of the site, right? The relative path looks a little odd to me, personally, but I don't feel strongly so long as both work correctly, including when the site is served at a subpath (e.g. github pages).

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not always the root of the site

@ndelangen
Copy link
Member Author

@IanVS I changed the code a bit so it mimics the manager more

@ndelangen
Copy link
Member Author

@tmeasday vite is implemented!

code/lib/core-server/src/build-dev.ts Show resolved Hide resolved
options,
router,
server,
channel: serverChannel,
Copy link
Member

Choose a reason for hiding this comment

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

The docs for builders will need to be updated.

Copy link
Member Author

Choose a reason for hiding this comment

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

true, will do this in a follow-up PR

code/lib/core-server/src/dev-server.ts Show resolved Hide resolved
@@ -0,0 +1,94 @@
# Preview (Web)

This is the main API for the (web) version of the Storybook Preview.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this README should also explain a bit about the globals?

* If you forget to do either, TypeScript will complain.
*
* This `definitions.ts` file is consumed by the `builder-manager` package,
* The `runtime.ts` file is used inside the manager's browser code runtime.
Copy link
Member

Choose a reason for hiding this comment

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

Copy-paste errors from manager code

code/lib/preview/src/globals/types.ts Show resolved Hide resolved
code/lib/preview/src/modules/addons.ts Show resolved Hide resolved
code/lib/builder-vite/src/index.ts Show resolved Hide resolved
configFile: false,
cacheDir: 'node_modules/.cache/.vite-storybook',
root: path.resolve(options.configDir, '..'),
// Allow storybook deployed as subfolder. See https://github.com/storybookjs/builder-vite/issues/238
base: './',

Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think this newline was accidental.

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

Looking good! Some minor concerns on top of everybody else's comments

code/lib/builder-webpack5/src/index.ts Outdated Show resolved Hide resolved
code/lib/core-server/src/index.ts Show resolved Hide resolved
code/ui/components/src/Loader/Loader.tsx Outdated Show resolved Hide resolved
code/ui/manager/src/components/preview/preview.tsx Outdated Show resolved Hide resolved
code/ui/manager/src/containers/menu.tsx Show resolved Hide resolved
@ndelangen ndelangen merged commit d491ad3 into next Nov 22, 2022
@ndelangen ndelangen deleted the norbert/sb-798-figure-out-plan-for-package-structure-rework branch November 22, 2022 14:40
@ndelangen ndelangen removed the ci:merged Run the CI jobs that normally run when merged. label Nov 22, 2022
@ndelangen ndelangen restored the norbert/sb-798-figure-out-plan-for-package-structure-rework branch November 22, 2022 16:30
@ndelangen ndelangen deleted the norbert/sb-798-figure-out-plan-for-package-structure-rework branch November 25, 2022 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance User-facing maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants