Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds a configuration option for @types auto-adds
  • Loading branch information
arcanis committed Mar 23, 2022
commit 4e7d0e47f7675b7bf47a1c2d1d010cb9d5c2362a
6 changes: 6 additions & 0 deletions packages/gatsby/static/configuration/yarnrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,12 @@
"type": "string",
"examples": ["yarnpkg/berry"]
},
"tsEnableAutoTypes": {
"_package": "@yarnpkg/plugin-typescript",
"description": "If true, Yarn will automatically add `@types` dependencies when running `yarn add` with packages that don't provide their own typings (as reported by the Algolia npm database). This behaviour is enabled by default if you have a tsconfig file at the root of your project.",
"type": "boolean",
"examples": [true]
},
"unsafeHttpWhitelist": {
"_package": "@yarnpkg/core",
"description": "This setting lists the hostnames for which using the HTTP protocol is allowed. Any other hostname will be required to use HTTPS instead. The reason behind this decision and more details can be found [here](https://github.com/yarnpkg/berry/pull/416).\n\nGlob patterns are supported.",
Expand Down
21 changes: 19 additions & 2 deletions packages/plugin-typescript/sources/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Descriptor, Plugin, Workspace, ResolveOptions, Manifest, AllDependencies, DescriptorHash, Package} from '@yarnpkg/core';
import {Descriptor, Plugin, Workspace, ResolveOptions, Manifest, AllDependencies, DescriptorHash, Package, SettingsType} from '@yarnpkg/core';
import {structUtils, ThrowReport, miscUtils, semverUtils} from '@yarnpkg/core';
import {Filename, ppath, xfs} from '@yarnpkg/fslib';
import {Hooks as EssentialsHooks} from '@yarnpkg/plugin-essentials';
Expand Down Expand Up @@ -26,7 +26,10 @@ const afterWorkspaceDependencyAddition = async (
const {project} = workspace;
const {configuration} = project;

if (!xfs.existsSync(ppath.join(project.cwd, `tsconfig.json` as Filename)))
const tsEnableAutoTypes = configuration.get(`tsEnableAutoTypes`)
?? xfs.existsSync(ppath.join(project.cwd, `tsconfig.json` as Filename));

if (!tsEnableAutoTypes)
return;

const resolver = configuration.makeResolver();
Expand Down Expand Up @@ -132,7 +135,21 @@ const beforeWorkspacePacking = (workspace: Workspace, rawManifest: any) => {
}
};

declare module '@yarnpkg/core' {
interface ConfigurationValueMap {
tsEnableAutoTypes: boolean | null;
}
}

const plugin: Plugin<EssentialsHooks & PackHooks> = {
configuration: {
tsEnableAutoTypes: {
description: `Whether Yarn should auto-install @types/ dependencies on 'yarn add'`,
type: SettingsType.BOOLEAN,
isNullable: true,
default: null,
},
},
hooks: {
afterWorkspaceDependencyAddition,
afterWorkspaceDependencyRemoval,
Expand Down