How it works...

What will change from the moment you start using flow-remove-types? First, obviously, you cannot just run your project with a simple node src/somefilename.js; first you'll have to strip Flow by npm run build. The effect of this command will be to create a copy in out/, of everything in src/, but without type declarations. Then, you will be able to run the project by doing node out/somefilename.js—filenames won't be changed.

When flow-remove-types package cleans up your files, it replaces all type declarations with whitespaces, so the transformed output files have exactly the same number of lines, and every function starts at exactly the same line as before, removing the need for sourcemaps and keeping the output legible. The following code shows how part of our module from the Working with modules section would look after the process:

/* @flow */
"use strict";

// These won't be exported:

const roundToCents = (x: number): number => Math.round(x * 100) / 100;

const changeSign = (x: number): number => -x;

// The following will be exported:

const addR = (x: number, y: number): number => roundToCents(x + y);

const subR = (x: number, y: number): number => addR(x, changeSign(y));

const multR = (x: number, y: number): number => roundToCents(x * y);

const divR = (x: number, y: number): number => {
if (y === 0) {
throw new Error("Divisor must be nonzero");
} else {
return roundToCents(x / y);
}
};

If you would rather have a smaller-sized output (after all, reading code with all those blank spaces can be a bit tiresome) you can produce a source map and remove all spaces, by adding a couple of parameters to your build script, or by adding a different script, as shown in the following code snippet:

"scripts": {
"build": "flow-remove-types src/ -d out/",
"buildWithMaps": "flow-remove-types src/ -d out/ --pretty --sourcemaps",
.
.
.
},
The Node debugger included in VSC fully supports source maps, so producing briefer code won't be a problem. We'll get to see more about this in Chapter 5, Testing and Debugging Your Server.

Now we have a way to keep working with Node and Flow together, but running our code has become just a tad more complex; let's see if we can fix that!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.187.233