fix(npm): improved optional dependency support (#19135)

Note: If the package information has already been cached, then this
requires running with `--reload` or for the registry information to be
fetched some other way (ex. the cache busting).

Closes #15544

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
David Sherret 2023-05-17 17:38:50 -04:00 committed by GitHub
parent ad22336245
commit 41f618a1df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 300 additions and 61 deletions

View file

@ -0,0 +1 @@
import "npm:@denotest/binary-package";

View file

@ -0,0 +1 @@
console.log("Hello from binary package on linux");

View file

@ -0,0 +1,8 @@
{
"name": "@denotest/binary-package-linux",
"version": "1.0.0",
"main": "index.js",
"os": [
"linux"
]
}

View file

@ -0,0 +1 @@
console.log("Hello from binary package on mac");

View file

@ -0,0 +1,8 @@
{
"name": "@denotest/binary-package-linux",
"version": "1.0.0",
"main": "index.js",
"os": [
"darwin"
]
}

View file

@ -0,0 +1 @@
console.log("Hello from binary package on windows");

View file

@ -0,0 +1,8 @@
{
"name": "@denotest/binary-package-windows",
"version": "1.0.0",
"main": "index.js",
"os": [
"win32"
]
}

View file

@ -0,0 +1,13 @@
const packageByOs = {
"darwin": "@denotest/binary-package-mac",
"linux": "@denotest/binary-package-linux",
"win32": "@denotest/binary-package-windows",
}
const selectedPackage = packageByOs[process.platform];
if (!selectedPackage) {
throw new Error("trying to run on unsupported platform");
}
require(selectedPackage);

View file

@ -0,0 +1,10 @@
{
"name": "@denotest/binary-package",
"version": "1.0.0",
"main": "index.js",
"optionalDependencies": {
"@denotest/binary-package-linux": "1.0.0",
"@denotest/binary-package-mac": "1.0.0",
"@denotest/binary-package-windows": "1.0.0"
}
}