Modern JavaScript Web Development Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it…

Installing Flow depends on whether you are working with Babel (as would be the case for client-side browser code) or not (as you would do for server-side code). We will see how to deal with Node starting in Chapter 3, Developing with Node; here, we'll just consider Babel. 

To start, execute the following command to get the needed Flow packages, including the Babel and ESLint ones:

npm install flow-bin babel-preset-flow eslint-plugin-flowtype --save-dev

Then, add the "flow" preset for Babel in package.json

"babel": {
"presets": ["env", "flow"]
},

Add some lines to the ESLint configuration, also in package.json:

"eslintConfig": {
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"env": {
"browser": true,
"node": true
},
"parser": "babel-eslint",
"extends": ["eslint:recommended", "plugin:flowtype/recommended"],
"plugins": ["flowtype"],
"rules": {
.
.
.
}
},

Add a "flow" script in package.json:

"scripts": {
"build": "babel src -d out",
"flow": "flow",
.
.
.
},

Finally, perform npm run flow init to initialize Flow, only once, to create a .flowconfig file with information that will be used by the Flow process. (See https://flow.org/en/docs/config/ for more information on this file.)

The .flowconfig file doesn't really match the style of other configuration files, and should really be a JSON file instead, possibly part of package.json. However, this is a still pending item; you can check  https://github.com/facebook/flow/issues/153 to monitor advances, but for the time being, you'll have to deal with .flowconfig as is.