🐛 Use .ts extensions in imports for node --strip-types
Node's --strip-types does not rewrite import specifiers the way bundlers and Deno do, so 'import ... from "./match.js"' literally looks for match.js (not match.ts) and fails with ERR_MODULE_NOT_FOUND at test time. Fix by switching source imports to .ts extensions and letting TypeScript's rewriteRelativeImportExtensions do the rewrite at build emit time. Changes: - tsconfig.json: enable allowImportingTsExtensions (works because the root config has noEmit: true) - tsconfig.build.json: enable rewriteRelativeImportExtensions so the emitted dist/*.js files keep './match.js' style imports (correct for consumers), not './match.ts' - src/index.ts: import './match.ts' and './pattern.ts' - src/match.ts: import './pattern.ts' - src/index.test.ts: import './index.ts' Verified: npm run test runs tsc --noEmit (passes) and node --test --strip-types src/ — all 6 tests pass. tsc -p tsconfig.build.json emits dist/*.js with './match.js' imports as before; consumers see no change.
This commit is contained in:
1 parent
77dc40da07
commit
9a242225ee
5 files changed
+7
-5
No files matched your search
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
import { test } from "node:test";
|
import { test } from "node:test";
|
||||||
import { strict as assert } from "node:assert";
|
import { strict as assert } from "node:assert";
|
||||||
import { expectTypeOf } from "expect-type";
|
import { expectTypeOf } from "expect-type";
|
||||||
import { match, P, type Matcher } from "./index.js";
|
import { match, P, type Matcher } from "./index.ts";
|
||||||
|
|
||||||
test("match returns a builder", () => {
|
test("match returns a builder", () => {
|
||||||
const builder = match("x");
|
const builder = match("x");
|
||||||
|
|||||||
+2
-2
@@ -1,2 +1,2 @@
|
|||||||
export { match, P } from "./match.js";
|
export { match, P } from "./match.ts";
|
||||||
export type { Matcher, Pattern } from "./pattern.js";
|
export type { Matcher, Pattern } from "./pattern.ts";
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
import { P, type Matcher, type Pattern } from "./pattern.js";
|
import { P, type Matcher, type Pattern } from "./pattern.ts";
|
||||||
|
|
||||||
type Cases<R> = ReadonlyArray<
|
type Cases<R> = ReadonlyArray<
|
||||||
readonly [Matcher<unknown>, (value: unknown) => R]
|
readonly [Matcher<unknown>, (value: unknown) => R]
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
"extends": "./tsconfig.json",
|
"extends": "./tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"noEmit": false,
|
"noEmit": false,
|
||||||
|
"rewriteRelativeImportExtensions": true,
|
||||||
"rootDir": "src",
|
"rootDir": "src",
|
||||||
"outDir": "dist"
|
"outDir": "dist"
|
||||||
},
|
},
|
||||||
|
|||||||
+2
-1
@@ -23,6 +23,7 @@
|
|||||||
"noUnusedParameters": true,
|
"noUnusedParameters": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"verbatimModuleSyntax": true,
|
"verbatimModuleSyntax": true,
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"declarationMap": true,
|
"declarationMap": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
@@ -34,4 +35,4 @@
|
|||||||
"types": ["node"]
|
"types": ["node"]
|
||||||
},
|
},
|
||||||
"include": ["src"]
|
"include": ["src"]
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user