使用 npm 包导出我自己的 Flow 类型?
Exporting my own Flow type with npm package?
我有一个使用流进行类型检查的 React 组件的 npm 包。
如果我的组件的用户能够访问我的流类型,那将很有用。但是目前我正在使用 Babel 编译我的代码,它去除了所有类型信息。
我的项目结构如下:
|
|- flowdecls
myTypes.js
| -components
- Component1
Component1.js
| - lib
- Component1.js (compiled using Babel)
- Component1.js.flow (created using flow-copy-source)
例如我在 myTypes.js
中的一个类型是
declare type DataItemIconType = {
iconElement: React$Element<React$ElementType>,
color?: string,
hoverColor?: string
}
我会在 Component1
中使用它。例如 Component1
的道具之一是
iconList : Array<DataItemIconType>
我已经发布了几个版本的 React 组件库作为一个没有 Flow 的 npm 包,我的组件被广泛使用。不过我很想提供流量支持。
最近,我尝试使用本文中指定的 flow-copy-source (Authoring and publishing JavaScript modules with Flow),但我图书馆的用户仍然无法访问我的类型。
我如何才能使 DataItemIconType
之类的类型可供在我的图书馆中使用 Component1
的人使用?
package.json
{
"name": "@company/react-common-components-build-template",
"version": "0.6.0",
"main": "./lib/index.js",
"private": true,
"engines": {
"node": ">=4.0.0"
},
"files": [
"lib"
],
"description": "Common component library",
"peerDependencies": {
"react": "16.10.0",
"react-dom": "16.10.0",
"prop-types": "15.7.2"
},
"dependencies": {
"@material-ui/core": "4.9.5",
"@material-ui/icons": "4.9.1",
"@material-ui/lab": "3.0.0-alpha.30",
"@material-ui/styles": "4.9.0",
"lodash": "4.17.15"
},
"devDependencies": {
"@babel/cli": "7.4.4",
"@babel/core": "7.6.0",
"@babel/node": "7.2.2",
"@babel/plugin-proposal-class-properties": "7.2.1",
"@babel/plugin-syntax-dynamic-import": "7.2.0",
"@babel/plugin-transform-object-assign": "7.2.0",
"@babel/plugin-transform-react-constant-elements": "7.6.0",
"@babel/plugin-transform-runtime": "7.6.2",
"@babel/preset-env": "7.4.2",
"@babel/preset-flow": "7.0.0",
"@babel/preset-react": "7.0.0",
"@babel/register": "7.0.0",
"@svgr/webpack": "4.3.2",
"@typescript-eslint/eslint-plugin": "^2.2.0",
"@typescript-eslint/parser": "^2.2.0",
"babel-eslint": "10.0.3",
"babel-jest": "24.9.0",
"babel-loader": "8.0.6",
"babel-plugin-named-asset-import": "0.3.4",
"babel-plugin-react-remove-properties": "0.3.0",
"babel-preset-react-app": "9.0.2",
"camelcase": "^5.2.0",
"case-sensitive-paths-webpack-plugin": "2.2.0",
"chokidar": "1.6.1",
"classnames": "2.2.6",
"cpx": "1.5.0",
"cross-env": "6.0.3",
"css-loader": "2.1.1",
"dotenv": "6.2.0",
"dotenv-expand": "5.1.0",
"enzyme": "3.10.0",
"enzyme-adapter-react-16": "1.15.1",
"enzyme-to-json": "3.4.3",
"eslint": "6.6.0",
"eslint-config-react-app": "5.0.2",
"eslint-loader": "3.0.0",
"eslint-plugin-flowtype": "3.13.0",
"eslint-plugin-flowtype-errors": "4.1.0",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-react": "7.16.0",
"eslint-plugin-react-hooks": "^2.3.0",
"file-loader": "3.0.1",
"flow-bin": "0.113.0",
"flow-copy-source": "^2.0.9",
"flow-typed": "^2.6.2",
"fs-extra": "7.0.1",
"glob-gitignore": "1.0.14",
"hard-source-webpack-plugin": "^0.13.1",
"highlight": "^0.2.4",
"highlight.js": "^9.10.0",
"html-webpack-plugin": "4.0.0-beta.5",
"husky": "3.0.8",
"identity-obj-proxy": "3.0.0",
"is-wsl": "^1.1.0",
"jest": "24.9.0",
"jest-environment-jsdom-fourteen": "1.0.1",
"jest-enzyme": "^7.1.2",
"jest-resolve": "24.9.0",
"jest-watch-typeahead": "0.4.0",
"mini-css-extract-plugin": "0.9.0",
"npm-run-all": "4.0.2",
"optimize-css-assets-webpack-plugin": "5.0.3",
"pnp-webpack-plugin": "1.5.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-normalize": "7.0.1",
"postcss-preset-env": "6.7.0",
"postcss-safe-parser": "4.0.1",
"prettier": "1.19.1",
"react": "16.10.0",
"react-addons-test-utils": "15.5.1",
"react-app-polyfill": "^1.0.3",
"react-dev-utils": "10.2.0",
"react-docgen": "3.0.0",
"react-dom": "16.10.0",
"react-highlight": "^0.12.0",
"react-test-renderer": "16.10.0",
"resolve": "1.15.0",
"resolve-url-loader": "3.1.1",
"sass-loader": "8.0.2",
"semver": "6.3.0",
"style-loader": "1.0.0",
"terser-webpack-plugin": "2.3.4",
"ts-pnp": "1.1.5",
"url-loader": "2.3.0",
"webpack": "4.41.5",
"webpack-dev-server": "3.10.2",
"webpack-manifest-plugin": "2.2.0",
"workbox-webpack-plugin": "4.3.1"
},
"scripts": {
"prestart": "npm run gen:docs",
"start": "npm-run-all --parallel start:docs gen:docs-watch",
"start:docs": "node scripts/start.js",
"gen:docs": "node scripts/generateComponentData.js",
"gen:docs-watch": "npm run gen:docs -- --watch",
"build:docs": "node scripts/build.js",
"test": "node scripts/test.js",
"predeploy:docs": "npm run build:docs",
"flow": "flow",
"lint": "eslint src --debug",
"lint:flow-typed": "flow-typed install --ignoreDeps dev",
"build:images": "cpx \"./src/components/images/**/*.*\" ./lib/images",
"prebuild:common-components-lib": "rimraf lib",
"build:common-components-lib": "npm-run-all --parallel build:components build:utils build:images build:copy-files build:copyflowsource",
"build:components": "cross-env NODE_ENV=production BABEL_ENV=cjs babel ./src/components --out-dir ./lib/ --ignore spec.js",
"build:utils": "cross-env NODE_ENV=production BABEL_ENV=cjs babel src/components/utils --out-dir ./lib/utils --ignore spec.js",
"build:copyflowsource": "flow-copy-source ./src/components ./lib ",
"build:copy-files": "node scripts/copyBuildFiles.js",
"prettier:changed": "node ./scripts/prettier.js",
"prettier:all": "node ./scripts/prettier.js write",
"format-check": "prettier --check \"./src/**/*.{js,test.js,spec.js}\""
},
"publishConfig": {
"registry": "http://srv-ie-nexus/repository/npm-hosted/"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"jest": {
"roots": [
"<rootDir>/src/components"
],
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
],
"setupFiles": [
"react-app-polyfill/jsdom"
],
"setupFilesAfterEnv": [
"<rootDir>/jest-test-setup.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
],
"testEnvironment": "jest-environment-jsdom-fourteen",
"transform": {
"^.+\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
"^.+\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\]node_modules[/\\].+\.(js|jsx|ts|tsx)$",
"^.+\.module\.(css|sass|scss)$"
],
"modulePaths": [],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\.module\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
],
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
]
}
}
我认为 Flow 还没有解决这个问题的方法。但我和你在同一条船上。
您的消费者无法使用您的包类型的大部分原因是因为他们从他们的 .*/flow-typed/*
中读取全局类型而不是您的类型,当他们安装您的包时将在 .*/node_modules/@company/react-common-components-build-template
.它与任何 package.json 设置无关。
尽管您有两种选择来运送您的包裹类型。
选项 1:
要么将它们作为 src
代码的一部分并允许它们导入它,这意味着您的类型将不再是全局的,您需要导入要在源代码中使用的类型代码。但这意味着你们消费者也可以。
选项 2:
或者将它们保存在您的 flowdecls
中并确保它们已发布。一旦完成,您的用户将不得不将您的类型定义复制到他们的 flow-typed
目录中,这现在也会使它们在他们的项目中成为全局文件。
您可以在您的 README 中添加类似以下的内容,以便为用户提供一些见解:
react-common-components-build-template
也在内部使用全局类型定义。如果您想利用这一点,您可以将 /node_modules/@company/react-common-components-build-template/flowdecls/myTypes.js
复制到您本地的流类型目录。每次版本升级都复制一次可能是个好主意,这样您就可以通过 npm 脚本与最新的更改保持同步。
"scripts": {
// ...
"postinstall": "cp /node_modules/@company/react-common-components-build-template/flowdecls/myTypes.js flow-typed"
},
第二个选项实际上是我的选择,上面是我的回购协议中的内容。作为旁注,我建议您将目录重命名为 flow-typed
并将类型文件重命名为 react-common-components-build-template.js
之类的名称,这样当复制过程发生时,人们就不会想知道 myTypes.js
是用来。
作为此过程的一部分,这看起来也是一个非常有用的包 - https://www.npmjs.com/package/gen-flow-files
Flow 将自动查找形容词 module.js.flow
declarations files,如果存在的话。
例如,如果您有一个指向 dist/index.js
的主字段,那么将 dist/index.js.flow
添加到您的包的输出中,用户的语言服务器就会选择它。
为此你可以使用:
flow-remove-types
NPM 包将创建 lib/Component1.js
没有流类型。如果你愿意,也可以使用 Babel
flow-copy-source
NPM 包,它将 flow 类型的文件复制到带有 .flow 后缀的目标文件夹 lib/Component1.js.flow
我有一个使用流进行类型检查的 React 组件的 npm 包。
如果我的组件的用户能够访问我的流类型,那将很有用。但是目前我正在使用 Babel 编译我的代码,它去除了所有类型信息。
我的项目结构如下:
|
|- flowdecls
myTypes.js
| -components
- Component1
Component1.js
| - lib
- Component1.js (compiled using Babel)
- Component1.js.flow (created using flow-copy-source)
例如我在 myTypes.js
中的一个类型是
declare type DataItemIconType = {
iconElement: React$Element<React$ElementType>,
color?: string,
hoverColor?: string
}
我会在 Component1
中使用它。例如 Component1
的道具之一是
iconList : Array<DataItemIconType>
我已经发布了几个版本的 React 组件库作为一个没有 Flow 的 npm 包,我的组件被广泛使用。不过我很想提供流量支持。
最近,我尝试使用本文中指定的 flow-copy-source (Authoring and publishing JavaScript modules with Flow),但我图书馆的用户仍然无法访问我的类型。
我如何才能使 DataItemIconType
之类的类型可供在我的图书馆中使用 Component1
的人使用?
package.json
{
"name": "@company/react-common-components-build-template",
"version": "0.6.0",
"main": "./lib/index.js",
"private": true,
"engines": {
"node": ">=4.0.0"
},
"files": [
"lib"
],
"description": "Common component library",
"peerDependencies": {
"react": "16.10.0",
"react-dom": "16.10.0",
"prop-types": "15.7.2"
},
"dependencies": {
"@material-ui/core": "4.9.5",
"@material-ui/icons": "4.9.1",
"@material-ui/lab": "3.0.0-alpha.30",
"@material-ui/styles": "4.9.0",
"lodash": "4.17.15"
},
"devDependencies": {
"@babel/cli": "7.4.4",
"@babel/core": "7.6.0",
"@babel/node": "7.2.2",
"@babel/plugin-proposal-class-properties": "7.2.1",
"@babel/plugin-syntax-dynamic-import": "7.2.0",
"@babel/plugin-transform-object-assign": "7.2.0",
"@babel/plugin-transform-react-constant-elements": "7.6.0",
"@babel/plugin-transform-runtime": "7.6.2",
"@babel/preset-env": "7.4.2",
"@babel/preset-flow": "7.0.0",
"@babel/preset-react": "7.0.0",
"@babel/register": "7.0.0",
"@svgr/webpack": "4.3.2",
"@typescript-eslint/eslint-plugin": "^2.2.0",
"@typescript-eslint/parser": "^2.2.0",
"babel-eslint": "10.0.3",
"babel-jest": "24.9.0",
"babel-loader": "8.0.6",
"babel-plugin-named-asset-import": "0.3.4",
"babel-plugin-react-remove-properties": "0.3.0",
"babel-preset-react-app": "9.0.2",
"camelcase": "^5.2.0",
"case-sensitive-paths-webpack-plugin": "2.2.0",
"chokidar": "1.6.1",
"classnames": "2.2.6",
"cpx": "1.5.0",
"cross-env": "6.0.3",
"css-loader": "2.1.1",
"dotenv": "6.2.0",
"dotenv-expand": "5.1.0",
"enzyme": "3.10.0",
"enzyme-adapter-react-16": "1.15.1",
"enzyme-to-json": "3.4.3",
"eslint": "6.6.0",
"eslint-config-react-app": "5.0.2",
"eslint-loader": "3.0.0",
"eslint-plugin-flowtype": "3.13.0",
"eslint-plugin-flowtype-errors": "4.1.0",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-react": "7.16.0",
"eslint-plugin-react-hooks": "^2.3.0",
"file-loader": "3.0.1",
"flow-bin": "0.113.0",
"flow-copy-source": "^2.0.9",
"flow-typed": "^2.6.2",
"fs-extra": "7.0.1",
"glob-gitignore": "1.0.14",
"hard-source-webpack-plugin": "^0.13.1",
"highlight": "^0.2.4",
"highlight.js": "^9.10.0",
"html-webpack-plugin": "4.0.0-beta.5",
"husky": "3.0.8",
"identity-obj-proxy": "3.0.0",
"is-wsl": "^1.1.0",
"jest": "24.9.0",
"jest-environment-jsdom-fourteen": "1.0.1",
"jest-enzyme": "^7.1.2",
"jest-resolve": "24.9.0",
"jest-watch-typeahead": "0.4.0",
"mini-css-extract-plugin": "0.9.0",
"npm-run-all": "4.0.2",
"optimize-css-assets-webpack-plugin": "5.0.3",
"pnp-webpack-plugin": "1.5.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-normalize": "7.0.1",
"postcss-preset-env": "6.7.0",
"postcss-safe-parser": "4.0.1",
"prettier": "1.19.1",
"react": "16.10.0",
"react-addons-test-utils": "15.5.1",
"react-app-polyfill": "^1.0.3",
"react-dev-utils": "10.2.0",
"react-docgen": "3.0.0",
"react-dom": "16.10.0",
"react-highlight": "^0.12.0",
"react-test-renderer": "16.10.0",
"resolve": "1.15.0",
"resolve-url-loader": "3.1.1",
"sass-loader": "8.0.2",
"semver": "6.3.0",
"style-loader": "1.0.0",
"terser-webpack-plugin": "2.3.4",
"ts-pnp": "1.1.5",
"url-loader": "2.3.0",
"webpack": "4.41.5",
"webpack-dev-server": "3.10.2",
"webpack-manifest-plugin": "2.2.0",
"workbox-webpack-plugin": "4.3.1"
},
"scripts": {
"prestart": "npm run gen:docs",
"start": "npm-run-all --parallel start:docs gen:docs-watch",
"start:docs": "node scripts/start.js",
"gen:docs": "node scripts/generateComponentData.js",
"gen:docs-watch": "npm run gen:docs -- --watch",
"build:docs": "node scripts/build.js",
"test": "node scripts/test.js",
"predeploy:docs": "npm run build:docs",
"flow": "flow",
"lint": "eslint src --debug",
"lint:flow-typed": "flow-typed install --ignoreDeps dev",
"build:images": "cpx \"./src/components/images/**/*.*\" ./lib/images",
"prebuild:common-components-lib": "rimraf lib",
"build:common-components-lib": "npm-run-all --parallel build:components build:utils build:images build:copy-files build:copyflowsource",
"build:components": "cross-env NODE_ENV=production BABEL_ENV=cjs babel ./src/components --out-dir ./lib/ --ignore spec.js",
"build:utils": "cross-env NODE_ENV=production BABEL_ENV=cjs babel src/components/utils --out-dir ./lib/utils --ignore spec.js",
"build:copyflowsource": "flow-copy-source ./src/components ./lib ",
"build:copy-files": "node scripts/copyBuildFiles.js",
"prettier:changed": "node ./scripts/prettier.js",
"prettier:all": "node ./scripts/prettier.js write",
"format-check": "prettier --check \"./src/**/*.{js,test.js,spec.js}\""
},
"publishConfig": {
"registry": "http://srv-ie-nexus/repository/npm-hosted/"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"jest": {
"roots": [
"<rootDir>/src/components"
],
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
],
"setupFiles": [
"react-app-polyfill/jsdom"
],
"setupFilesAfterEnv": [
"<rootDir>/jest-test-setup.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
],
"testEnvironment": "jest-environment-jsdom-fourteen",
"transform": {
"^.+\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
"^.+\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\]node_modules[/\\].+\.(js|jsx|ts|tsx)$",
"^.+\.module\.(css|sass|scss)$"
],
"modulePaths": [],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\.module\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
],
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
]
}
}
我认为 Flow 还没有解决这个问题的方法。但我和你在同一条船上。
您的消费者无法使用您的包类型的大部分原因是因为他们从他们的 .*/flow-typed/*
中读取全局类型而不是您的类型,当他们安装您的包时将在 .*/node_modules/@company/react-common-components-build-template
.它与任何 package.json 设置无关。
尽管您有两种选择来运送您的包裹类型。
选项 1:
要么将它们作为 src
代码的一部分并允许它们导入它,这意味着您的类型将不再是全局的,您需要导入要在源代码中使用的类型代码。但这意味着你们消费者也可以。
选项 2:
或者将它们保存在您的 flowdecls
中并确保它们已发布。一旦完成,您的用户将不得不将您的类型定义复制到他们的 flow-typed
目录中,这现在也会使它们在他们的项目中成为全局文件。
您可以在您的 README 中添加类似以下的内容,以便为用户提供一些见解:
react-common-components-build-template
也在内部使用全局类型定义。如果您想利用这一点,您可以将 /node_modules/@company/react-common-components-build-template/flowdecls/myTypes.js
复制到您本地的流类型目录。每次版本升级都复制一次可能是个好主意,这样您就可以通过 npm 脚本与最新的更改保持同步。
"scripts": {
// ...
"postinstall": "cp /node_modules/@company/react-common-components-build-template/flowdecls/myTypes.js flow-typed"
},
第二个选项实际上是我的选择,上面是我的回购协议中的内容。作为旁注,我建议您将目录重命名为 flow-typed
并将类型文件重命名为 react-common-components-build-template.js
之类的名称,这样当复制过程发生时,人们就不会想知道 myTypes.js
是用来。
作为此过程的一部分,这看起来也是一个非常有用的包 - https://www.npmjs.com/package/gen-flow-files
Flow 将自动查找形容词 module.js.flow
declarations files,如果存在的话。
例如,如果您有一个指向 dist/index.js
的主字段,那么将 dist/index.js.flow
添加到您的包的输出中,用户的语言服务器就会选择它。
为此你可以使用:
flow-remove-types
NPM 包将创建lib/Component1.js
没有流类型。如果你愿意,也可以使用 Babelflow-copy-source
NPM 包,它将 flow 类型的文件复制到带有 .flow 后缀的目标文件夹lib/Component1.js.flow