TypeScriptプロジェクト
TypeScriptプロジェクトジェネレータは、ECMAScript Modules (ESM)、TypeScriptのプロジェクト参照、テスト実行用のVitest、静的解析用のESLintなど、ベストプラクティスで構成されたモダンなTypeScriptライブラリまたはアプリケーションを作成するために使用できます。
TypeScriptプロジェクトの生成
Section titled “TypeScriptプロジェクトの生成”新しいTypeScriptプロジェクトを2つの方法で生成できます:
- インストール Nx Console VSCode Plugin まだインストールしていない場合
- VSCodeでNxコンソールを開く
- クリック
Generate (UI)"Common Nx Commands"セクションで - 検索
@aws/nx-plugin - ts#project - 必須パラメータを入力
- クリック
Generate
pnpm nx g @aws/nx-plugin:ts#projectyarn nx g @aws/nx-plugin:ts#projectnpx nx g @aws/nx-plugin:ts#projectbunx nx g @aws/nx-plugin:ts#project| パラメータ | 型 | デフォルト | 説明 |
|---|---|---|---|
| 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. |
ジェネレータの出力
Section titled “ジェネレータの出力”ジェネレータは<directory>/<name>ディレクトリに以下のプロジェクト構造を作成します:
Directorysrc 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ソースコードの記述
Section titled “TypeScriptソースコードの記述”TypeScriptコードはsrcディレクトリに追加します。
ESMインポート構文
Section titled “ESMインポート構文”TypeScriptプロジェクトはESモジュールであるため、インポート文ではファイル拡張子を明示的に指定する必要があります:
import { sayHello } from './hello.js';他のTypeScriptプロジェクト向けのエクスポート
Section titled “他のTypeScriptプロジェクト向けのエクスポート”TypeScriptプロジェクトのエントリポイントはsrc/index.tsです。他のプロジェクトからインポート可能にする要素はここでエクスポートできます:
export { sayHello } from './hello.js';export * from './algorithms/index.js';他のプロジェクトでのライブラリコードのインポート
Section titled “他のプロジェクトでのライブラリコードのインポート”ワークスペースのtsconfig.base.jsonで設定されたTypeScriptエイリアスを使用して、他のTypeScriptプロジェクトからプロジェクトを参照できます:
import { sayHello } from ':my-scope/my-library';ワークスペース内の新しいプロジェクトを初めてインポートする場合、以下のようなエラーがIDEに表示される可能性があります:
インポートエラー
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 Syncジェネレータで事前設定されているため、手動での設定は不要です。以下のコマンドを実行するとNxが必要な設定を追加します:
pnpm nx syncyarn nx syncnpx nx syncbunx nx syncこれによりIDEのエラーが解消され、ライブラリを使用できるようになります。
TypeScriptプロジェクトにpackage.jsonファイルが存在しないことに気付くかもしれません。これは従来のTypeScriptモノレポに慣れている場合、予期しないことでしょう。
モノレポ内のTypeScriptパッケージの依存関係を追加するには、ワークスペースルートのpackage.jsonに依存関係を追加します。パッケージマネージャのコマンドラインから追加できます:
pnpm add -w some-npm-packageyarn add some-npm-packagenpm install --legacy-peer-deps some-npm-packagebun install some-npm-packageこれにより、ワークスペース内のすべてのTypeScriptプロジェクトで依存関係を使用できるようになります。
ランタイムコード
Section titled “ランタイムコード”TypeScriptプロジェクトをランタイムコード(例:AWS Lambda関数のハンドラ)として使用する場合、Rolldownなどのツールを使用してプロジェクトをバンドルすることを推奨します。これにより、実際に参照されている依存関係のみが含まれるようツリーシェイキングされます。
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ファイルは以下のように追加します:
import { defineConfig } from 'rolldown';
export default defineConfig([ { input: 'src/index.ts', output: { file: '../../dist/packages/my-library/bundle/index.js', format: 'cjs', inlineDynamicImports: true, }, },]);NPMへの公開
Section titled “NPMへの公開”TypeScriptプロジェクトをNPMに公開する場合は、package.jsonファイルを作成する必要があります。
このファイルにはプロジェクトが参照する依存関係を宣言する必要があります。ビルド時にはワークスペースルートのpackage.json経由でインストールされた依存関係が解決されるため、Nx Dependency Checks ESLint Pluginを設定して、公開用のpackage.jsonにプロジェクトで使用するすべての依存関係が含まれるようにすることを推奨します。
TypeScriptプロジェクトにはbuildターゲット(project.jsonで定義)が設定されており、以下のコマンドで実行できます:
pnpm nx run <project-name>:buildyarn nx run <project-name>:buildnpx nx run <project-name>:buildbunx nx run <project-name>:build<project-name>はプロジェクトの完全修飾名です。
buildターゲットはプロジェクトのコンパイル、リンティング、テストを実行します。
ビルド出力はワークスペースルートのdistフォルダ内のパッケージディレクトリ(例: dist/packages/<my-library>/tsc)に生成されます。
Vitestがプロジェクトのテスト用に設定されています。
テストの記述
Section titled “テストの記述”テストは.spec.tsまたは.test.tsファイルに記述し、プロジェクトのsrcフォルダに配置します。
例:
Directorysrc
- hello.ts ライブラリソースコード
- hello.spec.ts hello.tsのテスト
Vitestはdescribe、it、test、expectなどのJest風の構文を提供します。
import { sayHello } from './hello.js';
describe('sayHello', () => {
it('should greet the caller', () => { expect(sayHello('Darth Vader')).toBe('Hello, Darth Vader!'); });
});テストの記述方法や依存関係のモック作成などの詳細については、Vitestドキュメントを参照してください。
テストの実行
Section titled “テストの実行”テストはプロジェクトのbuildターゲットの一部として実行されますが、testターゲットを個別に実行することもできます:
pnpm nx run <project-name>:testyarn nx run <project-name>:testnpx nx run <project-name>:testbunx nx run <project-name>:test-tフラグを使用して特定のテストを実行できます:
pnpm nx run <project-name>:test -t 'sayHello'yarn nx run <project-name>:test -t 'sayHello'npx nx run <project-name>:test -t 'sayHello'bunx nx run <project-name>:test -t 'sayHello'リンティング
Section titled “リンティング”TypeScriptプロジェクトでは、リンティングにESLint、フォーマットにPrettierを使用します。
ワークスペースルートのeslint.config.mjsファイルでESLintを構成することを推奨します。これにより、変更がワークスペース内のすべてのTypeScriptプロジェクトに適用され、一貫性が保たれます。
同様に、Prettierはルートの.prettierrcファイルで設定できます。
リンターの実行
Section titled “リンターの実行”プロジェクトのリンターをチェックするにはlintターゲットを実行します:
pnpm nx run <project-name>:lintyarn nx run <project-name>:lintnpx nx run <project-name>:lintbunx nx run <project-name>:lintリント問題の修正
Section titled “リント問題の修正”ほとんどのリンティングやフォーマットの問題は自動修正可能です。--configuration=fix引数を付けてESLintを実行することで修正できます:
pnpm nx run <project-name>:lint --configuration=fixyarn nx run <project-name>:lint --configuration=fixnpx nx run <project-name>:lint --configuration=fixbunx nx run <project-name>:lint --configuration=fixワークスペース内のすべてのパッケージのリント問題を修正するには:
pnpm nx run-many --target lint --all --configuration=fixyarn nx run-many --target lint --all --configuration=fixnpx nx run-many --target lint --all --configuration=fixbunx nx run-many --target lint --all --configuration=fix