跳转到内容

TypeScript 项目

TypeScript项目生成器可用于创建配置了最佳实践的现代化TypeScript库或应用,包括ECMAScript Modules (ESM)、TypeScript项目引用、用于运行测试的Vitest和用于静态分析的ESLint

您可以通过两种方式生成新的TypeScript项目:

  1. 安装 Nx Console VSCode Plugin 如果您尚未安装
  2. 在VSCode中打开Nx控制台
  3. 点击 Generate (UI) 在"Common Nx Commands"部分
  4. 搜索 @aws/nx-plugin - ts#project
  5. 填写必需参数
    • 点击 Generate
    参数 类型 默认值 描述
    name 必需 string - TypeScript project name
    directory string packages Parent directory where the library is placed.
    subDirectory string - The sub directory the lib is placed in. By default this is the library name.

    生成器将在<directory>/<name>目录下创建以下项目结构:

    • 文件夹src TypeScript源代码
      • index.ts
    • project.json 项目配置和构建目标
    • tsconfig.json 项目基础TypeScript配置(继承工作区根目录的tsconfig.base.json)
    • tsconfig.lib.json 库的TypeScript配置(运行时或打包源码)
    • tsconfig.spec.json 测试的TypeScript配置
    • vite.config.ts Vitest配置
    • eslint.config.mjs ESLint配置

    您还会发现工作区根目录的以下文件发生了变化:

    • nx.json Nx配置更新以配置@nx/js/typescript插件
    • tsconfig.base.json 设置TypeScript别名以便工作区其他项目导入
    • tsconfig.json 添加了项目的TypeScript项目引用

    src目录中添加TypeScript代码。

    由于TypeScript项目是ES模块,请确保使用正确的ESM语法编写导入语句,显式指定文件扩展名:

    index.ts
    import { sayHello } from './hello.js';

    项目的入口点是src/index.ts。您可以在此处添加需要被其他项目导入的导出:

    src/index.ts
    export { sayHello } from './hello.js';
    export * from './algorithms/index.js';

    工作区tsconfig.base.json中配置了TypeScript别名,允许从其他TypeScript项目引用您的项目:

    packages/my-other-project/src/index.ts
    import { sayHello } from ':my-scope/my-library';

    首次在工作区中导入新项目时,IDE可能会显示类似以下错误:

    导入错误
    Terminal window
    File '/path/to/my/workspace/packages/my-library/src/index.ts' is not under 'rootDir' '/path/to/my/workspace/packages/my-consumer'. 'rootDir' is expected to contain all source files.
    File is ECMAScript module because '/path/to/my/workspace/package.json' has field "type" with value "module" ts(6059)
    File '/path/to/my/workspace/packages/my-library/src/index.ts' is not listed within the file list of project '/path/to/my/workspace/packages/my-consumer/tsconfig.lib.json'. Projects must list all files or use an 'include' pattern.
    File is ECMAScript module because '/path/to/my/workspace/package.json' has field "type" with value "module" ts(6307)

    这是因为尚未设置项目引用

    TypeScript项目默认配置了Nx TypeScript同步生成器,您无需手动配置项目引用。只需运行以下命令,Nx将自动添加所需配置:

    Terminal window
    pnpm nx sync

    此后IDE错误应消失,即可正常使用库。

    您会注意到TypeScript项目没有package.json文件,这对习惯传统TypeScript单体仓库的用户可能有些意外。

    要为monorepo中的TypeScript包添加依赖,只需将依赖添加到工作区根目录的package.json。可通过包管理器的命令行添加:

    Terminal window
    pnpm add -w some-npm-package

    此后工作区中所有TypeScript项目均可使用该依赖。

    当将TypeScript项目作为运行时代码(例如AWS Lambda函数处理程序)时,建议使用Rolldown等工具进行打包,因其支持tree-shaking确保仅包含实际使用的依赖。

    可通过在project.json中添加如下目标实现:

    {
    ...
    "targets": {
    ...
    "bundle": {
    "cache": true,
    "executor": "nx:run-commands",
    "outputs": ["{workspaceRoot}/dist/packages/my-library/bundle"],
    "options": {
    "command": "rolldown -c rolldown.config.ts"
    }
    },
    },
    }

    并添加rolldown.config.ts文件:

    rolldown.config.ts
    import { defineConfig } from 'rolldown';
    export default defineConfig([
    {
    input: 'src/index.ts',
    output: {
    file: '../../dist/packages/my-library/bundle/index.js',
    format: 'cjs',
    inlineDynamicImports: true,
    },
    },
    ]);

    如需将TypeScript项目发布到NPM,必须创建package.json文件。

    该文件需声明项目依赖。由于构建时项目会解析工作区根目录package.json中的依赖,建议配置Nx Dependency Checks ESLint插件来确保发布的package.json包含所有使用依赖。

    项目配置了build目标(定义于project.json),可通过以下命令运行:

    Terminal window
    pnpm nx run <project-name>:build

    其中<project-name>是项目的全限定名称。

    build目标将编译、lint和测试项目。

    构建输出位于工作区根目录dist文件夹中,具体路径为dist/packages/<my-library>/tsc

    项目配置了Vitest进行测试。

    测试应写在.spec.ts.test.ts文件中,与源码共置于src目录。

    例如:

    • 文件夹src
      • hello.ts 库源码
      • hello.spec.ts 测试文件

    Vitest提供类似Jest的测试语法,支持describeittestexpect等工具。

    hello.spec.ts
    import { sayHello } from './hello.js';
    describe('sayHello', () => {
    it('should greet the caller', () => {
    expect(sayHello('Darth Vader')).toBe('Hello, Darth Vader!');
    });
    });

    更多测试编写方法和模拟依赖等特性,请参考Vitest文档

    测试将作为build目标的一部分运行,也可单独运行test目标:

    Terminal window
    pnpm nx run <project-name>:test

    可使用-t标志运行单个测试或测试套件:

    Terminal window
    pnpm nx run <project-name>:test -t 'sayHello'

    TypeScript项目使用ESLint进行代码检查,Prettier进行格式化。

    建议在工作区根目录eslint.config.mjs中配置ESLint,修改将应用于工作区所有TypeScript项目以确保一致性。

    同理,可在根目录.prettierrc中配置Prettier。

    运行lint目标进行检查:

    Terminal window
    pnpm nx run <project-name>:lint

    大多数检查或格式化问题可自动修复。通过--configuration=fix参数让ESLint自动修复:

    Terminal window
    pnpm nx run <project-name>:lint --configuration=fix

    要修复工作区所有包的lint问题,可运行:

    Terminal window
    pnpm nx run-many --target lint --all --configuration=fix