1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var flowRemoveTypes = require('./index');
|
9 | var pirates = require('pirates');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | var options;
|
20 | module.exports = function setOptions(newOptions) {
|
21 | options = newOptions;
|
22 | };
|
23 |
|
24 | var jsLoader = require.extensions['.js'];
|
25 | var exts = ['.js', '.mjs', '.cjs', '.jsx', '.flow', '.es6'];
|
26 |
|
27 | var revert = pirates.addHook(
|
28 | function hook(code, filename) {
|
29 | try {
|
30 | return flowRemoveTypes(code, options).toString();
|
31 | } catch (e) {
|
32 | e.message = filename + ': ' + e.message;
|
33 | throw e;
|
34 | }
|
35 | },
|
36 | {exts: exts, matcher: shouldTransform}
|
37 | );
|
38 |
|
39 | function shouldTransform(filename) {
|
40 | var includes = options && regexpPattern(options.includes || options.include);
|
41 | var excludes =
|
42 | options && 'excludes' in options
|
43 | ? regexpPattern(options.excludes)
|
44 | : options && 'exclude' in options
|
45 | ? regexpPattern(options.exclude)
|
46 | : /\/node_modules\//;
|
47 | return (
|
48 | (!includes || includes.test(filename)) &&
|
49 | !(excludes && excludes.test(filename))
|
50 | );
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | function regexpPattern(pattern) {
|
56 | if (!pattern) {
|
57 | return pattern;
|
58 | }
|
59 |
|
60 |
|
61 | if (typeof pattern === 'string') {
|
62 | pattern = pattern.replace(/\./g, '\\.').replace(/\*/g, '.*');
|
63 | if (pattern[0] !== '/') {
|
64 | pattern = '/' + pattern;
|
65 | }
|
66 | return new RegExp(pattern);
|
67 | }
|
68 | if (typeof pattern.test === 'function') {
|
69 | return pattern;
|
70 | }
|
71 | throw new Error(
|
72 | 'flow-remove-types: ' +
|
73 | 'includes and excludes must be RegExp or path strings. Got: ' +
|
74 | pattern
|
75 | );
|
76 | }
|