Skip to content

Commit 3da0edc

Browse files
authored
Add @types packages support (#360)
1 parent e6694b5 commit 3da0edc

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1717
<!-- ### Fixed -->
1818
<!-- ### Removed -->
1919

20-
<!-- ## Unreleased -->
20+
## Unreleased
21+
22+
### Added
23+
24+
- Fetch all `@types` packages listed in the project's `package.json` file and
25+
include them for TypeScript compilation. This allows type-checking packages
26+
that do not ship their own types but do have a DefinitelyTyped package
27+
available. Note: This does not automatically download the `@types` package for
28+
a package. It must be manually listed in `package.json`.
2129

2230
## [0.17.0] - 2022-11-11
2331

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ statement should use the `.js` extension (the same as you would do when running
361361
import './my-other-module.js';
362362
```
363363

364+
You may also include any Definitely Typed (`@types`) packages for type checking
365+
during compilation by listing it as a dependency in the project's
366+
[`package.json` file](#packagejson).
367+
364368
## Hiding & folding
365369

366370
If a region of code in a Playground project file is surrounded by

src/test/types-fetcher_test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,52 @@ suite('types fetcher', () => {
630630
});
631631
});
632632

633+
test('@types package', async () => {
634+
const sourceTexts: string[] = [];
635+
const packageJson: PackageJson = {
636+
dependencies: {
637+
'@types/a': '1.0.0',
638+
},
639+
};
640+
const cdnData: CdnData = {
641+
'@types/a': {
642+
versions: {
643+
'1.0.0': {
644+
files: {
645+
'index.d.ts': {
646+
content: 'declare module a { export const a: 1; }',
647+
},
648+
},
649+
},
650+
},
651+
},
652+
};
653+
const expectedDependencyGraph: ExpectedDependencyGraph = {
654+
root: {
655+
'@types/a': '1.0.0',
656+
},
657+
deps: {},
658+
};
659+
const expectedLayout: NodeModulesDirectory = {
660+
'@types/a': {
661+
version: '1.0.0',
662+
nodeModules: {},
663+
},
664+
};
665+
const expectedFiles = new Map([
666+
['@types/a/index.d.ts', 'declare module a { export const a: 1; }'],
667+
['@types/a/package.json', '{}'],
668+
]);
669+
await checkTypesFetcher({
670+
sourceTexts,
671+
packageJson,
672+
cdnData,
673+
expectedFiles,
674+
expectedDependencyGraph,
675+
expectedLayout,
676+
});
677+
});
678+
633679
test('declare module', async () => {
634680
// Declaring a module should not count as an import, but anything imported
635681
// from within the declare module block should.

src/typescript-worker/types-fetcher.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export class TypesFetcher {
9494
fetcher._handleBareAndRelativeSpecifiers(source, root)
9595
),
9696
...tsLibs.map((lib) => fetcher._addTypeScriptStandardLib(lib)),
97+
fetcher._fetchTypesPackages(),
9798
]);
9899
const layout = new NodeModulesLayoutMaker().layout(
99100
fetcher._rootDependencies,
@@ -123,6 +124,27 @@ export class TypesFetcher {
123124
this._rootPackageJson = rootPackageJson;
124125
}
125126

127+
private async _fetchTypesPackages(): Promise<void> {
128+
if (
129+
this._rootPackageJson === undefined ||
130+
this._rootPackageJson.dependencies === undefined
131+
) {
132+
return;
133+
}
134+
135+
const typesPackages = Object.keys(
136+
this._rootPackageJson.dependencies
137+
).filter((k) => k.startsWith('@types/'));
138+
139+
if (typesPackages.length === 0) {
140+
return;
141+
}
142+
143+
await Promise.allSettled(
144+
typesPackages.map((k) => this._handleBareSpecifier(k, root))
145+
);
146+
}
147+
126148
private async _addTypeScriptStandardLib(lib: string): Promise<void> {
127149
return this._handleBareSpecifier(
128150
`typescript/lib/lib.${lib.toLowerCase()}.js`,

web-test-runner.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ export default {
4545
// only one or the other can be installed at once (see our "postinstall" NPM
4646
// script). See
4747
// https://modern-web.dev/docs/test-runner/browser-launchers/puppeteer/.
48-
puppeteerLauncher({launchOptions: {product: 'firefox'}}),
48+
puppeteerLauncher({launchOptions: {product: 'firefox'}, concurrency: 1}),
4949
],
50+
concurrentBrowsers: Number(process.env.CONCURRENT_BROWSERS) || 2, // default 2
5051
browserStartTimeout: 30000, // default 30000
5152
testsStartTimeout: 20000, // default 10000
5253
testsFinishTimeout: 90000, // default 20000

0 commit comments

Comments
 (0)