Previously, tsm didn't handle the case where you could require a foo.js file that contained ESM syntax. Even though this combination is/should be invalid, tsm has to be able to rewrite the file(s) as necessary in the event there's a series or combination of tools that produce this scenario.
For example, you could be writing tests in TypeScript & those tests import third-party code that's written in ESM within .js files. When executing those tests with a require hook – for example uvu -r tsm tests – then the TypeScript would be converted into CommonJS, making require() calls to the third-party ".js" file, which still contains ESM. This would fail.
This is implemented in a way such that node -r tsm does nothing to .js files by default. It will only retry a .js file if the ERR_REQUIRE_ESM error was thrown. In fact, if any loader (regardless of extension) attempts to execute but throws the ERR_REQUIRE_ESM error, then the file is retried w/ the same options, but forcing format: cjs the second time around.
Note: If you add custom configuration for .js files, then tsm will respect that and follow your directions anyway. Your config will always execute, not just when the ERR_REQUIRE_ESM error is thrown.
Previously, tsm didn't handle the case where you could
require
afoo.js
file that contained ESM syntax. Even though this combination is/should be invalid, tsm has to be able to rewrite the file(s) as necessary in the event there's a series or combination of tools that produce this scenario.For example, you could be writing tests in TypeScript & those tests import third-party code that's written in ESM within
.js
files. When executing those tests with a require hook – for exampleuvu -r tsm tests
– then the TypeScript would be converted into CommonJS, makingrequire()
calls to the third-party".js"
file, which still contains ESM. This would fail.This is implemented in a way such that
node -r tsm
does nothing to.js
files by default. It will only retry a.js
file if theERR_REQUIRE_ESM
error was thrown. In fact, if any loader (regardless of extension) attempts to execute but throws theERR_REQUIRE_ESM
error, then the file is retried w/ the same options, but forcingformat: cjs
the second time around.Closes #7