-
-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Expose esm hooks factory to public API (#1439)
* Add esm to project exports * Convert internal `registerAndCreateEsmHooks` to `createEsmHooks` API * Revert "Add esm to project exports" This reverts commit f53ac63. * Revert esm loaders + add `registerAndCreateEsmHooks` * Add tests for `createEsmHooks` * refactor experimentalEsmLoader into an @internal method on the Service: enableExperimentalEsmLoaderInterop() This avoids consumers needing to pass an @internal flag at service creation time, since we can call the method automatically within createEsmHooks. * lint-fix * Make test case more robust * fix * Fix version check; we do not support ESM loader on less than node 12.16 Co-authored-by: Andrew Bradley <[email protected]>
- Loading branch information
Showing
9 changed files
with
107 additions
and
23 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
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
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,39 @@ | ||
// ESM loader hook tests | ||
// TODO: at the time of writing, other ESM loader hook tests have not been moved into this file. | ||
// Should consolidate them here. | ||
|
||
import { context } from './testlib'; | ||
import semver = require('semver'); | ||
import { | ||
contextTsNodeUnderTest, | ||
EXPERIMENTAL_MODULES_FLAG, | ||
TEST_DIR, | ||
} from './helpers'; | ||
import { createExec } from './exec-helpers'; | ||
import { join } from 'path'; | ||
import * as expect from 'expect'; | ||
|
||
const test = context(contextTsNodeUnderTest); | ||
|
||
const exec = createExec({ | ||
cwd: TEST_DIR, | ||
}); | ||
|
||
test.suite('createEsmHooks', (test) => { | ||
if (semver.gte(process.version, '12.16.0')) { | ||
test('should create proper hooks with provided instance', async () => { | ||
const { err } = await exec( | ||
`node ${EXPERIMENTAL_MODULES_FLAG} --loader ./loader.mjs index.ts`, | ||
{ | ||
cwd: join(TEST_DIR, './esm-custom-loader'), | ||
} | ||
); | ||
|
||
if (err === null) { | ||
throw new Error('Command was expected to fail, but it succeeded.'); | ||
} | ||
|
||
expect(err.message).toMatch(/TS6133:\s+'unusedVar'/); | ||
}); | ||
} | ||
}); |
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,4 @@ | ||
export function abc() { | ||
let unusedVar: string; | ||
return true; | ||
} |
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 @@ | ||
import { fileURLToPath } from 'url'; | ||
import { createRequire } from 'module'; | ||
const require = createRequire(fileURLToPath(import.meta.url)); | ||
|
||
/** @type {import('../../dist')} **/ | ||
const { createEsmHooks, register } = require('ts-node'); | ||
|
||
const tsNodeInstance = register({ | ||
compilerOptions: { | ||
noUnusedLocals: true, | ||
}, | ||
}); | ||
|
||
export const { resolve, getFormat, transformSource } = createEsmHooks( | ||
tsNodeInstance | ||
); |
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": "module" | ||
} |
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": "ESNext", | ||
"moduleResolution": "node", | ||
"noUnusedLocals": false | ||
} | ||
} |