-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flow-remove-types require hook using pirates
Upstream: facebookarchive/flow-remove-types#62
- Loading branch information
Anand Thakker
committed
Feb 27, 2018
1 parent
00cd357
commit 6ec303a
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Temporary patch to work around | ||
* https://github.com/standard-things/esm/issues/119. | ||
* | ||
* Upstream PR: https://github.com/flowtype/flow-remove-types/pull/62 | ||
*/ | ||
|
||
var flowRemoveTypes = require('flow-remove-types'); | ||
var pirates = require('pirates'); | ||
|
||
// Supported options: | ||
// | ||
// - all: Transform all files, not just those with a @flow comment. | ||
// - includes: A Regexp/String to determine which files should be transformed. | ||
// (alias: include) | ||
// - excludes: A Regexp/String to determine which files should not be | ||
// transformed, defaults to ignoring /node_modules/, provide null | ||
// to exclude nothing. (alias: exclude) | ||
var options; | ||
module.exports = function setOptions(newOptions) { | ||
options = newOptions; | ||
} | ||
|
||
var jsLoader = require.extensions['.js']; | ||
var exts = [ '.js', '.mjs', '.jsx', '.flow', '.es6' ]; | ||
|
||
var revert = pirates.addHook(function hook(code, filename) { | ||
try { | ||
var patched = flowRemoveTypes(code, options); | ||
return patched.toString(); | ||
} | ||
catch (e) { | ||
e.message = filename + ': ' + e.message; | ||
throw e; | ||
} | ||
}, { exts, matcher: shouldTransform }); | ||
|
||
function shouldTransform(filename) { | ||
var includes = options && regexpPattern(options.includes || options.include); | ||
var excludes = | ||
options && 'excludes' in options ? regexpPattern(options.excludes) : | ||
options && 'exclude' in options ? regexpPattern(options.exclude) : | ||
/\/node_modules\//; | ||
return (!includes || includes.test(filename)) && !(excludes && excludes.test(filename)); | ||
} | ||
|
||
// Given a null | string | RegExp | any, returns null | Regexp or throws a | ||
// more helpful error. | ||
function regexpPattern(pattern) { | ||
if (!pattern) { | ||
return pattern; | ||
} | ||
// A very simplified glob transform which allows passing legible strings like | ||
// "myPath/*.js" instead of a harder to read RegExp like /\/myPath\/.*\.js/. | ||
if (typeof pattern === 'string') { | ||
pattern = pattern.replace(/\./g, '\\.').replace(/\*/g, '.*'); | ||
if (pattern[0] !== '/') { | ||
pattern = '/' + pattern; | ||
} | ||
return new RegExp(pattern); | ||
} | ||
if (typeof pattern.test === 'function') { | ||
return pattern; | ||
} | ||
throw new Error( | ||
'flow-remove-types: includes and excludes must be RegExp or path strings. Got: ' + pattern | ||
); | ||
} |
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
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