首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安装反应-样式设计师,创建反应应用程序,类型记录,材料用户界面和样式-组件

安装反应-样式设计师,创建反应应用程序,类型记录,材料用户界面和样式-组件
EN

Stack Overflow用户
提问于 2019-09-17 14:05:26
回答 1查看 1.4K关注 0票数 7

我试图设置从创建反应应用程序3 (CRA),类型记录,材料UI和样式-组件的反应-样式Material (RSG)。我被这个错误困住了:

代码语言:javascript
复制
./node_modules/react-styleguidist/lib/client/rsg-components/ReactExample/ReactExample.js
Module not found: Can't resolve 'rsg-components/Wrapper' in '/Users/ofj/code/new-core-web/node_modules/react-styleguidist/lib/client/rsg-components/ReactExample'

我根据文档为MUI主题和样式组件设置了一个包装器:

https://react-styleguidist.js.org/docs/thirdparties.html#styled-components

/styleguidist/MuiThemeWrapper.tsx

代码语言:javascript
复制
const muiTheme = getMuiTheme({});
const MuiThemeWrapper = ({ children, ...rest }) => (
    <MuiThemeProvider muiTheme={muiTheme}>
        <ThemeProvider theme={theme}>
            {children}
        </ThemeProvider>
    </MuiThemeProvider>
);

export default MuiThemeWrapper;

我的样式设计师配置:

/styleguidist.config.js

代码语言:javascript
复制
const path = require('path');
module.exports = {
    components: "src/components/**/layout.{js,jsx,ts,tsx}",
    propsParser: require('react-docgen-typescript').withCustomConfig(
        './tsconfig.json'
      ).parse,
    serverPort: 6161,
    styleguideComponents: {
      Wrapper: path.join(__dirname, 'styleguidist/MuiThemeWrapper.jsx')
    }
};

我的tsconfig遵循标准的CRA / MUI建议

https://material-ui.com/guides/typescript/

tsconfig.json

代码语言:javascript
复制
{
  "compilerOptions": {
    "noImplicitAny": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "baseUrl": "src",
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "checkJs": false,
    "pretty": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "exclude": [
    "node_modules"
  ],
  "types": [
    "./node_modules/@types/"
  ]
}

我没有自定义webpack/babel配置设置,因为我不知道如何和不搞砸TS的转移。也许这是为了让RSG工作所缺少的.?或者rsg-components/ReactExample/ReactExprese.js的错误是一个错误?

EN

回答 1

Stack Overflow用户

发布于 2020-10-15 11:24:24

我让它发挥作用,希望其他人也能发现这一点,因为到目前为止,很少也只有一些非类型文本的例子可以找到。

我的设置要注意的事情:

  • react应用程序在src/中,组件在src/components/中,使用modules: webpack配置查看src/的绝对进口。
  • 应用程序是使用react创建者创建的。
  • tsconfig.json自动创建(稍后专门为“ts-加载器”重写noEmit,请参见下文);下面显示了一个重要的异常/添加。
  • 没有分开的webpack和babel配置,包括在styleguide.config.js中。

tsconfig.json是样板,除了这里的gem paths:'rsg-components/*',它需要手动添加--它隐藏在Styleguidist的食谱中。没有它,我们需要在webpack配置中使用别名定义,包括替换包装器!有了正确的paths定义在tsconfig.json中,事情终于开始正确地到位。

代码语言:javascript
复制
{
  "compilerOptions": {
    "paths": {
      "rsg-components/*": [
        "node_modules/react-styleguidist/lib/client/rsg-components/*"
      ]
    }
  }
}

styleguide.config.js位于项目的顶层目录中:

代码语言:javascript
复制
const path = require('path')

module.exports = {
    components: [
        'src/components/**/*.{ts,tsx}',
        'src/models/**/*.ts',
    ],
    ignore: [
        'src/**/index.{ts,tsx}',
    ],
    // We need to override how to decide on what an example file is, in order
    // to remove default which tries to document undocumented components. Ugh.
    // So, only document components for which we also have an explicit
    // documentation file named the same as the component file, but ending in
    // ".md" instead.
    getExampleFilename: (cpath) => {
        return cpath.replace(/\.(tsx?)$/, '.md')
    },
    // Show import commands without the component filename extension and only
    // for the module; also remove the first "src/" path component.
    getComponentPathLine: (cpath) => {
        const cname = ['.tsx', '.ts'].reduce((name, ext) => path.basename(name, ext), cpath)
        const cdir = path.dirname(cpath).replace(/^src\//, '')
        return `import { ${cname} } from ${cdir}`
    },
    // How uncivilized: do not list components lacking an example.
    skipComponentsWithoutExample: true,
    // Always expand the props and methods of components.
    usageMode: 'expand',
    // Support rendering prop types of typescript components.
    propsParser: require('react-docgen-typescript').withCustomConfig(
        './tsconfig.json',
        {
            "compilerOptions": { "noEmit": false },
        }
    ).parse,
    // Replace the standard wrapper for example component usage code with our
    // own wrapper which brings in the Material UI theme.
    styleguideComponents: {
        Wrapper: path.join(__dirname, 'styleguidist/MuiThemeWrapper.tsx')
    },
    // Tell webpack what to look for and where and how to load it.
    webpackConfig: {
        resolve: {
            extensions: ['.tsx', '.ts', '.js'],
            // https://webpack.js.org/configuration/resolve/#resolvemodules;
            // we're allowing absolute imports to be satisfied from the src/
            // directory.
            modules: [
                path.resolve(__dirname, 'src/'), 
                'node_modules'
            ],
            alias: {
                // Could also be covered by a modules clause, but we are
                // sticking with an alias instead to cover only exactly
                // absolute "styleguidist/..." imports.
                'styleguidist': path.join(__dirname, 'styleguidist'),
            }
        },
        module: {
            rules: [
                {
                    test: /\.tsx?$/,
                    exclude: /node_modules/,
                    use: [
                        {
                            loader: 'babel-loader',
                            options: {
                                presets: [
                                    "@babel/preset-env",
                                    "@babel/react",
                                ]
                            },
                        },
                        {
                            loader: 'ts-loader',
                            options: {
                                // Important! Avoids "Error: TypeScript emitted no output for..." errors
                                compilerOptions: {
                                    noEmit: false,
                                },
                            },
                        },
                    ],
                },
                {
                    test: /\.css$/,
                    loader: 'style-loader!css-loader?modules',
                },
                {
                    test: /\.svg$/,
                    loader: 'url-loader',
                },
                {
                    test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
                    use: [
                        {
                            loader: 'url-loader',
                            options: {
                                name: '[name].[ext]',
                                outputPath: 'fonts/',
                            }
                        },
                    ]
                }
            ]
        },
    }
}
  • 可以用styleguidist/子句来覆盖modules:绝对导入,但在本例中,我选择了一个别名。你可能会有不同的判断:)
  • 我正在设置一个限制性很强的机制,只记录带有示例的组件;如果您不想这样做,就取消它。
  • 困难是使用正确的加载器配置webpack;我启用了打字本、TSX和JSX,以及提取字体资源。
  • 重要:包装器在styleguidist/MuiThemeWrapper.tsx中。

那是我的包装器styleguidist/MuiThemeWrapper.tsx

(关于MUI v5,请参见梅v5 +反应定型师+ ScopedCSSBaseline + createTheme styleOverrides:身体fontSize改变无效的答案)

代码语言:javascript
复制
import React from 'react'

import "fontsource-roboto/400.css"
import "fontsource-roboto/500.css"
import "fontsource-roboto/700.css"
import "fontsource-roboto-mono/400.css"

import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'
import CssBaseline from '@material-ui/core/CssBaseline'

const muiTheme = createMuiTheme({})

const MuiThemeWrapper = ({children}) => (
    <ThemeProvider theme={muiTheme}>
        <CssBaseline />
        {children}
    </ThemeProvider>
)

export default MuiThemeWrapper
  • 显式地拉入我在react+Material UI项目中需要的Roboto字体。
  • 应用CSS基线定义,否则将不能正确定义font-family
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57976044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档