我试图将React CSS模块添加到我的项目中,但我遇到了eslint的问题。我的登录视图如下所示:
import React, { PropTypes as T } from 'react';
import {ButtonToolbar, Button} from 'react-bootstrap';
import Messages from '../Messages/Messages';
import AuthService from '../../utils/AuthService';
import CSSModules from 'react-css-modules';
import styles from './styles.module.css';
class Login extends React.Component {
render() {
let rootStyle = {
textAlign: 'center'
};
let toolbarStyle = {
display: 'inline-block'
};
const { auth } = this.props;
return (
<div styleName="root">
<h2>Login</h2>
<Messages auth={this.props.auth}></Messages>
<ButtonToolbar styleName="toolbar">
<Button bsStyle="primary" onClick={auth.login.bind(this)}>Login</Button>
</ButtonToolbar>
</div>
);
}
}
Login.PropTypes = {
location: T.object,
auth: T.instanceOf(AuthService)
};
export default CSSModules(Login, styles);
&#13;
这是我原来的.eslintrc文件,在添加导入/忽略设置之前:
{
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings"
],
"plugins": [
"react"
],
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"experimentalObjectRestSpread": true
}
},
"env": {
"es6": true,
"browser": true,
"node": true,
"jquery": true,
"mocha": true
},
"rules": {
"quotes": 0,
"no-console": 1,
"no-debugger": 1,
"no-var": 1,
"semi": [1, "always"],
"no-trailing-spaces": 0,
"eol-last": 0,
"no-unused-vars": 0,
"no-underscore-dangle": 0,
"no-alert": 0,
"no-lone-blocks": 0,
"jsx-quotes": 1,
"react/display-name": [ 1, {"ignoreTranspilerName": false }],
"react/forbid-prop-types": [1, {"forbid": ["any"]}],
"react/jsx-boolean-value": 1,
"react/jsx-closing-bracket-location": 0,
"react/jsx-indent-props": 0,
"react/jsx-key": 1,
"react/jsx-max-props-per-line": 0,
"react/jsx-no-bind": 1,
"react/jsx-no-duplicate-props": 1,
"react/jsx-no-literals": 0,
"react/jsx-no-undef": 1,
"react/jsx-pascal-case": 1,
"react/jsx-sort-prop-types": 0,
"react/jsx-sort-props": 0,
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1,
"react/no-danger": 1,
"react/no-did-mount-set-state": 1,
"react/no-did-update-set-state": 1,
"react/no-direct-mutation-state": 1,
"react/no-multi-comp": 1,
"react/no-set-state": 0,
"react/no-unknown-property": 1,
"react/prefer-es6-class": 1,
"react/prop-types": 1,
"react/react-in-jsx-scope": 1,
"react/require-extension": 1,
"react/self-closing-comp": 1,
"react/sort-comp": 1,
"react/wrap-multilines": 1
}
}
通过eslint运行此操作会给我带来2个错误:
6:20 error Parse errors in imported module './styles.module.css': Unexpected token . (1:1) import/namespace
6:20 error Parse errors in imported module './styles.module.css': Unexpected token . (1:1) import/default
当我将以下设置添加到.eslintrc文件时:
"settings": {
"import/ignore": [".css$"]
}
解析错误消失了,但是以下错误取而代之:
1:8 error No default export found in module import/default
1:17 error PropTypes not found in 'react' import/named
5:8 error No default export found in module import/default
关于如何让eslint与React CSS Modules搭配好玩的任何建议?
答案 0 :(得分:3)
我确实找到了一个解决方案:如果我将.eslintrc中的导入/忽略设置更改为
"settings": {
"import/ignore": [".css$","node_modules/*"]
}
然后所有的掉毛错误都消失了。我想可以避免在node_modules
中扯掉所有东西,但是它唠叨我,我不知道造成错误的原因是什么。
答案 1 :(得分:0)
这是因为 import styles from './styles.module.css';
使 Node 尝试像加载 JS 一样加载 CSS 模块,并且在您的情况下它会导致服务器端渲染或 Jest 崩溃。正确的解决方案是为服务器端/Jest 执行配置 Babel,将这些 CSS 导入转换为样式映射,就像 Webpack 所做的那样。有a plugin for that。