-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ts-node integration as sucrase/ts-node-plugin (#729)
Fixes #726 This makes it easy to use Sucrase with all of the nice benefits that ts-node provides, such as an ESM loader, tsconfig discovery, and a REPL. The implementation is based off of these docs: https://typestrong.org/ts-node/docs/transpilers/ and the built-in SWC integration: https://github.com/TypeStrong/ts-node/blob/main/src/transpilers/swc.ts Some implementation notes: * Currently, the plugin is written as a CJS module and written in JS rather than TS. This it the easiest approach with the current build system, though it may be reasonable to extend the build system to compile a `ts-node-plugin-src` directory to `ts-node-plugin` or something like that. The future plan is to add an `exports` line to `package.json` in a semver-major release, which will make this a bit easier to manage. * The plugin is included as part of the core `sucrase` package rather than as an integration to be installed separately. This should make the usage and version management a little easier, and feels reasonable because the integration is small and has zero dependencies. * I rewrote the usage section of the README to put installation instructions at the top and to suggest ts-node as the recommended way to use Sucrase with Node. * I added an `integration-test` directory with various cases that this plugin should handle, and it may be useful for future node/tooling integration tests as well.
- Loading branch information
1 parent
d8bf165
commit 7b349b1
Showing
50 changed files
with
553 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
benchmark/sample/* | ||
benchmark/node_modules/* | ||
example-runner/example-repos | ||
integration-test/test-cases | ||
spec-compliance-tests/babel-tests/babel-tests-checkout | ||
spec-compliance-tests/test262/test262-checkout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
!bin/** | ||
!dist/** | ||
!register/** | ||
!ts-node-plugin/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {join} from "path"; | ||
import {readdir, stat} from "fs/promises"; | ||
import {exec} from "child_process"; | ||
import {promisify} from "util"; | ||
const execPromise = promisify(exec); | ||
|
||
describe("ts-node tests", async () => { | ||
/** | ||
* Find all integration tests in the test-cases directory. | ||
* | ||
* Each test has a file starting with "main" (e.g. main.ts, main.tsx, | ||
* main.mts, etc) that's used as the entry point. The test should be written | ||
* in such a way that the execution throws an exception if the test fails. | ||
*/ | ||
async function* discoverTests(dir: string): AsyncIterable<string> { | ||
for (const child of await readdir(dir)) { | ||
const childPath = join(dir, child); | ||
if ((await stat(childPath)).isDirectory()) { | ||
yield* discoverTests(childPath); | ||
} else if (child.startsWith("main")) { | ||
yield childPath; | ||
} | ||
} | ||
} | ||
|
||
for await (const testPath of discoverTests("test-cases")) { | ||
it(testPath, async () => { | ||
// To help confirm that the behavior is in sync with the default ts-node | ||
// behavior, first run ts-node without the plugin to make sure it works, | ||
// then run it with the plugin. | ||
await execPromise(`npx ts-node --esm --transpile-only ${testPath}`); | ||
await execPromise( | ||
`npx ts-node --esm --transpiler ${__dirname}/../ts-node-plugin ${testPath}`, | ||
); | ||
}); | ||
} | ||
|
||
// Currently, mocha needs to be run with --delay to allow async test | ||
// generation like this, and that also requires explicitly invoking this run | ||
// callback. | ||
run(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "sucrase-integration-tests", | ||
"version": "1.0.0", | ||
"private": true, | ||
"devDependencies": { | ||
"ts-node": "^10.9.1" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
integration-test/test-cases/commonjs-cases/allows-js-files-with-import-syntax/file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x = 4; |
13 changes: 13 additions & 0 deletions
13
integration-test/test-cases/commonjs-cases/allows-js-files-with-import-syntax/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {x} from './file'; | ||
|
||
if (x !== 4) { | ||
throw new Error(); | ||
} | ||
|
||
const a = 1; | ||
// This snippet confirms that we're running in JS, not TS. In TS, it is parsed | ||
// as a function call, and in JS, it is parsed as comparison operators. | ||
const comparisonResult = a<2>(3); | ||
if (comparisonResult !== false) { | ||
throw new Error(); | ||
} |
8 changes: 8 additions & 0 deletions
8
integration-test/test-cases/commonjs-cases/allows-js-files-with-import-syntax/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"target": "ESNext", | ||
"esModuleInterop": true, | ||
"allowJs": true | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
integration-test/test-cases/commonjs-cases/dynamic-import-is-transpiled/file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x = 7; |
14 changes: 14 additions & 0 deletions
14
integration-test/test-cases/commonjs-cases/dynamic-import-is-transpiled/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
async function foo() { | ||
let calledFakeRequire = false; | ||
const require = () => { | ||
calledFakeRequire = true; | ||
} | ||
// Import should become require, which will end up calling our shadowed | ||
// declaration of require. This is different behavior from nodenext, where | ||
// import should be a true ESM import. | ||
const OtherFile = await import('./file'); | ||
if (!calledFakeRequire) { | ||
throw new Error(); | ||
} | ||
} | ||
foo(); |
3 changes: 3 additions & 0 deletions
3
integration-test/test-cases/commonjs-cases/esmoduleinterop-false-is-respected/file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function twelve() { | ||
return 12; | ||
} |
5 changes: 5 additions & 0 deletions
5
integration-test/test-cases/commonjs-cases/esmoduleinterop-false-is-respected/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as twelve from './file'; | ||
|
||
if (twelve() !== 12) { | ||
throw new Error(); | ||
} |
7 changes: 7 additions & 0 deletions
7
integration-test/test-cases/commonjs-cases/esmoduleinterop-false-is-respected/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"target": "ESNext", | ||
"esModuleInterop": false | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"target": "ESNext", | ||
"esModuleInterop": true | ||
}, | ||
} |
16 changes: 16 additions & 0 deletions
16
integration-test/test-cases/jsx-cases/allows-automatic-dev-transform/main.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
let wasCalled: boolean = false; | ||
function require(path) { | ||
if (path !== "react/jsx-dev-runtime") { | ||
throw new Error(); | ||
} | ||
return { | ||
jsxDEV: () => { | ||
wasCalled = true; | ||
} | ||
}; | ||
} | ||
|
||
const elem = <div />; | ||
if (!wasCalled) { | ||
throw new Error(); | ||
} |
8 changes: 8 additions & 0 deletions
8
integration-test/test-cases/jsx-cases/allows-automatic-dev-transform/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "NodeNext", | ||
"target": "ESNext", | ||
"esModuleInterop": true, | ||
"jsx": "react-jsxdev", | ||
}, | ||
} |
16 changes: 16 additions & 0 deletions
16
integration-test/test-cases/jsx-cases/allows-automatic-prod-transform/main.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
let wasCalled: boolean = false; | ||
function require(path) { | ||
if (path !== "react/jsx-runtime") { | ||
throw new Error(); | ||
} | ||
return { | ||
jsx: () => { | ||
wasCalled = true; | ||
} | ||
}; | ||
} | ||
|
||
const elem = <div />; | ||
if (!wasCalled) { | ||
throw new Error(); | ||
} |
8 changes: 8 additions & 0 deletions
8
integration-test/test-cases/jsx-cases/allows-automatic-prod-transform/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "NodeNext", | ||
"target": "ESNext", | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
}, | ||
} |
20 changes: 20 additions & 0 deletions
20
integration-test/test-cases/jsx-cases/jsx-factory-config-is-respected/main.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
let hWasCalledWithDiv = false; | ||
let hWasCalledWithFragment = false; | ||
const Fragment = {}; | ||
|
||
function h(tag) { | ||
if (tag === 'div') { | ||
hWasCalledWithDiv = true; | ||
} else if (tag === Fragment) { | ||
hWasCalledWithFragment = true; | ||
} | ||
} | ||
const elem1 = <div />; | ||
if (!hWasCalledWithDiv) { | ||
throw new Error(); | ||
} | ||
|
||
const elem2 = <>hello</>; | ||
if (!hWasCalledWithFragment) { | ||
throw new Error(); | ||
} |
10 changes: 10 additions & 0 deletions
10
integration-test/test-cases/jsx-cases/jsx-factory-config-is-respected/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "NodeNext", | ||
"target": "ESNext", | ||
"esModuleInterop": true, | ||
"jsx": "react", | ||
"jsxFactory": "h", | ||
"jsxFragmentFactory": "Fragment" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
integration-test/test-cases/jsx-cases/jsx-files-are-transpiled/main.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
let wasCalled = false; | ||
const React = { | ||
createElement() { | ||
wasCalled = true; | ||
} | ||
} | ||
const elem = <div />; | ||
if (!wasCalled) { | ||
throw new Error(); | ||
} | ||
|
||
const a = 1; | ||
// This snippet confirms that we're running in JS, not TS. In TS, it is parsed | ||
// as a function call, and in JS, it is parsed as comparison operators. | ||
const comparisonResult = a<2>(3); | ||
if (comparisonResult !== false) { | ||
throw new Error(); | ||
} | ||
{} |
9 changes: 9 additions & 0 deletions
9
integration-test/test-cases/jsx-cases/jsx-files-are-transpiled/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "NodeNext", | ||
"target": "ESNext", | ||
"esModuleInterop": true, | ||
"jsx": "react", | ||
"allowJs": true | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
integration-test/test-cases/jsx-cases/jsx-import-source-is-respected/main.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
let wasCalled: boolean = false; | ||
function require(path) { | ||
if (path !== "my-library/jsx-runtime") { | ||
throw new Error(); | ||
} | ||
return { | ||
jsx: () => { | ||
wasCalled = true; | ||
} | ||
}; | ||
} | ||
|
||
const elem = <div />; | ||
if (!wasCalled) { | ||
throw new Error(); | ||
} |
9 changes: 9 additions & 0 deletions
9
integration-test/test-cases/jsx-cases/jsx-import-source-is-respected/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "NodeNext", | ||
"target": "ESNext", | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "my-library" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
integration-test/test-cases/jsx-cases/tsx-files-allow-jsx-syntax/main.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
let wasCalled: boolean = false; | ||
const React = { | ||
createElement(): void { | ||
wasCalled = true; | ||
} | ||
} | ||
const elem = <div />; | ||
if (!wasCalled) { | ||
throw new Error(); | ||
} |
8 changes: 8 additions & 0 deletions
8
integration-test/test-cases/jsx-cases/tsx-files-allow-jsx-syntax/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "NodeNext", | ||
"target": "ESNext", | ||
"esModuleInterop": true, | ||
"jsx": "react", | ||
}, | ||
} |
3 changes: 3 additions & 0 deletions
3
integration-test/test-cases/nodenext-cases/cts-can-dynamic-import-mts/file.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Crashes if run as CJS | ||
console.log(import.meta.url); | ||
export const x = 3; |
Oops, something went wrong.