2015-09-22 16:00:04 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2016-10-22 13:07:01 +00:00
|
|
|
*
|
|
|
|
* @flow
|
2015-09-22 16:00:04 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-05-03 13:38:40 +00:00
|
|
|
const blacklist = require('../../packager/blacklist');
|
2015-09-22 16:00:04 +00:00
|
|
|
const fs = require('fs');
|
2017-05-03 09:47:38 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-09-22 16:00:04 +00:00
|
|
|
const path = require('path');
|
|
|
|
|
2017-05-03 13:38:40 +00:00
|
|
|
const {providesModuleNodeModules} = require('../../packager/defaults');
|
|
|
|
|
2015-09-22 16:00:04 +00:00
|
|
|
const RN_CLI_CONFIG = 'rn-cli.config.js';
|
|
|
|
|
2017-05-03 13:38:40 +00:00
|
|
|
import type {GetTransformOptions, PostMinifyProcess, PostProcessModules} from '../../packager/src/Bundler';
|
|
|
|
import type {HasteImpl} from '../../packager/src/node-haste/Module';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration file of the CLI.
|
|
|
|
*/
|
|
|
|
export type ConfigT = {
|
|
|
|
extraNodeModules: {[id: string]: string},
|
|
|
|
/**
|
2017-05-04 12:06:27 +00:00
|
|
|
* Specify any additional asset file extensions to be used by the packager.
|
2017-05-03 13:38:40 +00:00
|
|
|
* For example, if you want to include a .ttf file, you would return ['ttf']
|
|
|
|
* from here and use `require('./fonts/example.ttf')` inside your app.
|
|
|
|
*/
|
|
|
|
getAssetExts: () => Array<string>,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a regular expression for modules that should be ignored by the
|
|
|
|
* packager on a given platform.
|
|
|
|
*/
|
|
|
|
getBlacklistRE(): RegExp,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify any additional platforms to be used by the packager.
|
|
|
|
* For example, if you want to add a "custom" platform, and use modules
|
|
|
|
* ending in .custom.js, you would return ['custom'] here.
|
|
|
|
*/
|
|
|
|
getPlatforms: () => Array<string>,
|
|
|
|
|
|
|
|
getProjectRoots(): Array<string>,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify any additional node modules that should be processed for
|
|
|
|
* providesModule declarations.
|
|
|
|
*/
|
|
|
|
getProvidesModuleNodeModules?: () => Array<string>,
|
2017-05-04 12:06:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify any additional source file extensions to be used by the packager.
|
|
|
|
* For example, if you want to include a .ts file, you would return ['ts']
|
|
|
|
* from here and use `require('./module/example')` to require the file with
|
|
|
|
* path 'module/example.ts' inside your app.
|
|
|
|
*/
|
|
|
|
getSourceExts: () => Array<string>,
|
|
|
|
|
2017-05-03 13:38:40 +00:00
|
|
|
/**
|
|
|
|
* Returns the path to a custom transformer. This can also be overridden
|
|
|
|
* with the --transformer commandline argument.
|
|
|
|
*/
|
|
|
|
getTransformModulePath: () => string,
|
|
|
|
getTransformOptions: GetTransformOptions,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An optional function that can modify the code and source map of bundle
|
|
|
|
* after the minifaction took place.
|
|
|
|
*/
|
|
|
|
postMinifyProcess: PostMinifyProcess,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An optional function that can modify the module array before the bundle is
|
|
|
|
* finalized.
|
|
|
|
*/
|
|
|
|
postProcessModules: PostProcessModules,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A module that exports:
|
|
|
|
* - a `getHasteName(filePath)` method that returns `hasteName` for module at
|
|
|
|
* `filePath`, or undefined if `filePath` is not a haste module.
|
|
|
|
*/
|
|
|
|
hasteImpl?: HasteImpl,
|
|
|
|
|
|
|
|
transformVariants: () => {[name: string]: Object},
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultConfig: ConfigT = {
|
|
|
|
extraNodeModules: Object.create(null),
|
|
|
|
getAssetExts: () => [],
|
|
|
|
getBlacklistRE: () => blacklist(),
|
|
|
|
getPlatforms: () => [],
|
|
|
|
getProjectRoots: () => [process.cwd()],
|
|
|
|
getProvidesModuleNodeModules: () => providesModuleNodeModules.slice(),
|
2017-05-04 12:06:27 +00:00
|
|
|
getSourceExts: () => [],
|
2017-05-03 13:38:40 +00:00
|
|
|
getTransformModulePath: () => path.resolve(__dirname, '../../packager/transformer'),
|
|
|
|
getTransformOptions: async () => ({}),
|
|
|
|
postMinifyProcess: x => x,
|
|
|
|
postProcessModules: modules => modules,
|
|
|
|
transformVariants: () => ({default: {}}),
|
|
|
|
};
|
2016-10-22 13:07:01 +00:00
|
|
|
|
2015-09-22 16:00:04 +00: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 15:49:29 +00:00
|
|
|
* Module capable of getting the configuration out of a given file.
|
2015-10-20 23:58:13 +00: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 15:49:29 +00:00
|
|
|
* The function will return all the default configuration, as specified by the
|
|
|
|
* `defaultConfig` param overriden by those found on `rn-cli.config.js` files, if any. If no
|
|
|
|
* default config is provided and no configuration can be found in the directory
|
|
|
|
* hierarchy, an error will be thrown.
|
2015-09-22 16:00:04 +00:00
|
|
|
*/
|
|
|
|
const Config = {
|
2017-05-03 13:38:40 +00:00
|
|
|
find(startDir: string): ConfigT {
|
2017-05-03 09:47:38 +00:00
|
|
|
const configPath = findConfigPath(startDir);
|
|
|
|
invariant(
|
|
|
|
configPath,
|
|
|
|
`Can't find "${RN_CLI_CONFIG}" file in any parent folder of "${startDir}"`,
|
|
|
|
);
|
|
|
|
return this.loadFile(configPath, startDir);
|
|
|
|
},
|
2015-09-22 16:00:04 +00:00
|
|
|
|
2017-05-03 13:38:40 +00:00
|
|
|
findOptional(startDir: string): ConfigT {
|
2017-05-03 09:47:38 +00:00
|
|
|
const configPath = findConfigPath(startDir);
|
|
|
|
return configPath
|
|
|
|
? this.loadFile(configPath, startDir)
|
2017-05-03 13:38:41 +00:00
|
|
|
: {...defaultConfig};
|
2016-08-12 18:46:04 +00:00
|
|
|
},
|
2015-10-20 23:58:13 +00:00
|
|
|
|
2017-05-03 13:38:42 +00:00
|
|
|
loadFile(pathToConfig: string): ConfigT {
|
|
|
|
//$FlowFixMe: necessary dynamic require
|
|
|
|
const config: {} = require(pathToConfig);
|
2017-05-03 13:38:41 +00:00
|
|
|
return {...defaultConfig, ...config};
|
2016-08-12 18:46:04 +00:00
|
|
|
},
|
2015-09-22 16:00:04 +00:00
|
|
|
};
|
|
|
|
|
2017-05-03 09:47:38 +00:00
|
|
|
function findConfigPath(cwd: string): ?string {
|
|
|
|
const parentDir = findParentDirectory(cwd, RN_CLI_CONFIG);
|
|
|
|
return parentDir ? path.join(parentDir, RN_CLI_CONFIG) : null;
|
|
|
|
}
|
|
|
|
|
2015-09-22 16:00:04 +00:00
|
|
|
// Finds the most near ancestor starting at `currentFullPath` that has
|
|
|
|
// a file named `filename`
|
|
|
|
function findParentDirectory(currentFullPath, filename) {
|
|
|
|
const root = path.parse(currentFullPath).root;
|
|
|
|
const testDir = (parts) => {
|
|
|
|
if (parts.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fullPath = path.join(root, parts.join(path.sep));
|
|
|
|
|
|
|
|
var exists = fs.existsSync(path.join(fullPath, filename));
|
|
|
|
return exists ? fullPath : testDir(parts.slice(0, -1));
|
|
|
|
};
|
|
|
|
|
2016-03-21 01:08:23 +00:00
|
|
|
return testDir(currentFullPath.substring(root.length).split(path.sep));
|
2015-09-22 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Config;
|