-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
I've being struggling to type check apps that use compiled flow libraries.
The problem is that in my program I import a package 'foo' who's package.json#main
points to ./dist/index.js
where dist file are generated via babeljs and have an UMD format and look roughly like this:
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports', 'react'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports, require('react'));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.React);
global.index = mod.exports;
}
})(this, function (exports, _react) {
'use strict';
/*::
import type {VirtualElement} from "reflex/src/core";
import type {Address} from "reflex/src/signal";
type RenderTarget = Node|Element|HTMLElement;
type Configuration = {target: RenderTarget};
...
What this gives us is:
- If you ignore
dist
flow fails to import foo and fails to type check for that reason. - If you do not ignore
dist
it will attempt to also type checkfoo/dist/index.js
and still fail but in this case with "Unexpected token import", presumably because imports supposed to be at the top level not inside the function.
Either options seems bad though and even if flow was able to handle foo/dist/index.js
and not complain about import inside a function it still would not be ideal as comments could be stripped out in dist files. It would make much more sense if it was passible to tell flow that source for this package is in this in this directory. I guess it has to be something like [include]
directive but with a package name mapping so that imports would be resolved correctly.
Another (probably better) alternative could be to introduce "flow" field for the package.json
so that package.json#flow.main
could take precedence over package.json#main
when flow resolves import path.