|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const path = require('path'); |
| 14 | +const cli = require('@react-native-community/cli'); |
| 15 | + |
| 16 | +// Use duck-typing because of Facebook-internal infra that doesn't have the cli package. |
| 17 | +const {haste} = (cli.loadConfig && cli.loadConfig()) || { |
| 18 | + haste: { |
| 19 | + providesModuleNodeModules: [], |
| 20 | + platforms: ['ios', 'android'], |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +// Detect out-of-tree platforms and add them to the whitelists |
| 25 | +const pluginRoots /*: Array<string> */ = haste.providesModuleNodeModules.map( |
| 26 | + name => path.resolve(__dirname, '../../', name) + path.sep, |
| 27 | +); |
| 28 | + |
| 29 | +const pluginNameReducers /*: Array<[RegExp, string]> */ = haste.platforms.map( |
| 30 | + name => [new RegExp(`^(.*)\\.(${name})$`), '$1'], |
| 31 | +); |
| 32 | + |
| 33 | +const ROOTS = [path.resolve(__dirname, '..') + path.sep, ...pluginRoots]; |
| 34 | + |
| 35 | +const BLACKLISTED_PATTERNS /*: Array<RegExp> */ = [ |
| 36 | + /.*[\\\/]__(mocks|tests)__[\\\/].*/, |
| 37 | + /^Libraries[\\\/]Animated[\\\/]src[\\\/]polyfills[\\\/].*/, |
| 38 | + /^Libraries[\\\/]Renderer[\\\/]fb[\\\/].*/, |
| 39 | + /DerivedData[\\\/].*/, |
| 40 | +]; |
| 41 | + |
| 42 | +const WHITELISTED_PREFIXES /*: Array<string> */ = [ |
| 43 | + 'IntegrationTests', |
| 44 | + 'Libraries', |
| 45 | + 'ReactAndroid', |
| 46 | + 'RNTester', |
| 47 | +]; |
| 48 | + |
| 49 | +const NAME_REDUCERS /*: Array<[RegExp, string]> */ = [ |
| 50 | + // extract basename |
| 51 | + [/^(?:.*[\\\/])?([a-zA-Z0-9$_.-]+)$/, '$1'], |
| 52 | + // strip .js/.js.flow suffix |
| 53 | + [/^(.*)\.js(\.flow)?$/, '$1'], |
| 54 | + // strip native suffix |
| 55 | + [/^(.*)\.(native)$/, '$1'], |
| 56 | + // strip plugin platform suffixes |
| 57 | + ...pluginNameReducers, |
| 58 | +]; |
| 59 | + |
| 60 | +function isHastePath(filePath /*: string */) /*: boolean */ { |
| 61 | + if (!filePath.endsWith('.js') && !filePath.endsWith('.js.flow')) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + const root = ROOTS.find(r => filePath.startsWith(r)); |
| 66 | + if (!root) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + filePath = filePath.substr(root.length); |
| 71 | + if (BLACKLISTED_PATTERNS.some(pattern => pattern.test(filePath))) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + return WHITELISTED_PREFIXES.some(prefix => filePath.startsWith(prefix)); |
| 75 | +} |
| 76 | + |
| 77 | +module.exports = { |
| 78 | + /* |
| 79 | + * @return {string|void} hasteName for module at filePath; or undefined if |
| 80 | + * filePath is not a haste module |
| 81 | + */ |
| 82 | + getHasteName( |
| 83 | + filePath /*: string */, |
| 84 | + sourceCode /*: ?string */, |
| 85 | + ) /*: string | void */ { |
| 86 | + if (!isHastePath(filePath)) { |
| 87 | + return undefined; |
| 88 | + } |
| 89 | + |
| 90 | + const hasteName = NAME_REDUCERS.reduce( |
| 91 | + (name, [pattern, replacement]) => name.replace(pattern, replacement), |
| 92 | + filePath, |
| 93 | + ); |
| 94 | + |
| 95 | + return hasteName; |
| 96 | + }, |
| 97 | +}; |
0 commit comments