Skip to content

Commit 9641f96

Browse files
doc(correct-ts-specifiers): put usage examples first in readme (#78)
1 parent 48013da commit 9641f96

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

recipes/correct-ts-specifiers/README.md

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,6 @@ This package transforms import specifiers from the old `tsc` (TypeScript's compi
44

55
This is a one-and-done process, and the updated source-code should be committed to your version control (eg git); thereafter, source-code import statements should be authored compliant with the ECMAScript (JavaScript) standard.
66

7-
> [!TIP]
8-
> Those using `tsc` to compile will need to enable [`rewriteRelativeImportExtensions`](https://www.typescriptlang.org/tsconfig/#rewriteRelativeImportExtensions); using `tsc` for only type-checking (ex via a lint/test step like `npm run test:types`) needs [`allowImportingTsExtensions`](https://www.typescriptlang.org/tsconfig/#allowImportingTsExtensions) (and some additional compile options—see the cited documentation);
9-
10-
This package does not just blindly find & replace file extensions within specifiers: It confirms that the replacement specifier actually exists; in ambiguous cases (such as two files with the same basename in the same location but different relevant file extensions like `/tmp/foo.js` and `/tmp/foo.ts`), it logs an error, skips that specifier, and continues processing.
11-
12-
> [!CAUTION]
13-
> This package does not confirm that imported modules contain the desired export(s). This _shouldn't_ actually ever result in a problem because ambiguous cases are skipped (so if there is a problem, it existed before the migration started). Merely running your source-code after the mirgration completes will confirm all is well (if there are problems, node will error, citing the problems).
14-
15-
> [!TIP]
16-
> Node.js requires the `type` keyword be present on type imports. For own code, this package usually handles that. However, in some cases and for node modules, it does not. Robust tooling already exists that will automatically fix this, such as
17-
>
18-
> * [`use-import-type` via biome](https://biomejs.dev/linter/rules/use-import-type/)
19-
> * [`typescript/no-import-type-side-effects` via oxlint](https://oxc.rs/docs/guide/usage/linter/rules/typescript/no-import-type-side-effects)
20-
> * [`consistent-type-imports` via typescript-lint](https://typescript-eslint.io/rules/consistent-type-imports)
21-
>
22-
> If your source code needs that, first run this codemod and then one of those fixers.
23-
24-
## Running
25-
26-
> [!CAUTION]
27-
> This will change your source-code. Commit any unsaved changes before running this package.
28-
29-
> [!IMPORTANT]
30-
> [`--experimental-import-meta-resolve`](https://nodejs.org/api/cli.html#--experimental-import-meta-resolve) MUST be enabled; the feature is not really experimental—it's nonstandard because it's not relevant for browsers.
31-
32-
```console
33-
$ NODE_OPTIONS="--experimental-import-meta-resolve" \
34-
npx codemod@latest @nodejs/correct-ts-specifiers
35-
```
36-
37-
### Monorepos
38-
39-
For best results, run this _within_ each workspace of the monorepo.
40-
41-
```text
42-
project-root/
43-
├ workspaces/
44-
├ foo/ ←--------- RUN HERE
45-
├ …
46-
├ package.json
47-
└ tsconfig.json
48-
└ bar/ ←--------- RUN HERE
49-
├ …
50-
├ package.json
51-
└ tsconfig.json
52-
└ utils/ ←--------- RUN HERE
53-
├ qux.js
54-
└ zed.js
55-
```
56-
577
## Supported cases
588

599
* no file extension → `.cts`, `.mts`, `.js`, `.ts`, `.d.cts`, `.d.mts`, or `.d.ts`
@@ -111,3 +61,53 @@ const bird = new Bird('Tweety');
11161
const cat = new Cat('Milo');
11262
const dog = new Dog('Otis');
11363
```
64+
65+
> [!TIP]
66+
> Those using `tsc` to compile will need to enable [`rewriteRelativeImportExtensions`](https://www.typescriptlang.org/tsconfig/#rewriteRelativeImportExtensions); using `tsc` for only type-checking (ex via a lint/test step like `npm run test:types`) needs [`allowImportingTsExtensions`](https://www.typescriptlang.org/tsconfig/#allowImportingTsExtensions) (and some additional compile options—see the cited documentation);
67+
68+
This package does not just blindly find & replace file extensions within specifiers: It confirms that the replacement specifier actually exists; in ambiguous cases (such as two files with the same basename in the same location but different relevant file extensions like `/tmp/foo.js` and `/tmp/foo.ts`), it logs an error, skips that specifier, and continues processing.
69+
70+
> [!CAUTION]
71+
> This package does not confirm that imported modules contain the desired export(s). This _shouldn't_ actually ever result in a problem because ambiguous cases are skipped (so if there is a problem, it existed before the migration started). Merely running your source-code after the mirgration completes will confirm all is well (if there are problems, node will error, citing the problems).
72+
73+
> [!TIP]
74+
> Node.js requires the `type` keyword be present on type imports. For own code, this package usually handles that. However, in some cases and for node modules, it does not. Robust tooling already exists that will automatically fix this, such as
75+
>
76+
> * [`use-import-type` via biome](https://biomejs.dev/linter/rules/use-import-type/)
77+
> * [`typescript/no-import-type-side-effects` via oxlint](https://oxc.rs/docs/guide/usage/linter/rules/typescript/no-import-type-side-effects)
78+
> * [`consistent-type-imports` via typescript-lint](https://typescript-eslint.io/rules/consistent-type-imports)
79+
>
80+
> If your source code needs that, first run this codemod and then one of those fixers.
81+
82+
## Running
83+
84+
> [!CAUTION]
85+
> This will change your source-code. Commit any unsaved changes before running this package.
86+
87+
> [!IMPORTANT]
88+
> [`--experimental-import-meta-resolve`](https://nodejs.org/api/cli.html#--experimental-import-meta-resolve) MUST be enabled; the feature is not really experimental—it's nonstandard because it's not relevant for browsers.
89+
90+
```console
91+
$ NODE_OPTIONS="--experimental-import-meta-resolve" \
92+
npx codemod@latest @nodejs/correct-ts-specifiers
93+
```
94+
95+
### Monorepos
96+
97+
For best results, run this _within_ each workspace of the monorepo.
98+
99+
```text
100+
project-root/
101+
├ workspaces/
102+
├ foo/ ←--------- RUN HERE
103+
├ …
104+
├ package.json
105+
└ tsconfig.json
106+
└ bar/ ←--------- RUN HERE
107+
├ …
108+
├ package.json
109+
└ tsconfig.json
110+
└ utils/ ←--------- RUN HERE
111+
├ qux.js
112+
└ zed.js
113+
```

0 commit comments

Comments
 (0)