Skip to content

Commit

Permalink
fix(typescript): Allow for using compilerOptions.moduleResolution (#…
Browse files Browse the repository at this point in the history
…1453)

* fix(typescript): Add test for setting moduleResolution

This adds a test case for resolving types with node ESM `exports` feature
and `compilerOptions.moduleResolution` option configured.

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>

* fix(typescript): Allow for using `compilerOptions.moduleResolution`

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>

---------

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
  • Loading branch information
susnux committed May 12, 2023
1 parent 089a51e commit eb0cfe0
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/typescript/src/options/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function containsEnumOptions(
*/
function setModuleResolutionKind(parsedConfig: ParsedCommandLine): ParsedCommandLine {
const moduleKind = parsedConfig.options.module;
// Fallback if `parsedConfig.options.moduleResolution` is not set
const moduleResolution =
moduleKind === ModuleKind.Node16
? ModuleResolutionKind.Node16
Expand All @@ -115,8 +116,8 @@ function setModuleResolutionKind(parsedConfig: ParsedCommandLine): ParsedCommand
return {
...parsedConfig,
options: {
...parsedConfig.options,
moduleResolution
moduleResolution,
...parsedConfig.options
}
};
}
Expand Down
4 changes: 4 additions & 0 deletions packages/typescript/test/fixtures/nodenext-module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import foo from 'foo';

const bar: string = foo;
console.log(bar);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "nodenext"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compilerOptions": {
"module": "nodenext"
"moduleResolution": "nodenext"
}
}
22 changes: 21 additions & 1 deletion packages/typescript/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,26 @@ test.serial('works when code is in src directory', async (t) => {
});

test.serial('correctly resolves types in a nodenext module', async (t) => {
const warnings = [];
const bundle = await rollup({
input: 'fixtures/nodenext-module/index.ts',
plugins: [
typescript({
tsconfig: 'fixtures/nodenext-module/tsconfig.json'
})
],
onwarn({ toString, ...warning }) {
warnings.push(warning);
}
});
const code = await getCode(bundle, outputOptions);

t.true(code.includes('const bar = foo'), code);
t.is(warnings.length, 1);
t.is(warnings[0].code, 'UNRESOLVED_IMPORT');
});

test.serial('correctly resolves types with nodenext moduleResolution', async (t) => {
const warnings = [];
const bundle = await rollup({
input: 'fixtures/nodenext-resolution/index.ts',
Expand All @@ -1306,7 +1326,7 @@ test.serial('correctly resolves types in a nodenext module', async (t) => {
});
const code = await getCode(bundle, outputOptions);

t.true(code.includes('const bar = foo'), code);
t.true(code.includes('var bar = foo'), code);
t.is(warnings.length, 1);
t.is(warnings[0].code, 'UNRESOLVED_IMPORT');
});
Expand Down

0 comments on commit eb0cfe0

Please sign in to comment.