Skip to content

Commit

Permalink
fix: run prebuild-install only if actually used by the package (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
segevfiner authored Aug 14, 2024
1 parent 2f27414 commit 2a046e4
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lib/producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,36 @@ function findPackageJson(nodeFile: string) {
return dir;
}

function getPrebuildEnvPrefix(pkgName: string): string {
return `npm_config_${(pkgName || '')
.replace(/[^a-zA-Z0-9]/g, '_')
.replace(/^_/, '')}`;
}

function nativePrebuildInstall(target: Target, nodeFile: string) {
const prebuildInstall = path.join(
__dirname,
'../node_modules/.bin/prebuild-install',
);
const dir = findPackageJson(nodeFile);
const packageJson = JSON.parse(
fs.readFileSync(path.join(dir, 'package.json'), { encoding: 'utf-8' }),
);

// only try prebuild-install for packages that actually use it or if
// explicitly configured via environment variables
const envPrefix = getPrebuildEnvPrefix(packageJson.name);
if (
packageJson.dependencies?.['prebuild-install'] == null &&
![
`${envPrefix}_binary_host`,
`${envPrefix}_binary_host_mirror`,
`${envPrefix}_local_prebuilds`,
].some((i) => i in process.env)
) {
return;
}

// parse the target node version from the binaryPath
const nodeVersion = path.basename(target.binaryPath).split('-')[1];

Expand All @@ -221,9 +245,7 @@ function nativePrebuildInstall(target: Target, nodeFile: string) {
fs.copyFileSync(nodeFile, `${nodeFile}.bak`);
}

const napiVersions = JSON.parse(
fs.readFileSync(path.join(dir, 'package.json'), { encoding: 'utf-8' }),
)?.binary?.napi_versions;
const napiVersions = packageJson?.binary?.napi_versions;

const options = [
'--platform',
Expand Down Expand Up @@ -461,7 +483,7 @@ export default function producer({
try {
const platformFile = nativePrebuildInstall(target, stripe.file);

if (fs.existsSync(platformFile)) {
if (platformFile && fs.existsSync(platformFile)) {
return cb(
null,
pipeMayCompressToNewMeter(
Expand Down

0 comments on commit 2a046e4

Please # to comment.