Advertisement
giaynhap123

fixreactconfig

Mar 21st, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. yarn add react-native@0.54.0-rc.3
  2.  
  3. /**
  4. * Copyright (c) 2015-present, Facebook, Inc.
  5. * All rights reserved.
  6. *
  7. * This source code is licensed under the BSD-style license found in the
  8. * LICENSE file in the root directory of this source tree. An additional grant
  9. * of patent rights can be found in the PATENTS file in the same directory.
  10. *
  11. * @format
  12. * @flow
  13. */
  14. 'use strict';
  15.  
  16. const findSymlinkedModules = require('./findSymlinkedModules');
  17. const fs = require('fs');
  18. const getPolyfills = require('../../rn-get-polyfills');
  19. const invariant = require('fbjs/lib/invariant');
  20. const path = require('path');
  21.  
  22. const {Config: MetroConfig, createBlacklist} = require('metro');
  23.  
  24. const RN_CLI_CONFIG = 'rn-cli.config.js';
  25.  
  26. import type {ConfigT as MetroConfigT} from 'metro';
  27.  
  28. /**
  29. * Configuration file of the CLI.
  30. */
  31. export type ConfigT = MetroConfigT;
  32.  
  33. function getProjectPath() {
  34. if (
  35. __dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli[\/\\]util$/)
  36. ) {
  37. // Packager is running from node_modules.
  38. // This is the default case for all projects created using 'react-native init'.
  39. return path.resolve(__dirname, '../../../..');
  40. } else if (__dirname.match(/Pods[\/\\]React[\/\\]packager$/)) {
  41. // React Native was installed using CocoaPods.
  42. return path.resolve(__dirname, '../../../..');
  43. }
  44. return path.resolve(__dirname, '../..');
  45. }
  46.  
  47. const resolveSymlinksForRoots = roots =>
  48. roots.reduce(
  49. (arr, rootPath) => arr.concat(findSymlinkedModules(rootPath, roots)),
  50. [...roots],
  51. );
  52.  
  53. const getProjectRoots = () => {
  54. const root = process.env.REACT_NATIVE_APP_ROOT;
  55. if (root) {
  56. return resolveSymlinksForRoots([path.resolve(root)]);
  57. }
  58. return resolveSymlinksForRoots([getProjectPath()]);
  59. };
  60.  
  61. const getBlacklistRE = () => {
  62. return createBlacklist([/.*\/__fixtures__\/.*/]);
  63. };
  64.  
  65. /**
  66. * Module capable of getting the configuration out of a given file.
  67. *
  68. * The function will return all the default configuration, as specified by the
  69. * `DEFAULT` param overriden by those found on `rn-cli.config.js` files, if any. If no
  70. * default config is provided and no configuration can be found in the directory
  71. * hierarchy, an error will be thrown.
  72. */
  73. const Config = {
  74. DEFAULT: ({
  75. ...MetroConfig.DEFAULT,
  76. getBlacklistRE,
  77. getProjectRoots,
  78. getPolyfills,
  79. getModulesRunBeforeMainModule: () => [
  80. require.resolve('../../Libraries/Core/InitializeCore'),
  81. ],
  82. }: ConfigT),
  83.  
  84. find(startDir: string): ConfigT {
  85. return this.findWithPath(startDir).config;
  86. },
  87.  
  88. findWithPath(startDir: string): {config: ConfigT, projectPath: string} {
  89. const configPath = findConfigPath(startDir);
  90. invariant(
  91. configPath,
  92. `Can't find "${RN_CLI_CONFIG}" file in any parent folder of "${startDir}"`,
  93. );
  94. const projectPath = path.dirname(configPath);
  95. return {config: this.load(configPath, startDir), projectPath};
  96. },
  97.  
  98. findOptional(startDir: string): ConfigT {
  99. const configPath = findConfigPath(startDir);
  100. return configPath ? this.load(configPath, startDir) : {...Config.DEFAULT};
  101. },
  102.  
  103. getProjectPath,
  104. getProjectRoots,
  105.  
  106. load(configFile: string): ConfigT {
  107. return MetroConfig.load(configFile, Config.DEFAULT);
  108. },
  109. };
  110.  
  111. function findConfigPath(cwd: string): ?string {
  112. const parentDir = findParentDirectory(cwd, RN_CLI_CONFIG);
  113. return parentDir ? path.join(parentDir, RN_CLI_CONFIG) : null;
  114. }
  115.  
  116. // Finds the most near ancestor starting at `currentFullPath` that has
  117. // a file named `filename`
  118. function findParentDirectory(currentFullPath, filename) {
  119. const root = path.parse(currentFullPath).root;
  120. const testDir = parts => {
  121. if (parts.length === 0) {
  122. return null;
  123. }
  124.  
  125. const fullPath = path.join(root, parts.join(path.sep));
  126.  
  127. var exists = fs.existsSync(path.join(fullPath, filename));
  128. return exists ? fullPath : testDir(parts.slice(0, -1));
  129. };
  130.  
  131. return testDir(currentFullPath.substring(root.length).split(path.sep));
  132. }
  133.  
  134. module.exports = Config;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement