forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auto-generation of TypeScript definitions on build (facebook#38990)
Summary: Pull Request resolved: facebook#38990 This PR adds auto-generation of Typescript definitions from Flow source code for packages using the shared monorepo build setup (facebook#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: 5ac8f3b054c313407c37dca060e0c5e77d112e9a
- Loading branch information
1 parent
e44fdfe
commit cff7564
Showing
8 changed files
with
339 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
declare module '@tsconfig/node18/tsconfig.json' { | ||
declare module.exports: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
declare module 'typescript' { | ||
declare enum ModuleResolutionKind { | ||
Classic = 'Classic', | ||
NodeJs = 'NodeJs', | ||
Node10 = 'Node10', | ||
Node16 = 'Node16', | ||
NodeNext = 'NodeNext', | ||
Bundler = 'Bundler', | ||
} | ||
|
||
declare type SourceFile = $ReadOnly<{ | ||
fileName: string, | ||
text: string, | ||
... | ||
}>; | ||
|
||
declare type Diagnostic = $ReadOnly<{ | ||
file?: SourceFile, | ||
start?: number, | ||
messageText: string, | ||
... | ||
}>; | ||
|
||
declare type EmitResult = $ReadOnly<{ | ||
diagnostics: Array<Diagnostic>, | ||
... | ||
}>; | ||
|
||
declare type Program = $ReadOnly<{ | ||
emit: () => EmitResult, | ||
... | ||
}>; | ||
|
||
declare type TypeScriptAPI = { | ||
createProgram(files: Array<string>, compilerOptions: Object): Program, | ||
flattenDiagnosticMessageText: (...messageText: Array<string>) => string, | ||
getLineAndCharacterOfPosition( | ||
file: SourceFile, | ||
start?: number, | ||
): $ReadOnly<{line: number, character: number}>, | ||
ModuleResolutionKind: typeof ModuleResolutionKind, | ||
... | ||
}; | ||
|
||
declare module.exports: TypeScriptAPI; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* 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 TARGET_NODE_VERSION = '18'; | ||
|
||
const config /*: BabelCoreOptions */ = { | ||
presets: [ | ||
'@babel/preset-flow', | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
targets: { | ||
node: TARGET_NODE_VERSION, | ||
}, | ||
}, | ||
], | ||
], | ||
plugins: [ | ||
[ | ||
'transform-define', | ||
{ | ||
'process.env.BUILD_EXCLUDE_BABEL_REGISTER': true, | ||
}, | ||
], | ||
[ | ||
'minify-dead-code-elimination', | ||
{keepFnName: true, keepFnArgs: true, keepClassName: true}, | ||
], | ||
], | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.