简体   繁体   English

在 Typescript React 项目和 Typescript Express 项目之间共享代码

[英]Sharing code between a Typescript React project and a Typescript Express project

I have been trying to come up with an elegant solution to share code between two projects that are closely related to each other.我一直在努力想出一个优雅的解决方案来在两个彼此密切相关的项目之间共享代码。
I have a React web app which simply includes all of the client code and I have an Express app which serves the React web app, but it also acts as an API. Because I use Typescript for both projects, I want to reuse some types.我有一个 React web 应用程序,它只包含所有客户端代码,我有一个 Express 应用程序,它为 React web 应用程序提供服务,但它也充当 API。因为我在两个项目中都使用 Typescript,所以我想重用一些类型。 To accomplish that, I created a shared project.为此,我创建了一个shared项目。

My current folder structures is as follows:我目前的文件夹结构如下:

  • web-app网络应用
  • shared共享
  • server服务器

Now I want to link the web-app and server projects with my shared project, but there are some restrictions:现在我想将web-appserver项目与我的shared项目链接起来,但有一些限制:

  1. This shared folder should not be uploaded to npm.此共享文件夹不应上传至 npm。
  2. I have to be able to still deploy everything with Heroku (which rules out npm link I believe).我必须仍然能够使用 Heroku 部署所有内容(我相信这排除了npm link )。
  3. I would prefer not to eject my React project.我不想弹出我的 React 项目。

I am not sure whether my current structure of projects allows the functionality I need, so feel free to also suggest other folder structures.我不确定我当前的项目结构是否允许我需要的功能,所以请随意建议其他文件夹结构。

If you are using create-react-app to create the web-app then you can easily use NODE_PATH environment variable to do absolute imports without creating node modules. 如果要使用create-react-app创建Web应用程序,则可以轻松使用NODE_PATH环境变量来进行绝对导入,而无需创建节点模块。

  1. Create a .env file at the root level (same level as package.json of web-app directory) 在根级别(与web-app目录的package.json相同的级别)创建一个.env文件package.json
  2. Set an environment variable, NODE_PATH to shared/ ( NODE_PATH=shared/ ) 将环境变量NODE_PATHshared/NODE_PATH=shared/

Now instead of doing something like 现在,而不是做类似的事情

import { editUser } from '../../../shared/actions';

you can use 您可以使用

import { editUser } from 'shared/actions';

Fixing ESLint 修复ESLint

Install eslint-plugin-import to prevent eslint from throwing import errors. 安装eslint-plugin-import以防止eslint引发导入错误。 And update your .eslintrc file as 并将您的.eslintrc文件更新为

{
    "settings": {
        "import/resolver": {
            "node": {
                "moduleDirectory": ["node_modules", "shared/"]
            }
        }
    }
}

Fixing Flow 定型流程

Add the following content to your .flowconfig file 将以下内容添加到您的.flowconfig文件中

[options]
module.system.node.resolve_dirname=node_modules
module.system.node.resolve_dirname=shared

You can use yarn workspaces for that kind of purpose, There's a minimal working boilerplate I have created exactly for that kind of configuration: https://github.com/rok-tel/ts-express-react您可以将 yarn workspaces 用于这种目的,我为这种配置创建了一个最小的工作样板: https://github.com/rok-tel/ts-express-react

take a look at ./packages/shared folder which is consumed both by server (express) and client (react)看一下./packages/shared文件夹,它被服务器(express)和客户端(react)消耗

Consider use Nx - https://nx.dev/考虑使用 Nx - https://nx.dev/

This is an awesome framework to manage monorepos.这是一个很棒的管理 monorepos 的框架。

  • Use their React application boilerplate for the client side project将他们的 React 应用程序样板用于客户端项目
  • Use their express project boilerplate to create the API使用他们的快递项目样板创建 API
  • Use their shared code package boilerplate to create shared code package使用他们的共享代码 package 样板创建共享代码 package

Docs for creating the shared code package:用于创建共享代码 package 的文档:

https://nx.dev/l/r/tutorial/07-share-code https://nx.dev/l/r/tutorial/07-share-code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM