2015-09-22 09:00:04 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-16 18:24:55 -08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-10-22 06:07:01 -07:00
|
|
|
*
|
2017-09-26 12:54:44 -07:00
|
|
|
* @format
|
2016-10-22 06:07:01 -07:00
|
|
|
* @flow
|
2015-09-22 09:00:04 -07:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-09-14 04:20:26 -07:00
|
|
|
const findSymlinkedModules = require('./findSymlinkedModules');
|
2017-07-13 03:30:06 -07:00
|
|
|
const getPolyfills = require('../../rn-get-polyfills');
|
2015-09-22 09:00:04 -07:00
|
|
|
const path = require('path');
|
|
|
|
|
2018-07-25 05:44:40 -07:00
|
|
|
const {createBlacklist} = require('metro');
|
2018-08-02 05:22:57 -07:00
|
|
|
const {loadConfig} = require('metro-config');
|
2017-05-03 06:38:40 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration file of the CLI.
|
|
|
|
*/
|
2018-07-25 05:44:40 -07:00
|
|
|
import type {ConfigT} from 'metro-config/src/configTypes.flow';
|
2017-05-03 06:38:40 -07:00
|
|
|
|
2018-08-22 20:23:11 -07:00
|
|
|
function getProjectRoot() {
|
2017-09-26 12:54:44 -07:00
|
|
|
if (
|
|
|
|
__dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli[\/\\]util$/)
|
|
|
|
) {
|
Fix broken default getProjectRoots
Summary:
In <= 0.44, the default implementation of getProjectRoots() came from `local-cli/core/default.config.js`. With changes happening in the CLI and the packager over the course of the last two months, various pieces of this logic (specifically `local-cli/utils/Config.js`) were rewritten, and though default.config.js was still being imported and used in `local-cli/core/index.js`, the default `getProjectRoots()` was being overriden by the defaults specified in `local-cli/utils/Config.js`.
This PR moves the logic from default.config.js into Config.js and index.js, as appropriate. Specifically:
- The `getProjectCommands()`, `getProjectConfig()`, and `getDependencyConfig()` methods, which have traditionally not been part of the rn-cli.config.js spec, are now defined in `local-cli/core/index.js`.
- The `getProjectRoots()` method, which contained logic for properly resolving the _actual_ project root as well as resolving symlinks within that root, has been moved to `local-cli/utils/Config.js`, to match the fact that other default rn-cli.config.js definitions live there.
Closes https://github.com/facebook/react-native/pull/14412
Differential Revision: D5216887
Pulled By: hramos
fbshipit-source-id: 7a3840ecf0ad8ea3f6d7bbd3d54e4f02950c6a32
2017-06-09 01:14:37 -07:00
|
|
|
// Packager is running from node_modules.
|
|
|
|
// This is the default case for all projects created using 'react-native init'.
|
|
|
|
return path.resolve(__dirname, '../../../..');
|
|
|
|
} else if (__dirname.match(/Pods[\/\\]React[\/\\]packager$/)) {
|
|
|
|
// React Native was installed using CocoaPods.
|
|
|
|
return path.resolve(__dirname, '../../../..');
|
|
|
|
}
|
|
|
|
return path.resolve(__dirname, '../..');
|
|
|
|
}
|
|
|
|
|
2017-09-14 04:20:26 -07:00
|
|
|
const resolveSymlinksForRoots = roots =>
|
|
|
|
roots.reduce(
|
2018-04-17 05:37:02 -07:00
|
|
|
/* $FlowFixMe(>=0.70.0 site=react_native_fb) This comment suppresses an
|
|
|
|
* error found when Flow v0.70 was deployed. To see the error delete this
|
|
|
|
* comment and run Flow. */
|
2017-09-26 12:54:44 -07:00
|
|
|
(arr, rootPath) => arr.concat(findSymlinkedModules(rootPath, roots)),
|
|
|
|
[...roots],
|
Fix broken default getProjectRoots
Summary:
In <= 0.44, the default implementation of getProjectRoots() came from `local-cli/core/default.config.js`. With changes happening in the CLI and the packager over the course of the last two months, various pieces of this logic (specifically `local-cli/utils/Config.js`) were rewritten, and though default.config.js was still being imported and used in `local-cli/core/index.js`, the default `getProjectRoots()` was being overriden by the defaults specified in `local-cli/utils/Config.js`.
This PR moves the logic from default.config.js into Config.js and index.js, as appropriate. Specifically:
- The `getProjectCommands()`, `getProjectConfig()`, and `getDependencyConfig()` methods, which have traditionally not been part of the rn-cli.config.js spec, are now defined in `local-cli/core/index.js`.
- The `getProjectRoots()` method, which contained logic for properly resolving the _actual_ project root as well as resolving symlinks within that root, has been moved to `local-cli/utils/Config.js`, to match the fact that other default rn-cli.config.js definitions live there.
Closes https://github.com/facebook/react-native/pull/14412
Differential Revision: D5216887
Pulled By: hramos
fbshipit-source-id: 7a3840ecf0ad8ea3f6d7bbd3d54e4f02950c6a32
2017-06-09 01:14:37 -07:00
|
|
|
);
|
|
|
|
|
2018-08-22 20:23:11 -07:00
|
|
|
const getWatchFolders = () => {
|
2017-09-15 04:55:29 -07:00
|
|
|
const root = process.env.REACT_NATIVE_APP_ROOT;
|
|
|
|
if (root) {
|
|
|
|
return resolveSymlinksForRoots([path.resolve(root)]);
|
|
|
|
}
|
2018-08-22 20:23:11 -07:00
|
|
|
return [];
|
2017-09-15 04:55:29 -07:00
|
|
|
};
|
2017-09-14 04:20:26 -07:00
|
|
|
|
2018-02-16 20:39:27 -08:00
|
|
|
const getBlacklistRE = () => {
|
|
|
|
return createBlacklist([/.*\/__fixtures__\/.*/]);
|
|
|
|
};
|
|
|
|
|
2015-09-22 09:00:04 -07:00
|
|
|
/**
|
Merge `rnpm config` with the `cli config`, step 1
Summary:
This is step one on merging the `rnpm` config with the `cli config`. The plan is to remove two sources of truth (rnpm package.json config and rn-cli Javascript config)
Rationale:
As of now, we have `rnpm` config, that used to live in the `local-cli/core/config/index.js` and the `rn-cli` config, living in `default.config.js` and `utils/Config.js`.
This PR moves all the things into one file, called `local-cli/core', simplifies things, enhances types (Config now has better types, added missing properties and fixed descriptions).
One notable addition is that users can now opt-in to override the commands that are loaded at the package level. Previously it was only possible to add a command by writing a plugin. Now, you can just tweak the `rn-cli.config.js`.
This is one of the several improvements I have on my roadmap, with better documentation for the CLI as well.
Closes https://github.com/facebook/react-native/pull/11564
Differential Revision: D4360095
fbshipit-source-id: feaec7c88df63e51cef1f9620c7eedeb738d3d00
2017-01-09 07:49:29 -08:00
|
|
|
* Module capable of getting the configuration out of a given file.
|
2015-10-20 16:58:13 -07:00
|
|
|
*
|
Merge `rnpm config` with the `cli config`, step 1
Summary:
This is step one on merging the `rnpm` config with the `cli config`. The plan is to remove two sources of truth (rnpm package.json config and rn-cli Javascript config)
Rationale:
As of now, we have `rnpm` config, that used to live in the `local-cli/core/config/index.js` and the `rn-cli` config, living in `default.config.js` and `utils/Config.js`.
This PR moves all the things into one file, called `local-cli/core', simplifies things, enhances types (Config now has better types, added missing properties and fixed descriptions).
One notable addition is that users can now opt-in to override the commands that are loaded at the package level. Previously it was only possible to add a command by writing a plugin. Now, you can just tweak the `rn-cli.config.js`.
This is one of the several improvements I have on my roadmap, with better documentation for the CLI as well.
Closes https://github.com/facebook/react-native/pull/11564
Differential Revision: D4360095
fbshipit-source-id: feaec7c88df63e51cef1f9620c7eedeb738d3d00
2017-01-09 07:49:29 -08:00
|
|
|
* The function will return all the default configuration, as specified by the
|
2017-09-15 04:55:29 -07:00
|
|
|
* `DEFAULT` param overriden by those found on `rn-cli.config.js` files, if any. If no
|
Merge `rnpm config` with the `cli config`, step 1
Summary:
This is step one on merging the `rnpm` config with the `cli config`. The plan is to remove two sources of truth (rnpm package.json config and rn-cli Javascript config)
Rationale:
As of now, we have `rnpm` config, that used to live in the `local-cli/core/config/index.js` and the `rn-cli` config, living in `default.config.js` and `utils/Config.js`.
This PR moves all the things into one file, called `local-cli/core', simplifies things, enhances types (Config now has better types, added missing properties and fixed descriptions).
One notable addition is that users can now opt-in to override the commands that are loaded at the package level. Previously it was only possible to add a command by writing a plugin. Now, you can just tweak the `rn-cli.config.js`.
This is one of the several improvements I have on my roadmap, with better documentation for the CLI as well.
Closes https://github.com/facebook/react-native/pull/11564
Differential Revision: D4360095
fbshipit-source-id: feaec7c88df63e51cef1f9620c7eedeb738d3d00
2017-01-09 07:49:29 -08:00
|
|
|
* default config is provided and no configuration can be found in the directory
|
|
|
|
* hierarchy, an error will be thrown.
|
2015-09-22 09:00:04 -07:00
|
|
|
*/
|
|
|
|
const Config = {
|
2018-07-25 05:44:40 -07:00
|
|
|
DEFAULT: {
|
|
|
|
resolver: {
|
|
|
|
resolverMainFields: ['react-native', 'browser', 'main'],
|
|
|
|
blacklistRE: getBlacklistRE(),
|
|
|
|
},
|
|
|
|
serializer: {
|
|
|
|
getModulesRunBeforeMainModule: () => [
|
|
|
|
require.resolve('../../Libraries/Core/InitializeCore'),
|
|
|
|
],
|
|
|
|
getPolyfills,
|
|
|
|
},
|
Expose the actual transformer in the config
Summary:
This diff exposes the new more generic way to configure transformers in `Metro` via the config parameter `transformerPath`.
The new generic transformers can be used to transform any kind of file, since they don't call any JS-specific method and their API is generic. They only need to implement a single `transform` method:
```
async function transform(
absolutePath: string,
relativePath: string,
fileContents: Buffer,
options: TransformOptions, // very soon these will be configurable
): Promise<{
output: Array<mixed>,
dependencies: Array<{
name: string,
data: mixed, // very soon
}>,
}> {
// ...
}
```
Metro already had a `transformModulePath` config param, which was used to configure how babel was called in order to generate the AST. In order to avoid confusion, but keep the current open source transformer worker, I've renamed this param to `babelTransformerPath`. We can add a layer of compatibility and detect old config params in order to show a deprecation warning.
Reviewed By: pvdz
Differential Revision: D9070810
fbshipit-source-id: aebde879736026c09537f5d236eae24c06640abf
2018-08-23 15:44:48 -07:00
|
|
|
transformer: {
|
|
|
|
babelTransformerPath: require.resolve('metro/src/reactNativeTransformer'),
|
|
|
|
},
|
2018-08-22 20:23:11 -07:00
|
|
|
watchFolders: getWatchFolders(),
|
2016-08-12 11:46:04 -07:00
|
|
|
},
|
2015-10-20 16:58:13 -07:00
|
|
|
|
2018-07-27 20:31:20 -07:00
|
|
|
async load(configFile: ?string): Promise<ConfigT> {
|
2018-08-22 20:23:11 -07:00
|
|
|
const argv = {cwd: getProjectRoot()};
|
|
|
|
|
2018-08-02 05:22:57 -07:00
|
|
|
return await loadConfig(
|
2018-08-22 20:23:11 -07:00
|
|
|
configFile ? {...argv, config: configFile} : argv,
|
2018-08-02 05:22:57 -07:00
|
|
|
this.DEFAULT,
|
2018-07-27 20:31:20 -07:00
|
|
|
);
|
2016-08-12 11:46:04 -07:00
|
|
|
},
|
2015-09-22 09:00:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Config;
|