Open10

Next.jsアプリのStoryBookのビルドをWebpackからViteに置き換える

Hirotaka MiyagiHirotaka Miyagi

StoryBookの導入から始める場合は npx sb init --builder @storybook/builder-vite で始められるようだが、既に導入済みの状態で実行しても .storybook/main.js があるからかスキップされてしまった。
そのため手動で yarn add -D "storybook-builder-vite@*" を実行する。
viteがないと怒られたので yarn add -D "vite@*" も実行。
そしてビルダーを 'storybook-builder-vite' に置き換える。

https://github.com/MH4GF/log.mh4gf.dev/blob/52b9dfb3ffe4698c85aed3588bd92b2132f3bd9d/.storybook/main.js#L30-L32

この時点でWebpackFinalで追加しているWebpackの設定は不要になることが予想されるが、一旦後回し。

Hirotaka MiyagiHirotaka Miyagi

ビルドを試してみると以下のようなエラーが出た。

[rollup-plugin-dynamic-import-variables] Unexpected token (8:20)
file: /home/runner/work/log.mh4gf.dev/log.mh4gf.dev/.storybook/preview.js:8:20

ref: https://github.com/MH4GF/log.mh4gf.dev/runs/6139341449?check_suite_focus=true

これは…next/imageをStoryBookで表示するためのワークアラウンドのやつだ コミット

https://github.com/MH4GF/log.mh4gf.dev/blob/712b240048f72392d47a8a9c334cd3bd586c7205/.storybook/preview.js#L8-L8

JSXなのに拡張子がjsなのがよくなさそう?と思いjsxに直してみると解消された。(別のエラーは出る)
https://github.com/MH4GF/log.mh4gf.dev/pull/60/commits/52b9dfb3ffe4698c85aed3588bd92b2132f3bd9d

Hirotaka MiyagiHirotaka Miyagi

次は以下のようなエラーが出た。

[vite]: Rollup failed to resolve import "~/src/model/ArticleModel" from "src/components/Article/index.stories.tsx".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
ERR! Error: [vite]: Rollup failed to resolve import "~/src/model/ArticleModel" from "src/components/Article/index.stories.tsx".

ref: https://github.com/MH4GF/log.mh4gf.dev/runs/6139371425?check_suite_focus=true

importが解決できないとのことなので、 ~ から始まるpathの問題か?

Hirotaka MiyagiHirotaka Miyagi

しかしChromaticへのPublishでエラーになってしまう。

✖ Failed to verify your Storybook

Error communicating with the Chromatic API. Check if your Chromatic client is up-to-date.
In case of an outage, we provide status updates at https://twitter.com/chromaui
Check http://status.chromatic.com for service uptime.

→ INTERNAL_SERVER_ERROR: JavaScript failed to load at https://624991c239d2ba004adeffdf-phakhuihxt.capture.chromatic.com/iframe.html

ref: https://github.com/MH4GF/log.mh4gf.dev/runs/6140101847?check_suite_focus=true

/iframe.html が開けないっぽいな

ローカルで npx http-server storybook-static を実行して、ビルド結果を立ち上げてみても同様のエラーが出る

Uncaught TypeError: Cannot redefine property: default
    at Function.defineProperty (<anonymous>)
    at preview.jsx:6:8
Hirotaka MiyagiHirotaka Miyagi

ビルドは通るようになったが、いくつかのコンポーネントが開けなくなっているようだ…なんで…

https://www.chromatic.com/build?appId=624991c239d2ba004adeffdf&number=110

Reactのproduction buildなのでエラーはこのページで確認することになる

https://reactjs.org/docs/error-decoder.html/?invariant=130&args[]=object&args[]=

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

:thinking:

Hirotaka MiyagiHirotaka Miyagi

これはビルダーをWebpackに戻して yarn build-storybook && npx http-server storybook-static をすると問題なく開けるので、viteの設定の問題だなあ