Skip to content

Commit

Permalink
Add flow-remove-types require hook using pirates
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Mar 1, 2018
1 parent 2dfec41 commit c17916a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
68 changes: 68 additions & 0 deletions build/flow-remove-types.js
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
);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"npm-run-all": "^4.0.1",
"nyc": "^10.1.2",
"object.entries": "^1.0.4",
"pirates": "^3.0.2",
"pngjs": "^3.0.0",
"prismjs": "^1.8.1",
"prop-types": "^15.6.0",
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6861,6 +6861,10 @@ node-libs-browser@^2.0.0:
util "^0.10.3"
vm-browserify "0.0.4"

node-modules-regexp@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-modules-regexp/-/node-modules-regexp-1.0.0.tgz#8d9dbe28964a4ac5712e9131642107c71e90ec40"

node-pre-gyp@^0.6.39:
version "0.6.39"
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.39.tgz#c00e96860b23c0e1420ac7befc5044e1d78d8649"
Expand Down Expand Up @@ -7540,6 +7544,12 @@ pinkie@^2.0.0:
version "2.0.4"
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"

pirates@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/pirates/-/pirates-3.0.2.tgz#7e6f85413fd9161ab4e12b539b06010d85954bb9"
dependencies:
node-modules-regexp "^1.0.0"

pixelmatch@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/pixelmatch/-/pixelmatch-4.0.2.tgz#8f47dcec5011b477b67db03c243bc1f3085e8854"
Expand Down

0 comments on commit c17916a

Please sign in to comment.