Open12
都道府県のやつのメモ

あとでバージョンかいとこ
❯ node --version
v18.2.0
React 18
Emotion
Storybook
Vite

Storybook の起動とビルド
Node v17 との組み合わせで ERR_OSSL_EVP_UNSUPPORTED
エラーが出る
NODE_OPTIONS=--openssl-legacy-provider
をつけてとりあえず対応
"storybook": "NODE_OPTIONS=--openssl-legacy-provider start-storybook -p 6006",
"build-storybook": "NODE_OPTIONS=--openssl-legacy-provider build-storybook"

Global Decorator
Emotion のテーマを使うのに、Global Decorator を使えばいいのかなと思ってとりあえずサンプルに書いてある通りに preview.js
に書きたしてみたらエラーになった
import React from 'react';
export const decorators = [
(Story) => (
<div style={{ margin: '3em' }}>
<Story />
</div>
),
];
Failed to fetch dynamically imported module: http://localhost:6006/.storybook/preview.js
TypeError: Failed to fetch dynamically imported module: http://localhost:6006/.storybook/preview.js
===
サーバー側のコンソールを見たら Vite がエラーメッセージを出してた
12:21:30 AM [vite] Internal server error: Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.
拡張子ちゃんとしてよってことか。preview.jsx
にリネームしてみたら動いた。わーい。
↓は必要なさそうなので消しておいた
import React from 'react';

ThemeProvider
Decorator で ThemeProvider を指定してみる
import { theme } from '../src/themes/theme';
import { ThemeProvider } from '@emotion/react';
export const decorators = [
(Story) => (
<ThemeProvider theme={theme}>
<Story />
</ThemeProvider>
),
];
動いた

インポート
次はインポートの ../src/
を src/
で動くようにしたい。何もしなかったら見つけられないエラーになる。
12:36:59 AM [vite] Internal server error: Failed to resolve import "src/themes/theme" from ".storybook/preview.jsx". Does the file exist?
==
このあたり読みながらやってみようー

memo

これでいけた
main.js
const path = require('path');
module.exports = {
...
viteFinal: async (config) => {
config.resolve.alias = {
...(config.resolve.alias || {}),
src: path.resolve(__dirname, '../src'),
};
return config;
},
};

でも、一回目はエラーになるのなんだろうなぁ?一回起動してエラーになって、再起動したらいける。キャッシュ周りのなにかなのかなぁ?

memo
msw入れた。Dynamic Import使った。