Files
react-native/scripts/build/config.js
T
Alex Hunt b41a33ede9 Move metro-config package into monorepo build, enable TS generation (#41836)
Summary:
This adds `react-native/metro-config` to the monorepo build tool and emits the missing typescript declarations.

Right now, we do have typescript declarations on `metro-config`, but not `react-native/metro-config`. Which makes everything a bit harder extend from "[the default React Native metro config](https://github.com/facebook/react-native/pull/36502)" in Expo.

> Note, I also added the same `exports` block from `react-native/dev-middleware` for conformity.

One open question here is, why aren't we exporting _all_ helper functions from `metro-config`? To me, its a bit weird that we need both `metro-config` _and_ `react-native/metro-config` as `loadConfig` isn't exported.

## Changelog:

[INTERNAL] [FIXED] - Emit typescript declaration files for `react-native/metro-config`

Pull Request resolved: https://github.com/facebook/react-native/pull/41836

Test Plan:
Run the build tool, and check if the typescript declarations are emitted for `react-native/metro-config`.

```
yarn build metro-config
```

Reviewed By: hoxyq

Differential Revision: D51943453

Pulled By: huntie

fbshipit-source-id: cfaffe5660053fc9a9fcbe3dacf7f6ccc2bde01b
2024-02-12 05:20:38 -08:00

104 lines
2.3 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall react_native
*/
/*::
import type {BabelCoreOptions} from '@babel/core';
*/
const {ModuleResolutionKind} = require('typescript');
/*::
export type BuildOptions = $ReadOnly<{
// The target runtime to compile for.
target: 'node',
// Whether to emit Flow definition files (.js.flow) (default: true).
emitFlowDefs?: boolean,
// Whether to emit TypeScript definition files (.d.ts) (default: false).
emitTypeScriptDefs?: boolean,
}>;
export type BuildConfig = $ReadOnly<{
// The packages to include for build and their build options.
packages: $ReadOnly<{[packageName: string]: BuildOptions}>,
}>;
*/
/**
* - BUILD CONFIG -
*
* Add packages here to configure them as part of the monorepo `yarn build`
* setup. These must use a consistent package structure and (today) target
* Node.js packages only.
*/
const buildConfig /*: BuildConfig */ = {
packages: {
'community-cli-plugin': {
target: 'node',
},
'dev-middleware': {
target: 'node',
emitTypeScriptDefs: true,
},
'metro-config': {
target: 'node',
emitTypeScriptDefs: true,
},
},
};
const defaultBuildOptions = {
emitFlowDefs: true,
emitTypeScriptDefs: false,
};
function getBuildOptions(
packageName /*: $Keys<BuildConfig['packages']> */,
) /*: Required<BuildOptions> */ {
return {
...defaultBuildOptions,
...buildConfig.packages[packageName],
};
}
function getBabelConfig(
packageName /*: $Keys<BuildConfig['packages']> */,
) /*: BabelCoreOptions */ {
const {target} = getBuildOptions(packageName);
switch (target) {
case 'node':
return require('./babel/node.config.js');
}
}
function getTypeScriptCompilerOptions(
packageName /*: $Keys<BuildConfig['packages']> */,
) /*: Object */ {
const {target} = getBuildOptions(packageName);
switch (target) {
case 'node':
return {
...require('@tsconfig/node18/tsconfig.json').compilerOptions,
moduleResolution: ModuleResolutionKind.NodeJs,
};
}
}
module.exports = {
buildConfig,
getBabelConfig,
getBuildOptions,
getTypeScriptCompilerOptions,
};