876758b27940835022dd73f96d472da46370ad62.svn-base
2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var flowRemoveTypes = require('./index');
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', '.cjs', '.jsx', '.flow', '.es6'];
var revert = pirates.addHook(
function hook(code, filename) {
try {
return flowRemoveTypes(code, options).toString();
} catch (e) {
e.message = filename + ': ' + e.message;
throw e;
}
},
{exts: 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
);
}