English 中文(简体)
如何解决故事书中的内容?
原标题:How to resolve aliases in Storybook?

我有一个带有故事书的复读/描述项目。 故事书发挥了巨大作用,但一旦我开始用别物进口档案,就会坠毁。

例:

import Foo from "@components/foo" => crash
import Foo from "../../components/foo" => ok

服饰品的罚款。 这个问题只与故事书有关。

我的故事:

module.exports = {
  stories: ["../**/stories.tsx"],
  webpackFinal: (config) => {
    return {
      ...config,
      module: {
        ...config.module,
        rules: [
          {
            test: /.(ts|js)x?$/,
            exclude: /node_modules/,
            use: { loader: "babel-loader" },
          },
          { test: /.css$/, use: ["style-loader", "css-loader"] },
          { test: /.(png|jpg|gif)$/, use: ["file-loader"] },
          {
            test: /.svg$/,
            use: [
              {
                loader: "babel-loader",
              },
              {
                loader: "react-svg-loader",
                options: {
                  jsx: true,
                },
              },
            ],
          },
        ],
      },
    };
  },
  typescript: {
    check: false,
    checkOptions: {},
    reactDocgen: "react-docgen-typescript",
    reactDocgenTypescriptOptions: {
      shouldExtractLiteralValuesFromEnum: true,
      propFilter: (prop) =>
        prop.parent ? !/node_modules/.test(prop.parent.fileName) : true,
    },
  },
};

我的系列会议:

/* eslint-env node */
const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");
const Dotenv = require("dotenv-webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const isProductionMode = (mode) => mode === "production";

module.exports = () => {
  const env = require("dotenv").config({ path: __dirname + "/.env" });
  const nodeEnv = env.parsed.NODE_ENV;
  return {
    mode: "development",
    entry: "./src/index.tsx",
    output: {
      path: path.join(__dirname, "./dist"),
      filename: "[name].[contenthash].bundle.js",
      publicPath: "/",
    },
    resolve: {
      extensions: [".ts", ".tsx", ".js", "jsx", ".json"],
      alias: {
    "@api": path.resolve(__dirname, "src/api/"),
    "@assets": path.resolve(__dirname, "src/assets/"),
    "@components": path.resolve(__dirname, "src/components/"),
    "@containers": path.resolve(__dirname, "src/containers/"),
    "@data": path.resolve(__dirname, "src/data/"),
    "@i18n": path.resolve(__dirname, "src/i18n/"),
    "@models": path.resolve(__dirname, "src/models/"),
    "@pages": path.resolve(__dirname, "src/pages/"),
    "@src": path.resolve(__dirname, "src/"),
    "@stores": path.resolve(__dirname, "src/stores/"),
    "@utils": path.resolve(__dirname, "src/utils/"),
  },
    },
    module: {
      rules: [
        {
          test: /.(ts|js)x?$/,
          exclude: /node_modules/,
          use: { loader: "babel-loader" },
        },
        { test: /.css$/, use: ["style-loader", "css-loader"] },
        { test: /.(png|jpg|jpeg|gif)$/, use: ["file-loader"] },
        {
          test: /.svg$/,
          use: [
            {
              loader: "babel-loader",
            },
            {
              loader: "react-svg-loader",
              options: {
                jsx: true,
              },
            },
          ],
        },
      ],
    },
    devServer: {
      historyApiFallback: true,
      port: 3000,
      inline: true,
      hot: true,
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: "./src/index.html",
      }),
      new Dotenv(),
    ],
    optimization: {
      minimize: isProductionMode(nodeEnv),
      minimizer: isProductionMode(nodeEnv) ? [new TerserPlugin()] : [],
      splitChunks: { chunks: "all" },
    },
  };
};

如何解决这一问题? 我在网上读到第5.24.2和6.1.20号故事书,因此它们是最新版本。

最佳回答

仅添加以下内容:

const path = require( path );

module.exports = {
  "stories": [
    "../components/**/*.stories.mdx",
    "../components/**/*.stories.@(js|jsx|ts|tsx)"
  ],
  "addons": [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
     @storybook/preset-scss ,
  ],
  webpackFinal: async (config) => {
    config.resolve.alias = {
      ...config.resolve.alias,
       @/interfaces : path.resolve(__dirname, "../interfaces"),
    };

    return config;
  }
}

www.un.org/Depts/DGACM/index_french.htm

著作

问题回答

当我有同样的问题时,我就这样做了:

  • Install a package in dev deps yarn add -D tsconfig-paths-webpack-plugin.
  • Then adjust your ./storybook/main.js config:
... // other imports
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");

...
webpackFinal: (config) => {
  config.resolve.plugins = config.resolve.plugins || [];
  config.resolve.plugins.push(
    new TsconfigPathsPlugin({
      configFile: path.resolve(__dirname, "../tsconfig.json"),
    })
  );

  return { ... }
}
...

参考:

// .storybook/main.js

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

module.exports = {
  webpackFinal: async (config) => {
    config.resolve.plugins = [
      ...(config.resolve.plugins || []),
      new TsconfigPathsPlugin({
        extensions: config.resolve.extensions,
      }),
    ];
    return config;
  },
};

如您使用<代码>@storybook/builder-vite。 这间 ne子为我工作。

const tsconfigPaths = require("vite-tsconfig-paths");
...
module.exports = {
  ...
  async viteFinal(config) {
    return {
      ...config,
      plugins: [...(config.plugins || []), tsconfigPaths.default()],
    };
  },
};

如果你重新使用第5号网页,请具体说明除了先前的答复外,还应使用第5号网页:

  core: {
    builder: "webpack5",
  },

最后故事书/主编,随后将类似:

    // .storybook/main.js
const path = require( path );
const appWebpack = require(path.join(process.cwd(),  webpack.config.js ));
module.exports = {
  stories: [ ../src/**/*.stories.@(tsx|mdx) ],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
     @storybook/preset-scss 
  ],
  core: {
    builder: "webpack5",
  },
  webpackFinal: async (config) => {
    config.resolve.modules = [
      ...(config.resolve.modules || []),
      ...[path.resolve(process.cwd(), "src")],
    ];
    config.resolve.alias = {
      ...(config.resolve.alias || {}),
      ...appWebpack().resolve.alias,
    };
    return config;
  },
};

这将使两条绝对道路和别条都成为可能(只要这些别物在你的主要网站组合中适当设立。

Edited

在具体涉及别名的事件发生后,我又一次走下了网状石路。

我先更新了以上理论手册/主编的原始定稿,其中明确将各别和单元分号合并。

Edit 2

众所周知,es子将qu子在你创建的全球矫正器(并添加文字材料/审查.js)。 你们可以安全地忽视这一点——他们仍在工作。 如果我知道如何纠正这种情况,我就会回头,增加第3条。

We re using Vite and typescript project references, for us adding the following to the storybook main.cjs worked;

viteFinal: async (config) => {
    config.resolve.alias = {
        ...config.resolve.alias,
         @some-alias : path.resolve(__dirname,  ../../some/ts/project/reference ),
    };
    return config;
}

除了Jamie Birch的出色答复外,如果您重新使用和不希望安装vite-tsconfig-paths,你可以只读到

const path = require( path );

module.exports = {
 // ... whatever you already have here
  viteFinal: async (config) => {
    if (config.resolve.alias) {
      config.resolve.alias.push({ find:  @ , replacement: path.resolve(__dirname,  ../src ) +  /  });
    } else {
      config.resolve.alias = [{ find:  @ , replacement: path.resolve(__dirname,  ../src ) +  /  }];
    }
    return config;
  }
}

这里可以有2例使用进口品的情况。

import something from "@components/somewhere"
import something from "@/components/somewhere"

页: 1 档案

import path from "path";

const config: StorybookConfig = {
...other codes are here

  webpackFinal: async (config) => {
    ...other codes are here
    
    (config.resolve as any).alias = {
      ["@components"]: [path.resolve(__dirname, "./components")],
      ["@/components"]: [path.resolve(__dirname, "../components")],
    };
  }
}

希望会帮助!





相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...