Skip to content

Instantly share code, notes, and snippets.

@dmisdm
Last active January 21, 2020 11:13
Show Gist options
  • Save dmisdm/2c1b2ba682c18cf60edabb101347f484 to your computer and use it in GitHub Desktop.
Save dmisdm/2c1b2ba682c18cf60edabb101347f484 to your computer and use it in GitHub Desktop.
// Generate .flow.js from .ts files so that flow can understand TS and the two can live in peace at least until we have time to completely overhaul flow
const flowgen = require('flowgen');
const fs = require('fs');
const name = 'FlowgenWebpackPlugin';
const tsPattern = /\.(ts|tsx)$/;
class FlowgenWebpackPlugin {
apply(compiler) {
compiler.hooks.compilation.tap(name, compilation => {
const compiledModules = {};
compilation.hooks.succeedModule.tap(name, module => {
if (
module.issuer &&
tsPattern.test(module.issuer.resource) &&
!compiledModules[module.issuer.resource]
) {
fs.writeFile(
`${module.issuer.resource}.flow`,
flowgen.beautify(
flowgen.compiler.compileDefinitionFile(module.issuer.resource)
)
);
compiledModules[module.issuer.resource] = true;
}
});
});
}
}
module.exports = FlowgenWebpackPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment