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

Switch Storybook build to vite builder #745

Merged
merged 17 commits into from
Dec 8, 2024
Merged

Conversation

sergei-maertens
Copy link
Member

@sergei-maertens sergei-maertens commented Nov 29, 2024

Partly closes #724

Migrate Storybook to ViteJS

To figure out still

  • Address charset issue
    [vite:css] @charset must precede all other statements
    37 |  .fa-regular,
    38 |  .fab,
    39 |  .fa-brands {
       |  ^^^^^^^^^^^^
    40 |    -moz-osx-font-smoothing: grayscale;
       |  ^^^^^
    41 |    -webkit-font-smoothing: antialiased;
    
  • Fix design tokens import (no default import), /design-tokens/dist/tokens.js
  • Fix loading font assets
  • Delete the webpack builder dependency
  • Fix test-runner config - it still has require imports
  • Investigate visual regressions

Copy link

codecov bot commented Nov 29, 2024

Codecov Report

Attention: Patch coverage is 68.00000% with 8 lines in your changes missing coverage. Please review.

Project coverage is 79.44%. Comparing base (06c9bc4) to head (f058d80).
Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
src/env.mjs 64.28% 5 Missing ⚠️
src/index.jsx 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #745      +/-   ##
==========================================
- Coverage   79.57%   79.44%   -0.13%     
==========================================
  Files         237      238       +1     
  Lines        4964     4973       +9     
  Branches     1350     1355       +5     
==========================================
+ Hits         3950     3951       +1     
- Misses        983      992       +9     
+ Partials       31       30       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sergei-maertens sergei-maertens changed the title Chore/724 sb to vitejs Switch Storybook build to vite builder Dec 2, 2024
@sergei-maertens sergei-maertens marked this pull request as ready for review December 2, 2024 22:24
@sergei-maertens sergei-maertens marked this pull request as draft December 2, 2024 22:36
@sergei-maertens sergei-maertens marked this pull request as ready for review December 3, 2024 08:22
We can detect the environment based on the presence of 'process' or
not, and adapt the reading of environment variables based on the
detected environment.

This means that we currently have to duplicate the configuration,
but as long as this is a temporary measure, we should be fine.
require doesn't exist in Vite, but import.meta can be used
instead.
And silence the TS error about guard, it's fine at runtime.
Having looked into various configurations and plugins to automatically
get the CommonJS transformed into ESM breaks either dev mode or
production builds of Storybook.

There are some external plugins for CommonJS transformations, but they
either looks sketchy or don't seem to work in obvious ways (in
particular @rollup/plugin-commonjs wasn't a success).

So I've opted for the simple solution - write a custom plugin that
just replaces the CJS export with an ESM export and it works
in both dev and prod modes without any additional fuss. In the
future, we will process other formats of these token exports,
for which the design-token-editor package needs to be upgraded.
Vite copies the contents of public/\* as is into dist, which conflicts
with the storybook static build. Furthermore, for how we use and
distribute the SDK, the index.html and sdk.html are not relevant
anyway, they're only used during local development with CRA.

The base change from / to ./ should make things work better when
deployed/hosted on an arbitrary subpath.
We cannot yet move the fonts to the public dir or change the SCSS that
loads the fonts, as that would break the actual CRA-based build which
now correctly ships the fonts.

Because of the relative URLs used in the font-face declaration, Vite
expects the fonts to exist in the root of the project.
The formio styles include an invalid at-rule in the middle of the file,
which vite-css correctly warns about.

The workaround updates the SCSS to use @use rather than @import to
ensure the CSS is included rather than referenced, and this in turn
makes it compatible with the sass preprocessor options to disable the
charset at-rule, effectively getting rid of the warning.
CRA seems to properly translate the relative path and include the files
while Vite requires some extra hints to properly point to the package
location before it can find the font files.

Unfortunately, both setups are not compatible, so in the vite config
we override the sass-variable to pass down to the sass module import,
where the default is set to the current, legacy behaviour.
Since we use the Vite builder instead.
The preVisit hook is flaky, especially with local development
it's bad, but other people seem to be running into issues too
(see https://github.com/storybookjs/test-runner/issues/442\).

Let's see if this setup still has viewport support in Chromatic,
which is what we care about most for snapshotting.
lodash/template requires the window._ global to be defined, otherwise
the templates don't render properly.

Failing to provide this breaks the formio file upload stories.
When running tests using @storybook/test-runner, we need to apply
the same viewport to get the proper responsive elements as we
get in the real browser/storybook. However, we just ripped out
the viewport support because it's super flaky. So, we disable
these tests here, but they should still run on Chromatic so
we still get feedback there (provided we don't disable the
Chromatic snapshotting).
Vite/Rollup seem to be much more aggressive in handling imports with
side-effects, resulting in the Dutch locale not getting registered on
the flatpickr instance.

Changing the import to a named import and manually assigning it fixes
the issues in the static storybook build, while not breaking the CRA
build (verified locally by creating a production build and using that
with the real backend).
This is due to a difference between webpack and Vite/Rollup - CRA/wp
is configured to not mangle class names in the minification process,
while Vite/Rollup do mangle them. One option is to disable class name
mangling which would probably only have a minor effect on the bundle
sizes, however it's just technically more correct to point to the
class itself rather than the name of the class in the runtime time,
as this will be more robust for future build tooling changes.
@sergei-maertens sergei-maertens merged commit 4a43906 into main Dec 8, 2024
13 of 15 checks passed
@sergei-maertens sergei-maertens deleted the chore/724-sb-to-vitejs branch December 8, 2024 17:01
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.

Update toolchain (Jest/Vitest/ViteJS/TS)
1 participant