Skip to content

Commit 21de04f

Browse files
committed
fix: correct hook path, add dedup guards, Node 20.6 guard (#553)
- vitest.config.js: point to ts-resolver-hook.js (not ts-resolver-loader.js) - vitest.config.js: dedup --strip-types and --import flags - ts-resolver-hook.js: guard module.register() for Node >= 20.6
1 parent d6366de commit 21de04f

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

scripts/ts-resolver-hook.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* to install a resolve hook that falls back to .ts when .js is missing.
66
*/
77

8-
import { register } from 'node:module';
9-
10-
register('./ts-resolver-loader.js', import.meta.url);
8+
// module.register() requires Node >= 20.6.0
9+
const [_major, _minor] = process.versions.node.split('.').map(Number);
10+
if (_major > 20 || (_major === 20 && _minor >= 6)) {
11+
const { register } = await import('node:module');
12+
register('./ts-resolver-loader.js', import.meta.url);
13+
}

vitest.config.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
44
import { defineConfig } from 'vitest/config';
55

66
const __dirname = dirname(fileURLToPath(import.meta.url));
7-
const loaderPath = pathToFileURL(resolve(__dirname, 'scripts/ts-resolver-loader.js')).href;
7+
const hookPath = pathToFileURL(resolve(__dirname, 'scripts/ts-resolver-hook.js')).href;
88
const [major, minor] = process.versions.node.split('.').map(Number);
99
const supportsStripTypes = major > 22 || (major === 22 && minor >= 6);
10+
const existing = process.env.NODE_OPTIONS || '';
1011

1112
/**
1213
* During the JS → TS migration, some .js files import from modules that have
@@ -42,9 +43,13 @@ export default defineConfig({
4243
exclude: ['**/node_modules/**', '**/.git/**', '.claude/**'],
4344
env: {
4445
NODE_OPTIONS: [
45-
process.env.NODE_OPTIONS,
46-
supportsStripTypes ? (major >= 23 ? '--strip-types' : '--experimental-strip-types') : '',
47-
`--import ${loaderPath}`,
46+
existing,
47+
supportsStripTypes &&
48+
!existing.includes('--experimental-strip-types') &&
49+
!existing.includes('--strip-types')
50+
? (major >= 23 ? '--strip-types' : '--experimental-strip-types')
51+
: '',
52+
existing.includes(hookPath) ? '' : `--import ${hookPath}`,
4853
].filter(Boolean).join(' '),
4954
},
5055
},

0 commit comments

Comments
 (0)