Skip to content

Commit 3ea1097

Browse files
committed
Fix tests
1 parent a46625b commit 3ea1097

File tree

13 files changed

+73
-28
lines changed

13 files changed

+73
-28
lines changed

dist-raw/node-esm-resolve-implementation.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function legacyMainResolve(packageJSONUrl, packageConfig) {
292292

293293
function resolveExtensionsWithTryExactName(search) {
294294
if (fileExists(search)) return search;
295-
const resolvedReplacementExtension = getReplacementExtensionCandidates(search);
295+
const resolvedReplacementExtension = resolveReplacementExtensions(search);
296296
if(resolvedReplacementExtension) return resolvedReplacementExtension;
297297
return resolveExtensions(search);
298298
}
@@ -314,30 +314,20 @@ function resolveExtensions(search) {
314314
return undefined;
315315
}
316316

317-
function resolveReplacementExtensionsWithTryExactName(search) {
318-
if (fileExists(search)) return search;
319-
return getReplacementExtensionCandidates(search);
320-
}
321-
322317
/**
323318
* TS's resolver can resolve foo.js to foo.ts, by replacing .js extension with several source extensions.
324319
* IMPORTANT: preserve ordering according to preferTsExts; this affects resolution behavior!
325320
*/
326321
const replacementExtensions = extensions.filter(ext => ['.js', '.jsx', '.ts', '.tsx'].includes(ext));
327322

328-
function getReplacementExtensionCandidates(search) {
323+
function resolveReplacementExtensions(search) {
329324
if (search.pathname.match(/\.js$/)) {
330325
const pathnameWithoutExtension = search.pathname.slice(0, search.pathname.length - 3);
331-
return replacementExtensions.map(new URL(`${pathnameWithoutExtension}${extension}`, search));
332-
}
333-
return [search];
334-
}
335-
336-
// TODO re-merge with above function
337-
function resolveCandidates(guesses) {
338-
for (let i = 0; i < guesses.length; i++) {
339-
const guess = guesses[i];
340-
if (fileExists(guess)) return guess;
326+
for (let i = 0; i < replacementExtensions.length; i++) {
327+
const extension = replacementExtensions[i];
328+
const guess = new URL(`${pathnameWithoutExtension}${extension}`, search);
329+
if (fileExists(guess)) return guess;
330+
}
341331
}
342332
return undefined;
343333
}
@@ -361,12 +351,14 @@ function finalizeResolution(resolved, base) {
361351
}
362352

363353
if (StringPrototypeEndsWith(resolved.pathname, '/')) return resolved;
364-
const path = fileURLToPath(resolved);
365354

366-
const file = resolveCandidates(getReplacementExtensionCandidates(resolved));
367-
if (!file) {
368-
throw new ERR_MODULE_NOT_FOUND(
369-
path || resolved.pathname, fileURLToPath(base), 'module');
355+
const file = resolveReplacementExtensions(resolved) || resolved;
356+
357+
const path = fileURLToPath(file);
358+
359+
if (!tryStatSync(path).isFile()) {
360+
throw new ERR_MODULE_NOT_FOUND(
361+
path || resolved.pathname, fileURLToPath(base), 'module');
370362
}
371363

372364
return file;

src/index.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,11 +638,18 @@ describe('ts-node', function () {
638638
describe('esm', () => {
639639
this.slow(1000)
640640

641-
// `ts-node/` prefix required to import from ourselves
642-
const cmd = `node --loader ts-node/esm`
641+
const cmd = `node --loader ../../esm.mjs`
643642

644643
it('should compile and execute as ESM', (done) => {
645-
exec(`${cmd} index.ts`, {cwd: join(__dirname, '../tests/esm')}, function (err, stdout) {
644+
exec(`${cmd} index.ts`, { cwd: join(__dirname, '../tests/esm') }, function (err, stdout) {
645+
expect(err).to.equal(null)
646+
expect(stdout).to.equal('foo bar baz\n')
647+
648+
return done()
649+
})
650+
})
651+
it('supports --experimental-specifier-resolution=node', (done) => {
652+
exec(`${cmd} --experimental-specifier-resolution=node index.ts`, { cwd: join(__dirname, '../tests/esm-node-resolver') }, function (err, stdout) {
646653
expect(err).to.equal(null)
647654
expect(stdout).to.equal('foo bar baz\n')
648655

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const bar = 'bar' as const
2+
3+
if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')

tests/esm-node-resolver/baz.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const baz = 'baz'
2+
3+
if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')

tests/esm-node-resolver/biff.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const biff = 'biff'
2+
3+
const React = {
4+
createElement() {}
5+
}
6+
const div = <div></div>
7+
8+
if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')

tests/esm-node-resolver/foo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const foo = 'foo' as const
2+
3+
if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')

tests/esm-node-resolver/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {foo} from './foo'
2+
import {bar} from './bar'
3+
import {baz} from './baz'
4+
import {biff} from './biff'
5+
6+
if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')
7+
8+
console.log(`${foo} ${bar} ${baz} ${biff}`)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext",
4+
"allowJs": true,
5+
"jsx": "react",
6+
"moduleResolution": "node"
7+
}
8+
}

tests/esm/baz.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export const baz = 'baz' as const
1+
export const baz = 'baz'
22

33
if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')

0 commit comments

Comments
 (0)