如何使用 Tailwindcss、ReactJS 和 Typescript 设置 Storybook

发布于 2025-01-11 05:46:16 字数 167 浏览 0 评论 0原文

如何设置 Storybook 以便它解析 Tailwindcss 样式并解析绝对路径?

注意:这是一个根据允许的自记录问题/答案。这需要一段时间才能弄清楚,我相信很多其他人也会遇到这个问题。

How do you setup Storybook so that it parses Tailwindcss styles and also parses absolute paths?

Note: This is a self-documenting question/answer allowed as per this. This took a while to figure out and I'm sure many others will encounter this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生未歇 2025-01-18 05:46:16

为了解析 Storybook 中的路径,我们将使用 tsconfig 作为源。
我们假设您已经安装了带有 typescript 模板的 ReactJS 项目。

设置绝对路径

1. 在 typescript 中定义绝对路径

在 tsconfig.js 中的“paths”下添加绝对路径。

// tsconfig.json

{
  "compilerOptions": {
    // ...
    "baseUrl": "src",
    "paths": {
      "#assets/*": ["./assets/*"],
      "#components/*": ["./components/*"],
      // etc.
    }
  }
  "include": ["src"]
}

2. 扩展 tsconfig 绝对路径以在 Storybook 中工作

安装 tsconfig-paths-webpack -来自 npm 的插件。截至撰写本文时,每周下载量已达数百万次。

$ npm install -D tsconfig-paths-webpack-plugin
// or
$ yarn add -D tsconfig-paths-webpack-plugin

.storybook/main.js 下,通过将以下内容添加到 module.exports 来扩展 tsconfig 路径解析。

// .storybook/main.js

const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

module.exports = {
  // Add the following block of code in addition to what's existing
  "webpackFinal": async (config) => {
    config.resolve.plugins = [
      ...(config.resolve.plugins || []),
      new TsconfigPathsPlugin({
        extensions: config.resolve.extensions,
      }),
    ];
    return config;
  },
};

来源

3. 解析 Storybook

Add 中的 Tailwind 样式.storybook/preview.js 文件顶部的以下两行代码。

// .storybook/preview.js

import '!style-loader!css-loader!postcss-loader!tailwindcss/tailwind.css';
import 'tailwindcss/tailwind.css';

来源

您的 tailwindcss 现在应该解析。

其他文件

对于 Tailwind v3+,请确保您的 tailwind.config.js 没有清除选项并且没有明确声明 JIT。我的看起来像这样:

// tailwind.config.js

module.exports = {
    content: [
        "./src/**/*.{js,jsx,ts,tsx}"
    ],
    theme: {
        extend: {},
    },
    plugins: [],
};

To resolve paths in Storybook, we'll be using tsconfig as the source.
We assume you have installed a ReactJS project with the typescript template already.

Setting Absolute Paths

1. Define absolute paths in typescript

Add your absolute paths under "paths" in tsconfig.js.

// tsconfig.json

{
  "compilerOptions": {
    // ...
    "baseUrl": "src",
    "paths": {
      "#assets/*": ["./assets/*"],
      "#components/*": ["./components/*"],
      // etc.
    }
  }
  "include": ["src"]
}

2. Extend the tsconfig absolute paths to work in Storybook

Install the tsconfig-paths-webpack-plugin from npm. Has millions of weekly downloads at time of writing.

$ npm install -D tsconfig-paths-webpack-plugin
// or
$ yarn add -D tsconfig-paths-webpack-plugin

Under .storybook/main.js extend the tsconfig path resolution by adding the following to your module.exports.

// .storybook/main.js

const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

module.exports = {
  // Add the following block of code in addition to what's existing
  "webpackFinal": async (config) => {
    config.resolve.plugins = [
      ...(config.resolve.plugins || []),
      new TsconfigPathsPlugin({
        extensions: config.resolve.extensions,
      }),
    ];
    return config;
  },
};

Source

3. Parsing Tailwind Styles in Storybook

Add the following two lines of code at the top of your .storybook/preview.js file.

// .storybook/preview.js

import '!style-loader!css-loader!postcss-loader!tailwindcss/tailwind.css';
import 'tailwindcss/tailwind.css';

Source

Your tailwindcss should parse now.

Additional files

For Tailwind v3+, make sure your tailwind.config.js doesn't have the purge option and doesn't explicitly state JIT. Mine looks like this:

// tailwind.config.js

module.exports = {
    content: [
        "./src/**/*.{js,jsx,ts,tsx}"
    ],
    theme: {
        extend: {},
    },
    plugins: [],
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文