@activepipe/flowgen 中文文档教程
Flowgen
The state of the converter
就目前而言,它非常健壮且无损,这在很大程度上要归功于流程和打字稿定义文件的相似程度。 请查看this flow-typed PR中的输出以了解输出状态。
Usage
使用 npm i flowgen --save
安装
import { compiler } from 'flowgen';
// To compile a d.ts file
const flowdef = compiler.compileDefinitionFile(filename);
// To compile a string
const flowdef = compiler.compileDefinitionString(str);
// To compile a typescript test file to JavaScript
// esTarget = ES5/ES6 etc
const testCase = compiler.compileTest(path, esTarget)
推荐的第二步:
import { beautify } from 'flowgen';
// Make the definition human readable
const readableDef = beautify(generatedFlowdef);
CLI
标准用法(将生成 export.flow.js
):
npm i -g flowgen
flowgen lodash.d.ts
Options
-o / --output-file [outputFile]: Specifies the filename of the exported file, defaults to export.flow.js
Flags for specific cases
--flow-typed-format: Format output so it fits in the flow-typed repo
--compile-tests: Compile any sibling <filename>-tests.ts files found
The difficult parts
Namespaces
命名空间一直是一个很大的问题头痛。 它现在所做的是将任何名称空间拆分为带前缀的全局范围声明。 它工作正常,但它不漂亮并且有一些缺点。
External library imports
TS 和 Flow 中的定义往往大不相同,从其他库导入的类型通常没有 一对一的映射。 常见的情况有React.ReactElement
、React.CSSProps
等。 这可能需要手动处理,或者我们添加一组处理常见情况的硬编码突变。
Odd TS conventions
Lodash 一直是我使用过的参考库之一创造 转换器。 该定义主要只是一系列具有相同名称的接口,为每个功能一遍又一遍地重新声明,根本不会转换为流。 有多种方法可以解决这个问题,但我还没有一个很好的解决方案。
Contributing
感谢所有帮助。 请发推文给我如果您需要一些入门帮助,或者只是想讨论如何解决棘手部分的想法。
Distribution
git pull origin master
yarn compile
- Change the version in
package.json
git add .
- `git commit -m "New release"
npm publish
git push
Flowgen
The state of the converter
It's surprisingly robust and non-lossy as it stands right now, in big part thanks to how similar flow and typescript definition files are. Please see the output in this flow-typed PR for the state of the output.
Usage
Install using npm i flowgen --save
import { compiler } from 'flowgen';
// To compile a d.ts file
const flowdef = compiler.compileDefinitionFile(filename);
// To compile a string
const flowdef = compiler.compileDefinitionString(str);
// To compile a typescript test file to JavaScript
// esTarget = ES5/ES6 etc
const testCase = compiler.compileTest(path, esTarget)
Recommended second step:
import { beautify } from 'flowgen';
// Make the definition human readable
const readableDef = beautify(generatedFlowdef);
CLI
Standard usage (will produce export.flow.js
):
npm i -g flowgen
flowgen lodash.d.ts
Options
-o / --output-file [outputFile]: Specifies the filename of the exported file, defaults to export.flow.js
Flags for specific cases
--flow-typed-format: Format output so it fits in the flow-typed repo
--compile-tests: Compile any sibling <filename>-tests.ts files found
The difficult parts
Namespaces
Namespaces have been a big headache. What it does right now is that it splits any namespace out into prefixed global scope declarations instead. It works OK, but its not pretty and there's some drawbacks to it.
External library imports
Definitions in TS and flow are often quite different, and imported types from other libraries dont usually have a one-to-one mapping. Common cases are React.ReactElement
, React.CSSProps
etc. This might require manual processing, or we add a set of hardcoded mutations that handle common cases.
Odd TS conventions
Lodash has been one of the reference libraries i've worked with when creating the converter. The definition is mostly just a series of interfaces with the same name being re-declared over and over again for each function, which doesn't translate to flow at all. There's multiple ways of solving this but I dont have a great solution for it in place yet.
Contributing
All help is appreciated. Please tweet at me if you want some help getting started, or just want to discuss ideas on how to solve the trickier parts.
Distribution
git pull origin master
yarn compile
- Change the version in
package.json
git add .
- `git commit -m "New release"
npm publish
git push