So basically FlowType doesn't know about CSS Modules, a really handy way of dealing with the plagues of CSS in codebases (global variables and dependency wackiness mainly).
What WebPack allows us to do is "require" CSS files and use their class names:
import styles from "my_styles.css";
import React from "react";
const MyComponent = React.createClass({
render() {
return <h1 className={styles.redHeader}>Hello!</h1>;
}
});
Unfortunately, Flow will give us an error Required module not found
because, well, let's be honest, importing CSS with JavaScript is pretty out of this world and a little bit crazy (i.e: this).
So here's what I did to fix that. Flow has a nice way of dealing with this in its options, namely one called module.name_mapper
. Somebody was kind enough to make an npm module called empty
thatβ you guessed itβ returns empty objects and arrays. I'm pretty amazed to have found a use for this.
So as a fix, do this:
Run npm install --save empty
in your project directory.
Open your .flowconfig
, and add the following under [options]
:
module.name_mapper='.*\(.css\)' -> 'empty/object'
Ta-da! Another fun day in JavaScript land.
By "works", do you guys just mean that Flow manages to parse your code correctly? Have you checked whether Flow actually can check that the correct props are specified when you use your component in JSX for example?
I was able to do something similar to what was suggested here but that will apparently make
import CssModules from 'react-css-modules; export default CssModules(MyComponent, styles, options)
spit out a new Component which is missing the information about the props so Flow cannot check it in JSX.I was trying to put together a more sophisticated workaround today but without success. Would be interesting to hear if you also had this problem and if you can manage to figure out how to get my solution to work. I documented my efforts here: facebook/flow#2536