我无法使用其类名在故事书中设置我的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
似乎可以按namme类设置样式
.Eatery {
border: 5px dashed $color-primary;
margin: $base-spacing;
}
.name {
background: red;
}
不确定我为什么不能为这些类名设置样式?
答案 0 :(得分:0)
因此,在检查故事书Webpack中注入的.js文件中的类名称后,我最终意识到它正在对类名称进行哈希处理。
我进入rules.js
并通过将其设置为false
来关闭css模块。
loader: 'css-loader',
options: {
importLoaders: 1,
localIdentName: '[name]-[local]-[hash:base64:3]',
modules: false,
},
然后它停止对className
进行散列,并且开始工作!