Skip to content

Commit 343a376

Browse files
committed
Streamline deno shims since not all are needed for distribution
- Deno.test and Deno.env are only used in tests - Deno.version is only available for deno environments and does not need to be shimmed - DOMException is only used in tests
1 parent 170254a commit 343a376

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ and this project adheres to
1212

1313
### Changed
1414

15+
- Exclude Deno and DOMException from distribution shim.
16+
- Refine retrieval of Node/Deno version.
17+
1518
### Fixed
1619

1720
### Removed

scripts/build_npm.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ await build({
88
rootTestDir: "./tests",
99
outDir: "./npm",
1010
shims: {
11-
deno: true, // Required for `Deno.test`, `Deno.env`, etc.
12-
domException: true, // https://deno.land/std/async/delay.ts relies on DOMException
11+
// Tests require `Deno.test` and `Deno.env`.
12+
// Although `Deno.version` is used, it doesn't need to be shimmed since
13+
// it won't be used when the user runs the module in Node.
14+
deno: "dev",
15+
16+
// https://deno.land/std/async/delay.ts relies on DOMException.
17+
// This is only used in tests.
18+
domException: "dev", // Only used in tests.
19+
1320
undici: true, // Required for `fetch`
1421
},
1522
package: {

src/utils.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,23 @@ function getSource() {
7777
const moduleSource = `serpapi@${version}`;
7878
try {
7979
// Check if running in Node.js
80+
// dnt-shim-ignore
8081
// deno-lint-ignore no-explicit-any
81-
if ((globalThis as any)?.process?.version) {
82-
// deno-lint-ignore no-explicit-any
83-
const nodeVersion = (globalThis as any).process.version.replace("v", "");
82+
const nodeVersion = (globalThis as any).process?.versions?.node;
83+
if (nodeVersion) {
8484
return `nodejs@${nodeVersion},${moduleSource}`;
8585
}
8686

87-
// Assumes running in Deno instead
88-
return `deno@${Deno.version.deno},${moduleSource}`;
87+
// Assumes running in Deno instead. https://deno.land/api?s=Deno.version
88+
// Deno.version is not shimmed since it's not used when ran in a Node env.
89+
// dnt-shim-ignore
90+
// deno-lint-ignore no-explicit-any
91+
const denoVersion = (globalThis as any).Deno?.version?.deno;
92+
if (denoVersion) {
93+
return `deno@${denoVersion},${moduleSource}`;
94+
}
95+
96+
return `nodejs,${moduleSource}`;
8997
} catch {
9098
// If something unexpectedly occurs, revert to "nodejs".
9199
return `nodejs,${moduleSource}`;

0 commit comments

Comments
 (0)