{"id":32646,"date":"2023-05-07T22:19:27","date_gmt":"2023-05-08T05:19:27","guid":{"rendered":"https:\/\/www.uxpin.com\/studio\/?p=32646"},"modified":"2024-05-15T02:11:18","modified_gmt":"2024-05-15T09:11:18","slug":"0%e3%81%8b%e3%82%89%e4%bd%9c%e3%82%8buxpin-merge-typescript-storybook%e3%81%ae%e7%92%b0%e5%a2%83-%e3%81%9d%e3%81%ae2","status":"publish","type":"post","link":"https:\/\/www.uxpin.com\/studio\/jp\/blog-jp\/0%e3%81%8b%e3%82%89%e4%bd%9c%e3%82%8buxpin-merge-typescript-storybook%e3%81%ae%e7%92%b0%e5%a2%83-%e3%81%9d%e3%81%ae2\/","title":{"rendered":"0\u304b\u3089\u4f5c\u308bUXPin Merge + TypeScript + Storybook\u306e\u74b0\u5883 \u305d\u306e2"},"content":{"rendered":"\r\n
\u307e\u305a\u306f\u3053\u3061\u3089\u306e\u30b3\u30de\u30f3\u30c9\u3002<\/p>\r\n\r\n\r\n
\r\nyarn add -D @types\/css-modules storybook-css-modules-preset\r\n<\/pre><\/div>\r\n\r\n\r\nhttps:\/\/www.npmjs.com\/package\/@types\/css-modules<\/a><\/p>\r\n\r\n\r\n\r\nhttps:\/\/www.npmjs.com\/package\/storybook-css-modules-preset<\/a><\/p>\r\n\r\n\r\n\r\n
@types\/css-modules\u306e\u8aac\u660e<\/h3>\r\n\r\n\r\n\r\n
TypeScript \u306e\u74b0\u5883\u3067CSS Modules\u3092\u4f7f\u3046\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u304c\u51fa\u307e\u3059\u3002<\/p>\r\n\r\n\r\n
\r\nCannot find module '.\/foo.module.css' or its corresponding type declarations.\r\n<\/pre><\/div>\r\n\r\n\r\n\u3053\u308c\u3092\u89e3\u6d88\u3059\u308b\u305f\u3081\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\r\n\r\n\r\n\r\n
\u4e2d\u8eab\u306f\u5b9f\u8cea\u3053\u308c\u3060\u3051\u306a\u306e\u3067\u3001\u81ea\u5206\u3067d.ts<\/code>\u3092\u3069\u3053\u304b\u306b\u7528\u610f\u3057\u3066\u3082\u826f\u3044\u3093\u3067\u3059\u304c\u2026\u2026\u7b11<\/p>\r\n\r\n\r\n\r\ninterface CSSModule {\r\n \/**\r\n * Returns the specific selector from imported stylesheet as string.\r\n *\/\r\n [key: string]: string;\r\n}\r\n<\/pre><\/div>\r\n\r\n\r\n
Storybook\u306e\u6e96\u5099\u306e\u7d9a\u304d<\/h3>\r\n\r\n\r\n\r\n
storybook-css-modules-preset<\/code>\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u3060\u3051\u3067\u306f\u99c4\u76ee\u306a\u306e\u3067\u3001.storybook\/main.js<\/code>\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u7de8\u96c6\u3057\u307e\u3059\u3002<\/p>\r\n\r\n\r\n\r\n module.exports = {\r\n "stories": [\r\n "..\/stories\/**\/*.stories.mdx",\r\n "..\/stories\/**\/*.stories.@(js|jsx|ts|tsx)"\r\n ],\r\n "addons": [\r\n "@storybook\/addon-links",\r\n- "@storybook\/addon-essentials"\r\n+ "@storybook\/addon-essentials",\r\n+ "storybook-css-modules-preset"\r\n ]\r\n }\r\n<\/pre><\/div>\r\n\r\n\r\n\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u308f\u305amain.js<\/code>\u306e\u4e2d\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u8a18\u8ff0\u3092\u3057\u3066\u3082\u826f\u3044\u306e\u3067\u3059\u304c\u3001\u4f7f\u3046\u65b9\u304c\u7c21\u5358\u306a\u306e\u3067\u4eca\u56de\u306f\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\r\n\r\n\r\n\r\nwebpackFinal: (config) => {\r\n \/\/ webpack\u306e\u8a2d\u5b9a\r\n}\r\n<\/pre><\/div>\r\n\r\n\r\nhttps:\/\/storybook.js.org\/docs\/react\/configure\/webpack<\/a><\/p>\r\n\r\n\r\n\r\n
UXPin\u306e\u6e96\u5099<\/h3>\r\n\r\n\r\n\r\n
uxpin.webpack.config.js<\/code> \u3092\u7de8\u96c6\u3057\u307e\u3059\u3002<\/p>\r\n\r\n\r\n\r\n {\r\n test: \/\\.css$\/,\r\n use: [\r\n {\r\n loader: 'style-loader',\r\n },\r\n {\r\n loader: 'css-loader',\r\n options: {\r\n- importLoaders: 2\r\n+ importLoaders: 2,\r\n+ modules: true\r\n },\r\n },\r\n ],\r\n },\r\n<\/pre><\/div>\r\n\r\n\r\ncss-loader<\/code> \u306e modules<\/code> \u3092 true \u306b\u3059\u308b\u3060\u3051\u3067OK\u3002<\/p>\r\n\r\n\r\n\r\nCSS Modules\u306e\u9069\u7528<\/h2>\r\n\r\n\r\n\r\n
\u3042\u3068\u306f\u3072\u305f\u3059\u3089\u540c\u3058\u4f5c\u696d\u306a\u306e\u3067\u3001Button.tsx\u3092\u4f8b\u306b\u3057\u3066\u8aac\u660e\u3057\u307e\u3059\u3002<\/a><\/p>\r\n\r\n\r\n\r\nCSS\u306eimport\u3092CSS Modules\u306e\u4f5c\u6cd5\u306b\u306a\u3089\u3063\u3066\u5909\u66f4\u3002<\/p>\r\n\r\n\r\n
\r\n- import '.\/button.css';\r\n+ import styles from '.\/button.module.css';\r\n<\/pre><\/div>\r\n\r\n\r\nclassName\u304c\u5358\u306a\u308b\u6587\u5b57\u5217\u3068\u3057\u3066\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u7b87\u6240\u3092CSS Modules\u306e\u4f5c\u6cd5\u306b\u306a\u3089\u3063\u3066\u5909\u66f4\u3002<\/p>\r\n\r\n\r\n
\r\n- const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';\r\n+ const mode = primary ? styles['storybook-button--primary'] : styles['storybook-button--secondary'];\r\n<\/pre><\/div>\r\n\r\n\r\n\u3061\u306a\u307f\u306b\u4e00\u822c\u7684\u306b\u306f styles.foo<\/code> \u306e\u66f8\u5f0f\u3060\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u4eca\u56de\u306e\u3088\u3046\u306b\u30af\u30e9\u30b9\u540d\u304c\u30b1\u30d0\u30d6\u30b1\u30fc\u30b9\u306b\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u306f styles.['foo-bar']<\/code> \u3068\u3059\u308b\u3053\u3068\u3067\u56de\u907f\u53ef\u80fd\u3067\u3059\u3002<\/p>\r\n\r\n\r\n\r\n
\r\n2021 12 08 15.15.56<\/figcaption>\r\n<\/figure>\r\n\r\n\r\n\r\n\u305d\u3082\u305d\u3082\u30cf\u30a4\u30d5\u30f3\u3067\u7e4b\u304c\u306a\u3051\u308c\u3070\u826f\u3044\u3058\u3083\u3093\u2026\u2026\u3068\u3044\u3046\u306e\u306f\u3054\u3082\u3063\u3068\u3082\u3067\u3059\u304c\u3001\u3068\u304d\u306b\u306f\u4f7f\u3048\u308b\u30c6\u30af\u30cb\u30c3\u30af\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\r\n\r\n\r\n\r\n
\u3042\u3068\u306f\u3072\u305f\u3059\u3089\u540c\u3058\u4f5c\u696d\u3092\u7e70\u308a\u8fd4\u3057\u3066\u3044\u3051\u3070\u5b8c\u6210\u3067\u3059\uff01<\/p>\r\n\r\n\r\n\r\n
\u3061\u306a\u307f\u306b<\/h2>\r\n\r\n\r\n\r\n
\u524d\u56de\u306e\u8a18\u4e8b\u3067\u306f uxpin.config.js<\/code> \u304b\u3089\u3057\u308c\u3063\u3068 Page.tsx<\/code> \u3092\u629c\u3044\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\r\n\r\n\r\n\r\nmodule.exports = {\r\n components: {\r\n categories: [\r\n {\r\n name: 'General',\r\n include: [\r\n 'stories\/Button.tsx',\r\n 'stories\/Header.tsx'\r\n \/\/ Storybook\u306e\u30a4\u30cb\u30b7\u30e3\u30e9\u30a4\u30ba\u3067Page.tsx\u3082\u751f\u6210\u3055\u308c\u3066\u3044\u308b\u3051\u3069\u767b\u9332\u3057\u3066\u3044\u306a\u3044\r\n ]\r\n }\r\n ],\r\n webpackConfig: 'uxpin.webpack.config.js'\r\n },\r\n name: 'UXPin Merge + TypeScript + Storybook'\r\n}\r\n<\/pre><\/div>\r\n\r\n\r\n\u3058\u3064\u306f page.css<\/code> \u306e\u4e2d\u3067\u306f h2<\/code> \u3084 p<\/code> \u306a\u3069\u30bf\u30b0\u306b\u5bfe\u3057\u3066\u76f4\u63a5\u30b9\u30bf\u30a4\u30eb\u3092\u6307\u5b9a\u3057\u3066\u3044\u305f\u3093\u3067\u3059\u306d\u3002
\u305d\u306e\u305f\u3081UXPin\u306eGUI\u306e\u753b\u9762\u306e\u30b9\u30bf\u30a4\u30eb\u304c\u5d29\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\r\n\r\n\r\n\r\n\u305d\u306e\u8aac\u660e\u3092\u524d\u56de\u306e\u8a18\u4e8b\u306e\u6642\u70b9\u3067\u3059\u308b\u306e\u306f\u8a71\u304c\u3084\u3084\u3053\u3057\u304f\u306a\u308b\u306a\u2026\u2026\u3068\u601d\u3063\u3066\u7701\u7565\u3057\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\r\n\r\n\r\n\r\n
\u4eca\u56deCSS Modules\u3092\u9069\u7528\u3057\u305f\u306e\u3067 Page.tsx<\/code> \u3092\u767b\u9332\u3057\u3066\u3082\u554f\u984c\u306a\u304f\u52d5\u304d\u307e\u3059\u3002<\/p>\r\n\r\n\r\n\r\n\u6b21\u56de<\/strong><\/h2>\r\n\r\n\r\n\r\n\u4eca\u56de\u306fCSS Modules\u3092\u5c0e\u5165\u3057\u307e\u3057\u305f\u304c\u3001\u7b46\u8005\u306fCSS in JS\u306e\u65b9\u304c\u597d\u307f\u306a\u305f\u3081\u66f4\u306b\u5165\u308c\u66ff\u3048\u307e\u3059\u3002\u4f7f\u3044\u6163\u308c\u3066\u3044\u308b\u306e\u3082\u3042\u308a\u3001Emotion<\/a>\u306e\u5c0e\u5165\u3092\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\r\n\r\n\r\n\r\n\u3053\u306e\u8a18\u4e8b\u304b\u3089UXPin<\/a>\u306b\u8208\u5473\u3092\u304a\u6301\u3061\u3044\u305f\u3060\u3051\u305f\u65b9\u306f\u3001\u00a014\u65e5\u9593\u306e\u7121\u6599\u30c8\u30e9\u30a4\u30a2\u30eb<\/a>\u306b\u30b5\u30a4\u30f3\u30a2\u30c3\u30d7\u3057\u3066\u3001\u4eca\u65e5\u304b\u3089\u3088\u308a\u662f\u975e\u3054\u5229\u7528\u3092\u958b\u59cb\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002\u307e\u305f\u3001UXPin Merge\u3092\u3054\u5e0c\u671b\u306e\u65b9\u306f\u3001\u3053\u3061\u3089\u3088\u308a\u30c7\u30e2\u3092\u3054\u4e88\u7d04<\/a>\u304f\u3060\u3055\u3044\u3002<\/p>\r\n\r\n\r\n\r\n