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 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
27 changes: 27 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 @@ -802,6 +804,31 @@ describe('ts-node', function () {
return done()
})
})

concision marked this conversation as resolved.
Show resolved Hide resolved
describe('support a transpile only mode', () => {
it('should execute successfully with type errors and transpile-only enabled', (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('foo bar baz biff\n')

return done()
})
})
it('should throw an error with type errors and transpile-only disabled', (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
3 changes: 3 additions & 0 deletions tests/esm-transpile-only/bar.ts
@@ -0,0 +1,3 @@
export const bar = 'bar'

if (typeof module !== 'undefined') throw new Error('module should not exist in ESM')
3 changes: 3 additions & 0 deletions tests/esm-transpile-only/baz.js
@@ -0,0 +1,3 @@
export const baz = 'baz'

if (typeof module !== 'undefined') throw new Error('module should not exist in ESM')
8 changes: 8 additions & 0 deletions tests/esm-transpile-only/biff.jsx
@@ -0,0 +1,8 @@
export const biff = 'biff'

const React = {
createElement() {}
}
const div = <div></div>

if (typeof module !== 'undefined') throw new Error('module should not exist in ESM')
3 changes: 3 additions & 0 deletions tests/esm-transpile-only/foo.ts
@@ -0,0 +1,3 @@
export const foo = 'foo'

if (typeof module !== 'undefined') throw new Error('module should not exist in ESM')
12 changes: 12 additions & 0 deletions tests/esm-transpile-only/index.ts
@@ -0,0 +1,12 @@
import { foo } from './foo.js'
import { bar } from './bar.js'
import { baz } from './baz.js'
import { biff } from './biff.js'
concision marked this conversation as resolved.
Show resolved Hide resolved

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'

console.log(`${foo} ${bar} ${baz} ${biff}`)
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"
}
}