mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
a978d343a6
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38990 This PR adds auto-generation of Typescript definitions from Flow source code for packages using the shared monorepo build setup (https://github.com/facebook/react-native/pull/38718). Today, these are the following Node.js packages: - `packages/community-cli-plugin` - `packages/dev-middleware` (⬅️ `emitTypeScriptDefs` enabled) This also improves emitted Flow definitions (`.js.flow`), by using [`flow-api-translator`](https://www.npmjs.com/package/flow-api-translator) to strip implementations. **All changes** - Include `flow-api-translator` and configure this to emit type definitions as part of `yarn build`. - Add translation from Flow source to TypeScript definitions (`.d.ts`) adjacent to each built file. - Improve emitted Flow definitions (`.js.flow`), by using `flow-api-translator` to strip implementations (previously, source files were copied). The Flow and TS defs now mirror each other. - Add `emitFlowDefs` and `emitTypeScriptDefs` options to build config to configure the above. - Integrate TypeScript compiler to perform program validation on emitted `.d.ts` files. - This is based on this guide: https://github.com/microsoft/TypeScript-wiki/blob/main/Using-the-Compiler-API.md#a-minimal-compiler. - Throw an exception on the `rewritePackageExports` step if a package does not define an `"exports"` field. - Add minimal `flow-typed` definitions for `typescript` 😄. **Notes on [`flow-api-translator`](https://www.npmjs.com/package/flow-api-translator)** This project is experimental but is in a more mature state than when we evaluated it earlier in 2023. - It's now possible to run this tool on our new Node.js packages, since they are exclusively authored using `import`/`export` syntax (a requirement of the tool). - As a safety net, we run the TypeScript compiler against the generated program, which will fail the build. Changelog: [Internal] Reviewed By: robhogan Differential Revision: D48312463 fbshipit-source-id: 817edb35f911f52fa987946f2d8fc1a319078c9d
100 lines
2.2 KiB
JavaScript
100 lines
2.2 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,
|
|
},
|
|
},
|
|
};
|
|
|
|
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,
|
|
};
|