我试图设置从创建反应应用程序3 (CRA),类型记录,材料UI和样式-组件的反应-样式Material (RSG)。我被这个错误困住了:
./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
const muiTheme = getMuiTheme({});
const MuiThemeWrapper = ({ children, ...rest }) => (
<MuiThemeProvider muiTheme={muiTheme}>
<ThemeProvider theme={theme}>
{children}
</ThemeProvider>
</MuiThemeProvider>
);
export default MuiThemeWrapper;我的样式设计师配置:
/styleguidist.config.js
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
{
"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的错误是一个错误?
发布于 2020-10-15 11:24:24
我让它发挥作用,希望其他人也能发现这一点,因为到目前为止,很少也只有一些非类型文本的例子可以找到。
我的设置要注意的事情:
src/中,组件在src/components/中,使用modules: webpack配置查看src/的绝对进口。tsconfig.json自动创建(稍后专门为“ts-加载器”重写noEmit,请参见下文);下面显示了一个重要的异常/添加。styleguide.config.js中。tsconfig.json是样板,除了这里的gem paths:'rsg-components/*',它需要手动添加--它隐藏在Styleguidist的食谱中。没有它,我们需要在webpack配置中使用别名定义,包括替换包装器!有了正确的paths定义在tsconfig.json中,事情终于开始正确地到位。
{
"compilerOptions": {
"paths": {
"rsg-components/*": [
"node_modules/react-styleguidist/lib/client/rsg-components/*"
]
}
}
}styleguide.config.js位于项目的顶层目录中:
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:绝对导入,但在本例中,我选择了一个别名。你可能会有不同的判断:)styleguidist/MuiThemeWrapper.tsx中。那是我的包装器styleguidist/MuiThemeWrapper.tsx
(关于MUI v5,请参见梅v5 +反应定型师+ ScopedCSSBaseline + createTheme styleOverrides:身体fontSize改变无效的答案)
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 MuiThemeWrapperfont-family。https://stackoverflow.com/questions/57976044
复制相似问题