Skip to content

Commit

Permalink
Added a transpile-only ESM loader (#1101) (#1102)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
concision committed Aug 12, 2020
1 parent 08dc47d commit e29ae04
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
7 changes: 7 additions & 0 deletions esm/transpile-only.mjs
@@ -0,0 +1,7 @@
import {fileURLToPath} from 'url'
import {createRequire} from 'module'
const require = createRequire(fileURLToPath(import.meta.url))

/** @type {import('../dist/esm')} */
const esm = require('../dist/esm')
export const {resolve, getFormat, transformSource} = esm.registerAndCreateEsmHooks({transpileOnly: true})
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -18,7 +18,9 @@
"./register/transpile-only": "./register/transpile-only.js",
"./register/type-check": "./register/type-check.js",
"./esm": "./esm.mjs",
"./esm.mjs": "./esm.mjs"
"./esm.mjs": "./esm.mjs",
"./esm/transpile-only": "./esm/transpile-only.mjs",
"./esm/transpile-only.mjs": "./esm/transpile-only.mjs"
},
"types": "dist/index.d.ts",
"bin": {
Expand All @@ -31,6 +33,7 @@
"dist/",
"dist-raw/",
"register/",
"esm/",
"esm.mjs",
"LICENSE",
"tsconfig.schema.json",
Expand Down
25 changes: 25 additions & 0 deletions src/index.spec.ts
Expand Up @@ -72,6 +72,8 @@ describe('ts-node', function () {
// `node --loader ts-node/esm`
testsDirRequire.resolve('ts-node/esm')
testsDirRequire.resolve('ts-node/esm.mjs')
testsDirRequire.resolve('ts-node/esm/transpile-only')
testsDirRequire.resolve('ts-node/esm/transpile-only.mjs')
})

describe('cli', function () {
Expand Down Expand Up @@ -821,6 +823,29 @@ describe('ts-node', function () {
return done()
})
})

it('should support transpile only mode via dedicated loader entrypoint', (done) => {
exec(`${cmd}/transpile-only index.ts`, { cwd: join(__dirname, '../tests/esm-transpile-only') }, function (err, stdout) {
expect(err).to.equal(null)
expect(stdout).to.equal('')

return done()
})
})
it('should throw type errors without transpile-only enabled', (done) => {
exec(`${cmd} index.ts`, { cwd: join(__dirname, '../tests/esm-transpile-only') }, function (err, stdout) {
if (err === null) {
return done('Command was expected to fail, but it succeeded.')
}

expect(err.message).to.contain('Unable to compile TypeScript')
expect(err.message).to.match(new RegExp('TS2345: Argument of type \'(?:number|1101)\' is not assignable to parameter of type \'string\'\\.'))
expect(err.message).to.match(new RegExp('TS2322: Type \'(?:"hello world"|string)\' is not assignable to type \'number\'\\.'))
expect(stdout).to.equal('')

return done()
})
})
}

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) {
Expand Down
5 changes: 5 additions & 0 deletions tests/esm-transpile-only/index.ts
@@ -0,0 +1,5 @@
if (typeof module !== 'undefined') throw new Error('module should not exist in ESM')

// intentional type errors to check transpile-only ESM loader skips type checking
parseInt(1101, 2)
const x: number = 'hello world'
3 changes: 3 additions & 0 deletions tests/esm-transpile-only/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
7 changes: 7 additions & 0 deletions tests/esm-transpile-only/tsconfig.json
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "ESNext",
"allowJs": true,
"jsx": "react"
}
}

0 comments on commit e29ae04

Please sign in to comment.