Skip to content

Commit e29ae04

Browse files
authored
Added a transpile-only ESM loader (#1101) (#1102)
* Added a transpile-only ESM loader (#1101) * feat: Added transpile-only ESM loader to package.json exports and resolve tests * feat: Added success/failure tests for transpile-only ESM loader * fix: Fix transpile-only ESM loader tests Fixed ESM tests being executed under node 13.0.0 Fixed error message matching criteria * fix: Fix erroneous quotations in transpile-only ESM loader tests * feat: Removed imports on tests for esm-transpile-only tests * feat: Renamed and restructured esm-transpile-only tests
1 parent 08dc47d commit e29ae04

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

esm/transpile-only.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {fileURLToPath} from 'url'
2+
import {createRequire} from 'module'
3+
const require = createRequire(fileURLToPath(import.meta.url))
4+
5+
/** @type {import('../dist/esm')} */
6+
const esm = require('../dist/esm')
7+
export const {resolve, getFormat, transformSource} = esm.registerAndCreateEsmHooks({transpileOnly: true})

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"./register/transpile-only": "./register/transpile-only.js",
1919
"./register/type-check": "./register/type-check.js",
2020
"./esm": "./esm.mjs",
21-
"./esm.mjs": "./esm.mjs"
21+
"./esm.mjs": "./esm.mjs",
22+
"./esm/transpile-only": "./esm/transpile-only.mjs",
23+
"./esm/transpile-only.mjs": "./esm/transpile-only.mjs"
2224
},
2325
"types": "dist/index.d.ts",
2426
"bin": {
@@ -31,6 +33,7 @@
3133
"dist/",
3234
"dist-raw/",
3335
"register/",
36+
"esm/",
3437
"esm.mjs",
3538
"LICENSE",
3639
"tsconfig.schema.json",

src/index.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ describe('ts-node', function () {
7272
// `node --loader ts-node/esm`
7373
testsDirRequire.resolve('ts-node/esm')
7474
testsDirRequire.resolve('ts-node/esm.mjs')
75+
testsDirRequire.resolve('ts-node/esm/transpile-only')
76+
testsDirRequire.resolve('ts-node/esm/transpile-only.mjs')
7577
})
7678

7779
describe('cli', function () {
@@ -821,6 +823,29 @@ describe('ts-node', function () {
821823
return done()
822824
})
823825
})
826+
827+
it('should support transpile only mode via dedicated loader entrypoint', (done) => {
828+
exec(`${cmd}/transpile-only index.ts`, { cwd: join(__dirname, '../tests/esm-transpile-only') }, function (err, stdout) {
829+
expect(err).to.equal(null)
830+
expect(stdout).to.equal('')
831+
832+
return done()
833+
})
834+
})
835+
it('should throw type errors without transpile-only enabled', (done) => {
836+
exec(`${cmd} index.ts`, { cwd: join(__dirname, '../tests/esm-transpile-only') }, function (err, stdout) {
837+
if (err === null) {
838+
return done('Command was expected to fail, but it succeeded.')
839+
}
840+
841+
expect(err.message).to.contain('Unable to compile TypeScript')
842+
expect(err.message).to.match(new RegExp('TS2345: Argument of type \'(?:number|1101)\' is not assignable to parameter of type \'string\'\\.'))
843+
expect(err.message).to.match(new RegExp('TS2322: Type \'(?:"hello world"|string)\' is not assignable to type \'number\'\\.'))
844+
expect(stdout).to.equal('')
845+
846+
return done()
847+
})
848+
})
824849
}
825850

826851
it('executes ESM as CJS, ignoring package.json "types" field (for backwards compatibility; should be changed in next major release to throw ERR_REQUIRE_ESM)', function (done) {

tests/esm-transpile-only/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (typeof module !== 'undefined') throw new Error('module should not exist in ESM')
2+
3+
// intentional type errors to check transpile-only ESM loader skips type checking
4+
parseInt(1101, 2)
5+
const x: number = 'hello world'

tests/esm-transpile-only/package.json

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext",
4+
"allowJs": true,
5+
"jsx": "react"
6+
}
7+
}

0 commit comments

Comments
 (0)