Skip to content

Commit 57bfc20

Browse files
committed
Only run prebuild-install if actually used by the package
1 parent 2f27414 commit 57bfc20

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lib/producer.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,29 @@ function findPackageJson(nodeFile: string) {
197197
return dir;
198198
}
199199

200+
function getPrebuildEnvPrefix(pkgName: string): string {
201+
return `npm_config_${ (pkgName || '').replace(/[^a-zA-Z0-9]/g, '_').replace(/^_/, '')}`
202+
}
203+
200204
function nativePrebuildInstall(target: Target, nodeFile: string) {
201205
const prebuildInstall = path.join(
202206
__dirname,
203207
'../node_modules/.bin/prebuild-install',
204208
);
205209
const dir = findPackageJson(nodeFile);
210+
const packageJson = JSON.parse(
211+
fs.readFileSync(path.join(dir, 'package.json'), { encoding: 'utf-8' }));
212+
213+
// only try prebuild-install for packages that actually use it or if
214+
// explicitly configured via environment variables
215+
const envPrefix = getPrebuildEnvPrefix(packageJson.name);
216+
if (packageJson.dependencies?.['prebuild-install'] == null &&
217+
!([`${envPrefix}_binary_host`,
218+
`${envPrefix}_binary_host_mirror`,
219+
`${envPrefix}_local_prebuilds`].some((i) => i in process.env))) {
220+
return;
221+
}
222+
206223
// parse the target node version from the binaryPath
207224
const nodeVersion = path.basename(target.binaryPath).split('-')[1];
208225

@@ -221,9 +238,7 @@ function nativePrebuildInstall(target: Target, nodeFile: string) {
221238
fs.copyFileSync(nodeFile, `${nodeFile}.bak`);
222239
}
223240

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

228243
const options = [
229244
'--platform',

0 commit comments

Comments
 (0)