diff --git a/build/flow-remove-types.js b/build/flow-remove-types.js new file mode 100644 index 00000000000..e67a9200924 --- /dev/null +++ b/build/flow-remove-types.js @@ -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 + ); +} diff --git a/package.json b/package.json index 020923521e1..49cc3226764 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index 911f8637f7a..527219812f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"