无法通过 Storybook 中的 Class 名称设置我的 React 组件的样式
Can't style my React Components by Class Name in Storybook
我无法通过 class 名称在故事书中设置我的 React 组件的样式!
如果我使用 div
、<h1>
等通用元素,那么我可以设置一些样式。它必须与我的 webpack 配置有关,但我只是不知道是什么。
我的rules.js
传给了webpack.config.js
rules.js
const { configureBabel } = require('../_babel.config');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
/**
* @param themePaths {ThemePaths}
* @param selectMode {SelectMode}
* @param selectTarget {SelectTarget}
* @returns {*[]}
*/
function rules(themePaths, selectMode, selectTarget) {
const babelOptions = configureBabel(selectMode, selectTarget);
const babelEnvDeps = require('webpack-babel-env-deps');
return [
{
test: /\.graphql$/,
exclude: /node_modules/,
use: [
{
loader: 'graphql-tag/loader',
},
],
},
{
include: [
path.resolve(__dirname, '..', 'server'),
path.resolve(__dirname, '..', 'runtime'),
themePaths.src,
themePaths.generated,
path.resolve(__dirname, '..', '..', 'swagger'),
path.resolve(__dirname, '..', 'node_modules', 'debug'),
babelEnvDeps.include(),
],
// exclude: [/node_modules/],
test: /\.(ts|tsx|mjs|js|svg|graphql)$/,
use: [
{
loader: 'babel-loader',
options: babelOptions,
},
],
},
{
test: [/\.scss$/],
use: [
selectMode({ development: 'style-loader' }),
selectMode({ production: MiniCssExtractPlugin.loader }),
{
loader: 'css-loader',
options: {
importLoaders: 1,
localIdentName: '[name]-[local]-[hash:base64:3]',
modules: true,
},
},
{
loader: 'sass-loader',
options: {
data: `
@import "_vars";
@import "_mixins";
@import "_normalize";
@import "_reset";
`,
includePaths: ['src/scss'],
},
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: [require('autoprefixer')()],
},
},
'resolve-url-loader',
].filter(Boolean),
},
{
test: /\.(jpe?g|png|svg|gif)$/,
use: [
{
loader: 'file-loader',
options: {},
},
],
},
{
test: /\.(woff2?)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
},
},
],
},
];
}
module.exports.rules = rules;
我的webpack.config.js
const { resolve } = require('path');
module.exports = (baseConfig, env, defaultConfig) => {
const rulesFn = require('../.webpack/rules').rules;
const rules = rulesFn(
{
outputLegacy: '',
generated: '',
output: '',
src: '',
},
input => input['development'],
input => input['modern'],
);
delete rules[1].include;
defaultConfig.module.rules = rules;
defaultConfig.resolve.extensions = ['.ts', '.tsx', '.js'];
defaultConfig.resolve.alias = {
...(defaultConfig.resolve.alias || {}),
swagger: resolve(__dirname, '..', '..', 'swagger'),
};
return defaultConfig;
};
我的config.ts
import { configure } from "@storybook/react";
const req = require.context('../src/', true, /.stories.tsx$/);
function loadStories() {
req.keys().forEach((filename) => req(filename))
}
configure(loadStories, module);
我的 React 组件 EateryItem.tsx
import React from "react";
import "./EateryItem.scss";
export const EateryItem: React.SFC<any> = props => {
return (
<div className="Eatery" key={props.id}>
<h3 className="name">{props.name}</h3>
<p className="address">{props.address}</p>
<p className="type">{props.foodType}</p>
</div>
);
};
我的 EateryItem.scss
我似乎可以通过 class namme
来设计这些样式
.Eatery {
border: 5px dashed $color-primary;
margin: $base-spacing;
}
.name {
background: red;
}
不确定为什么我不能为这些 class 名称设置样式?
所以在检查我的故事书 webpack 中注入的 .js 文件中的 class 名称后,我最终意识到它正在对 class 名称进行哈希处理。
我进入 rules.js
并关闭 css 模块,方法是将其设置为 false
。
loader: 'css-loader',
options: {
importLoaders: 1,
localIdentName: '[name]-[local]-[hash:base64:3]',
modules: false,
},
然后它停止对 className
进行哈希处理并开始工作!
我无法通过 class 名称在故事书中设置我的 React 组件的样式!
如果我使用 div
、<h1>
等通用元素,那么我可以设置一些样式。它必须与我的 webpack 配置有关,但我只是不知道是什么。
我的rules.js
传给了webpack.config.js
rules.js
const { configureBabel } = require('../_babel.config');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
/**
* @param themePaths {ThemePaths}
* @param selectMode {SelectMode}
* @param selectTarget {SelectTarget}
* @returns {*[]}
*/
function rules(themePaths, selectMode, selectTarget) {
const babelOptions = configureBabel(selectMode, selectTarget);
const babelEnvDeps = require('webpack-babel-env-deps');
return [
{
test: /\.graphql$/,
exclude: /node_modules/,
use: [
{
loader: 'graphql-tag/loader',
},
],
},
{
include: [
path.resolve(__dirname, '..', 'server'),
path.resolve(__dirname, '..', 'runtime'),
themePaths.src,
themePaths.generated,
path.resolve(__dirname, '..', '..', 'swagger'),
path.resolve(__dirname, '..', 'node_modules', 'debug'),
babelEnvDeps.include(),
],
// exclude: [/node_modules/],
test: /\.(ts|tsx|mjs|js|svg|graphql)$/,
use: [
{
loader: 'babel-loader',
options: babelOptions,
},
],
},
{
test: [/\.scss$/],
use: [
selectMode({ development: 'style-loader' }),
selectMode({ production: MiniCssExtractPlugin.loader }),
{
loader: 'css-loader',
options: {
importLoaders: 1,
localIdentName: '[name]-[local]-[hash:base64:3]',
modules: true,
},
},
{
loader: 'sass-loader',
options: {
data: `
@import "_vars";
@import "_mixins";
@import "_normalize";
@import "_reset";
`,
includePaths: ['src/scss'],
},
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: [require('autoprefixer')()],
},
},
'resolve-url-loader',
].filter(Boolean),
},
{
test: /\.(jpe?g|png|svg|gif)$/,
use: [
{
loader: 'file-loader',
options: {},
},
],
},
{
test: /\.(woff2?)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
},
},
],
},
];
}
module.exports.rules = rules;
我的webpack.config.js
const { resolve } = require('path');
module.exports = (baseConfig, env, defaultConfig) => {
const rulesFn = require('../.webpack/rules').rules;
const rules = rulesFn(
{
outputLegacy: '',
generated: '',
output: '',
src: '',
},
input => input['development'],
input => input['modern'],
);
delete rules[1].include;
defaultConfig.module.rules = rules;
defaultConfig.resolve.extensions = ['.ts', '.tsx', '.js'];
defaultConfig.resolve.alias = {
...(defaultConfig.resolve.alias || {}),
swagger: resolve(__dirname, '..', '..', 'swagger'),
};
return defaultConfig;
};
我的config.ts
import { configure } from "@storybook/react";
const req = require.context('../src/', true, /.stories.tsx$/);
function loadStories() {
req.keys().forEach((filename) => req(filename))
}
configure(loadStories, module);
我的 React 组件 EateryItem.tsx
import React from "react";
import "./EateryItem.scss";
export const EateryItem: React.SFC<any> = props => {
return (
<div className="Eatery" key={props.id}>
<h3 className="name">{props.name}</h3>
<p className="address">{props.address}</p>
<p className="type">{props.foodType}</p>
</div>
);
};
我的 EateryItem.scss
我似乎可以通过 class namme
.Eatery {
border: 5px dashed $color-primary;
margin: $base-spacing;
}
.name {
background: red;
}
不确定为什么我不能为这些 class 名称设置样式?
所以在检查我的故事书 webpack 中注入的 .js 文件中的 class 名称后,我最终意识到它正在对 class 名称进行哈希处理。
我进入 rules.js
并关闭 css 模块,方法是将其设置为 false
。
loader: 'css-loader',
options: {
importLoaders: 1,
localIdentName: '[name]-[local]-[hash:base64:3]',
modules: false,
},
然后它停止对 className
进行哈希处理并开始工作!