Skip to content

Commit

Permalink
Merge pull request #670 from rexxars/fix/ts-types-import
Browse files Browse the repository at this point in the history
fix: allow using @types-module for typescript type-only imports
  • Loading branch information
rumpl committed Nov 10, 2021
2 parents a687b10 + 8643cd3 commit 180e05a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/detector/importDeclaration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { extractInlineWebpack } from './extract';

export default function detectImportDeclaration(node) {
return node.type === 'ImportDeclaration' && node.source && node.source.value
? extractInlineWebpack(node.source.value)
: [];
export default function detectImportDeclaration(node, deps) {
if (node.type !== 'ImportDeclaration' || !node.source || !node.source.value) {
return [];
}

// Typescript "import type X from 'foo'" - doesn't need to depend on the
// actual module, instead it can rely on `@types/<module>` instead.
if (
node.importKind === 'type' &&
deps.includes(`@types/${node.source.value}`)
) {
return [`@types/${node.source.value}`];
}

return extractInlineWebpack(node.source.value);
}
1 change: 1 addition & 0 deletions test/fake_modules/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"@types/org__org-pkg": "0.0.1",
"@types/react": "0.0.1",
"@types/node": "0.0.1",
"@types/typeless-module": "0.0.1",
"react": "0.0.1",
"ts-dep-1": "0.0.1",
"ts-dep-2": "0.0.1",
Expand Down
5 changes: 5 additions & 0 deletions test/fake_modules/typescript/typeOnly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Foo } from 'typeless-module';

const bar: Foo = { prop: 'here' };

export default bar;
1 change: 1 addition & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export default [
'@types/react': ['component.tsx'],
'@types/node': ['esnext.ts'],
'@types/org__org-pkg': ['esnext.ts'],
'@types/typeless-module': ['typeOnly.ts'],
'@org/org-pkg': ['esnext.ts'],
'ts-dep-1': ['index.ts'],
'ts-dep-2': ['index.ts'],
Expand Down

0 comments on commit 180e05a

Please sign in to comment.