Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a transpile-only ESM loader (#1101) #1102

Merged
merged 7 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions esm/transpile-only.mjs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -802,6 +804,29 @@ describe('ts-node', function () {
return done()
})
})

concision marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
7 changes: 7 additions & 0 deletions tests/esm-transpile-only/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "ESNext",
"allowJs": true,
"jsx": "react"
}
}