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

Add support for nodenext moduleResolution #149

Merged
merged 5 commits into from Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -136,7 +136,7 @@ By default, `tsd` applies the following configuration:
"lib": ["es2017"],
"module": "commonjs",
// The following option is set and is not overridable:
"moduleResolution": "node"
"moduleResolution": "node" | "node16" | "nodenext"
}
```

Expand All @@ -153,7 +153,7 @@ These options will be overridden if a `tsconfig.json` file is found in your proj
}
```

*Default options will apply if you don't override them explicitly.* You can't override the `moduleResolution` option.
*Default options will apply if you don't override them explicitly.* You can't override the `moduleResolution` option. `moduleResolution` is set to `nodenext` if `module` is `nodenext`, `node16` if `module` is `node16` or `node` otherwise.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to explain this using a code comment on line 139


## Assertions

Expand Down
18 changes: 14 additions & 4 deletions source/lib/config.ts
Expand Up @@ -27,19 +27,29 @@ export default (pkg: PackageJsonWithTsdConfig, cwd: string): Config => {
cwd
);

const combinedCompilerOptions = {
...tsConfigCompilerOptions,
...packageJsonCompilerOptions,
};

const module = combinedCompilerOptions.module ?? ModuleKind.CommonJS;

return {
directory: 'test-d',
...pkgConfig,
compilerOptions: {
strict: true,
jsx: JsxEmit.React,
lib: parseRawLibs(['es2017', 'dom', 'dom.iterable'], cwd),
module: ModuleKind.CommonJS,
module,
target: ScriptTarget.ES2017,
esModuleInterop: true,
...tsConfigCompilerOptions,
...packageJsonCompilerOptions,
moduleResolution: ModuleResolutionKind.NodeJs,
...combinedCompilerOptions,
moduleResolution: module === ModuleKind.NodeNext ?
ModuleResolutionKind.NodeNext :
module === ModuleKind.Node16 ?
ModuleResolutionKind.Node16 :
ModuleResolutionKind.NodeJs,
skipLibCheck: false
}
};
Expand Down
@@ -0,0 +1,3 @@
declare const foo: string;

export default foo;
@@ -0,0 +1 @@
module.exports.default = "foo";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use single-quotes.

@@ -0,0 +1,4 @@
import foo from "foo";
import {expectType} from '../../../..';

expectType<string>(foo);
@@ -0,0 +1,9 @@
{
"name": "foo",
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
"exports": "./index.js",
"tsd": {
"compilerOptions": {
"module": "node16"
}
}
}
@@ -0,0 +1,3 @@
declare const foo: string;

export default foo;
@@ -0,0 +1 @@
module.exports.default = "foo";
@@ -0,0 +1,4 @@
import foo from "foo";
import {expectType} from '../../../..';

expectType<string>(foo);
@@ -0,0 +1,4 @@
{
"name": "foo",
"exports": "./index.js"
}
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "node16"
}
}
@@ -0,0 +1,3 @@
declare const foo: string;

export default foo;
@@ -0,0 +1 @@
module.exports.default = "foo";
@@ -0,0 +1,4 @@
import foo from "foo";
import {expectType} from '../../../..';

expectType<string>(foo);
@@ -0,0 +1,9 @@
{
"name": "foo",
"exports": "./index.js",
"tsd": {
"compilerOptions": {
"module": "nodenext"
}
}
}
@@ -0,0 +1,3 @@
declare const foo: string;

export default foo;
@@ -0,0 +1 @@
module.exports.default = "foo";
@@ -0,0 +1,4 @@
import foo from "foo";
import {expectType} from '../../../..';

expectType<string>(foo);
@@ -0,0 +1,4 @@
{
"name": "foo",
"exports": "./index.js"
}
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "nodenext"
}
}
24 changes: 24 additions & 0 deletions source/test/test.ts
Expand Up @@ -138,6 +138,30 @@ test('allow specifying a lib in tsconfig.json', async t => {
verify(t, diagnostics, []);
});

test('use moduleResolution NodeNext when module is nodenext in tsconfig.json', async t => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('use moduleResolution NodeNext when module is nodenext in tsconfig.json', async t => {
test('use moduleResolution `nodenext` when module is `nodenext` in tsconfig.json', async t => {

const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/nodenext-from-tsconfig-json')});

verify(t, diagnostics, []);
});

test('use moduleResolution NodeNext when module is nodenext in package.json', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/nodenext-from-package-json')});

verify(t, diagnostics, []);
});

test('use moduleResolution Node16 when module is node16 in tsconfig.json', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/node16-from-tsconfig-json')});

verify(t, diagnostics, []);
});

test('use moduleResolution Node16 when module is node16 in package.json', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/node16-from-package-json')});

verify(t, diagnostics, []);
});

test('add support for esm with esModuleInterop', async t => {
const diagnostics = await tsd({
cwd: path.join(__dirname, 'fixtures/esm')
Expand Down