Last active
January 21, 2020 11:13
-
-
Save dmisdm/2c1b2ba682c18cf60edabb101347f484 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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