Skip to content

Commit 8ab71fb

Browse files
Lightning00BladeOrKoN
andauthoredAug 1, 2024··
refactor!: remove support for NPM configuration (#12792)
Co-authored-by: Alex Rudenko <alexrudenko@chromium.org>
1 parent 04e2263 commit 8ab71fb

7 files changed

+65
-113
lines changed
 

‎docs/guides/configuration.md

-7
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ Puppeteer will look up the file tree for any of the following formats:
4747
See the [`Configuration`](../api/puppeteer.configuration) interface for possible
4848
options.
4949

50-
:::caution
51-
52-
Previous versions of Puppeteer allowed configuration via the `config` key in
53-
`package.json`. This behavior is now deprecated and will be removed in the future.
54-
55-
:::
56-
5750
### Changing download options
5851

5952
When the changes to the configuration include changes to download option,

‎packages/puppeteer/src/getConfiguration.ts

+47-75
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {join} from 'path';
1010
import {cosmiconfigSync} from 'cosmiconfig';
1111
import type {Configuration, SupportedBrowser} from 'puppeteer-core';
1212

13-
function getBooleanEnvVar(name: string) {
13+
function getBooleanEnvVar(name: string): boolean | undefined {
1414
const env = process.env[name];
1515
if (env === undefined) {
1616
return;
@@ -29,7 +29,7 @@ function getBooleanEnvVar(name: string) {
2929
/**
3030
* @internal
3131
*/
32-
function isSupportedProduct(product: unknown): product is SupportedBrowser {
32+
function isSupportedBrowser(product: unknown): product is SupportedBrowser {
3333
switch (product) {
3434
case 'chrome':
3535
case 'firefox':
@@ -39,6 +39,36 @@ function isSupportedProduct(product: unknown): product is SupportedBrowser {
3939
}
4040
}
4141

42+
/**
43+
* @internal
44+
*/
45+
function getDefaultBrowser(browser: unknown): SupportedBrowser {
46+
// Validate configuration.
47+
if (browser && !isSupportedBrowser(browser)) {
48+
throw new Error(`Unsupported browser ${browser}`);
49+
}
50+
switch (browser) {
51+
case 'firefox':
52+
return 'firefox';
53+
default:
54+
return 'chrome';
55+
}
56+
}
57+
58+
/**
59+
* @internal
60+
*/
61+
function getLogLevel(logLevel: unknown): 'silent' | 'error' | 'warn' {
62+
switch (logLevel) {
63+
case 'silent':
64+
return 'silent';
65+
case 'error':
66+
return 'error';
67+
default:
68+
return 'warn';
69+
}
70+
}
71+
4272
/**
4373
* @internal
4474
*/
@@ -48,114 +78,56 @@ export const getConfiguration = (): Configuration => {
4878
}).search();
4979
const configuration: Configuration = result ? result.config : {};
5080

51-
configuration.logLevel = (process.env['PUPPETEER_LOGLEVEL'] ??
52-
process.env['npm_config_LOGLEVEL'] ??
53-
process.env['npm_package_config_LOGLEVEL'] ??
54-
configuration.logLevel ??
55-
'warn') as 'silent' | 'error' | 'warn';
81+
configuration.logLevel = getLogLevel(
82+
process.env['PUPPETEER_LOGLEVEL'] ?? configuration.logLevel
83+
);
5684

5785
// Merging environment variables.
58-
configuration.defaultBrowser = (process.env['PUPPETEER_BROWSER'] ??
59-
process.env['npm_config_puppeteer_browser'] ??
60-
process.env['npm_package_config_puppeteer_browser'] ??
61-
configuration.defaultBrowser ??
62-
'chrome') as SupportedBrowser;
86+
configuration.defaultBrowser = getDefaultBrowser(
87+
process.env['PUPPETEER_BROWSER'] ?? configuration.defaultBrowser
88+
);
6389

6490
configuration.executablePath =
65-
process.env['PUPPETEER_EXECUTABLE_PATH'] ??
66-
process.env['npm_config_puppeteer_executable_path'] ??
67-
process.env['npm_package_config_puppeteer_executable_path'] ??
68-
configuration.executablePath;
91+
process.env['PUPPETEER_EXECUTABLE_PATH'] ?? configuration.executablePath;
6992

7093
// Default to skipDownload if executablePath is set
7194
if (configuration.executablePath) {
7295
configuration.skipDownload = true;
7396
}
7497

7598
// Set skipDownload explicitly or from default
76-
configuration.skipDownload = Boolean(
77-
getBooleanEnvVar('PUPPETEER_SKIP_DOWNLOAD') ??
78-
getBooleanEnvVar('npm_config_puppeteer_skip_download') ??
79-
getBooleanEnvVar('npm_package_config_puppeteer_skip_download') ??
80-
configuration.skipDownload
81-
);
99+
configuration.skipDownload =
100+
getBooleanEnvVar('PUPPETEER_SKIP_DOWNLOAD') ?? configuration.skipDownload;
82101

83102
// Set skipChromeDownload explicitly or from default
84-
configuration.skipChromeDownload = Boolean(
103+
configuration.skipChromeDownload =
85104
getBooleanEnvVar('PUPPETEER_SKIP_CHROME_DOWNLOAD') ??
86-
getBooleanEnvVar('npm_config_puppeteer_skip_chrome_download') ??
87-
getBooleanEnvVar('npm_package_config_puppeteer_skip_chrome_download') ??
88-
configuration.skipChromeDownload
89-
);
105+
configuration.skipChromeDownload;
90106

91107
// Set skipChromeDownload explicitly or from default
92-
configuration.skipChromeHeadlessShellDownload = Boolean(
108+
configuration.skipChromeHeadlessShellDownload =
93109
getBooleanEnvVar('PUPPETEER_SKIP_CHROME_HEADLESS_SHELL_DOWNLOAD') ??
94-
getBooleanEnvVar(
95-
'npm_config_puppeteer_skip_chrome_headless_shell_download'
96-
) ??
97-
getBooleanEnvVar(
98-
'npm_package_config_puppeteer_skip_chrome_headless_shell_download'
99-
) ??
100-
configuration.skipChromeHeadlessShellDownload
101-
);
110+
configuration.skipChromeHeadlessShellDownload;
102111

103112
// Prepare variables used in browser downloading
104113
if (!configuration.skipDownload) {
105114
configuration.browserRevision =
106115
process.env['PUPPETEER_BROWSER_REVISION'] ??
107-
process.env['npm_config_puppeteer_browser_revision'] ??
108-
process.env['npm_package_config_puppeteer_browser_revision'] ??
109116
configuration.browserRevision;
110117

111-
const downloadHost =
112-
process.env['PUPPETEER_DOWNLOAD_HOST'] ??
113-
process.env['npm_config_puppeteer_download_host'] ??
114-
process.env['npm_package_config_puppeteer_download_host'];
115-
116-
if (downloadHost && configuration.logLevel === 'warn') {
117-
console.warn(
118-
`PUPPETEER_DOWNLOAD_HOST is deprecated. Use PUPPETEER_DOWNLOAD_BASE_URL instead.`
119-
);
120-
}
121-
122118
configuration.downloadBaseUrl =
123119
process.env['PUPPETEER_DOWNLOAD_BASE_URL'] ??
124-
process.env['npm_config_puppeteer_download_base_url'] ??
125-
process.env['npm_package_config_puppeteer_download_base_url'] ??
126-
configuration.downloadBaseUrl ??
127-
downloadHost;
128-
}
129-
130-
if (
131-
Object.keys(process.env).some(key => {
132-
return key.startsWith('npm_package_config_puppeteer_');
133-
}) &&
134-
configuration.logLevel === 'warn'
135-
) {
136-
console.warn(
137-
`Configuring Puppeteer via npm/package.json is deprecated. Use https://pptr.dev/guides/configuration instead.`
138-
);
120+
configuration.downloadBaseUrl;
139121
}
140122

141123
configuration.cacheDirectory =
142124
process.env['PUPPETEER_CACHE_DIR'] ??
143-
process.env['npm_config_puppeteer_cache_dir'] ??
144-
process.env['npm_package_config_puppeteer_cache_dir'] ??
145125
configuration.cacheDirectory ??
146126
join(homedir(), '.cache', 'puppeteer');
147127
configuration.temporaryDirectory =
148-
process.env['PUPPETEER_TMP_DIR'] ??
149-
process.env['npm_config_puppeteer_tmp_dir'] ??
150-
process.env['npm_package_config_puppeteer_tmp_dir'] ??
151-
configuration.temporaryDirectory;
128+
process.env['PUPPETEER_TMP_DIR'] ?? configuration.temporaryDirectory;
152129

153130
configuration.experiments ??= {};
154131

155-
// Validate configuration.
156-
if (!isSupportedProduct(configuration.defaultBrowser)) {
157-
throw new Error(`Unsupported product ${configuration.defaultBrowser}`);
158-
}
159-
160132
return configuration;
161133
};

‎website/versioned_docs/version-22.15.0/api/puppeteer.product.md

-13
This file was deleted.

‎website/versioned_docs/version-22.15.0/api/puppeteer.productlauncher.defaultargs.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
2-
sidebar_label: ProductLauncher.defaultArgs
2+
sidebar_label: BrowserLauncher.defaultArgs
33
---
44

5-
# ProductLauncher.defaultArgs() method
5+
# BrowserLauncher.defaultArgs() method
66

77
### Signature
88

99
```typescript
10-
class ProductLauncher {
10+
class BrowserLauncher {
1111
abstract defaultArgs(object: BrowserLaunchArgumentOptions): string[];
1212
}
1313
```

‎website/versioned_docs/version-22.15.0/api/puppeteer.productlauncher.executablepath.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
2-
sidebar_label: ProductLauncher.executablePath
2+
sidebar_label: BrowserLauncher.executablePath
33
---
44

5-
# ProductLauncher.executablePath() method
5+
# BrowserLauncher.executablePath() method
66

77
### Signature
88

99
```typescript
10-
class ProductLauncher {
10+
class BrowserLauncher {
1111
abstract executablePath(channel?: ChromeReleaseChannel): string;
1212
}
1313
```

‎website/versioned_docs/version-22.15.0/api/puppeteer.productlauncher.launch.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
2-
sidebar_label: ProductLauncher.launch
2+
sidebar_label: BrowserLauncher.launch
33
---
44

5-
# ProductLauncher.launch() method
5+
# BrowserLauncher.launch() method
66

77
### Signature
88

99
```typescript
10-
class ProductLauncher {
10+
class BrowserLauncher {
1111
launch(options?: PuppeteerNodeLaunchOptions): Promise<Browser>;
1212
}
1313
```

‎website/versioned_docs/version-22.15.0/api/puppeteer.productlauncher.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
2-
sidebar_label: ProductLauncher
2+
sidebar_label: BrowserLauncher
33
---
44

5-
# ProductLauncher class
5+
# BrowserLauncher class
66

77
Describes a launcher - a class that is able to create and launch a browser instance.
88

99
### Signature
1010

1111
```typescript
12-
export declare abstract class ProductLauncher
12+
export declare abstract class BrowserLauncher
1313
```
1414

1515
## Remarks
1616

17-
The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `ProductLauncher` class.
17+
The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `BrowserLauncher` class.
1818

1919
## Properties
2020

@@ -37,15 +37,15 @@ Description
3737
</th></tr></thead>
3838
<tbody><tr><td>
3939

40-
<span id="product">product</span>
40+
<span id="browser">browser</span>
4141

4242
</td><td>
4343

4444
`readonly`
4545

4646
</td><td>
4747

48-
[Product](./puppeteer.product.md)
48+
[SupportedBrowser](./puppeteer.supportedbrowser.md)
4949

5050
</td><td>
5151

@@ -69,7 +69,7 @@ Description
6969
</th></tr></thead>
7070
<tbody><tr><td>
7171

72-
<span id="defaultargs">[defaultArgs(object)](./puppeteer.productlauncher.defaultargs.md)</span>
72+
<span id="defaultargs">[defaultArgs(object)](./puppeteer.browserlauncher.defaultargs.md)</span>
7373

7474
</td><td>
7575

@@ -78,7 +78,7 @@ Description
7878
</td></tr>
7979
<tr><td>
8080

81-
<span id="executablepath">[executablePath(channel)](./puppeteer.productlauncher.executablepath.md)</span>
81+
<span id="executablepath">[executablePath(channel)](./puppeteer.browserlauncher.executablepath.md)</span>
8282

8383
</td><td>
8484

@@ -87,7 +87,7 @@ Description
8787
</td></tr>
8888
<tr><td>
8989

90-
<span id="launch">[launch(options)](./puppeteer.productlauncher.launch.md)</span>
90+
<span id="launch">[launch(options)](./puppeteer.browserlauncher.launch.md)</span>
9191

9292
</td><td>
9393

0 commit comments

Comments
 (0)
Please sign in to comment.