0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Next.js と Typescript で React-StyleGuidist を使うための設定

Posted at

概要

Next.jsとTypeScriptでReact-StyleGuidistを利用したかったので調査したときのメモとして残している。

Next.js プロジェクトの作成

create-next-app [prject-name]
cd [project-name]

mkdir src
mv pages src/
mkdir src/components
mkdir src/styleguide

必要なパッケージのインストール

# NextjsでTypeScriptを使うためのパッケージ
yarn add -D @types/node typescript @types/react

# React-StyleguidistをNextjs,TypeScriptと一緒に使うためのパッケージ
yarn add -D react-styleguidist babel-loader react-docgen-typescript

# Bootstrapを使いたかったので追加しているが、必須ではない。
yarn add bootstrap jquery popper.js

styleguide.config.jsの設定

const path = require("path");

module.exports = {
  components: "./src/components/**/*.tsx",
  propsParser: require("react-docgen-typescript").withCustomConfig(
    "./tsconfig.json"
  ).parse,
  styleguideComponents: {
    Wrapper: path.resolve(__dirname, "src/styleguide/Wrapper.tsx"),
  },
  webpackConfig: {
    module: {
      rules: [
        {
          test: /\.tsx?$/,
          exclude: /node_modules/,
          loader: "babel-loader",
        },
        {
          test: /\.css?$/,
          use: ["style-loader", "css-loader"],
        },
      ],
    },
  },
};

src/styleguide/Wrapper.tsxの設定

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap";

const Wrapper: React.FC = ({ children }) => {
  return <div>{children}</div>;
};

export default Wrapper;

babel.config.jsの設定

module.exports = {
  presets: ["next/babel"],
  plugins: [],
};

package.jsonの設定

"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "styleguide": "styleguidist server",
    "styleguide:build": "styleguidist build"
  },

StyleGuistサーバの起動

あとはReactで利用する場合と同じように、
以下のコマンドで起動し、src/componentsにコンポーネントと対応するMDファイルを作成していく。

yarn styleguide
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?