Skip to content

Commit 2a046e4

Browse files
authored
fix: run prebuild-install only if actually used by the package (#83)
1 parent 2f27414 commit 2a046e4

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

lib/producer.ts

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

200+
function getPrebuildEnvPrefix(pkgName: string): string {
201+
return `npm_config_${(pkgName || '')
202+
.replace(/[^a-zA-Z0-9]/g, '_')
203+
.replace(/^_/, '')}`;
204+
}
205+
200206
function nativePrebuildInstall(target: Target, nodeFile: string) {
201207
const prebuildInstall = path.join(
202208
__dirname,
203209
'../node_modules/.bin/prebuild-install',
204210
);
205211
const dir = findPackageJson(nodeFile);
212+
const packageJson = JSON.parse(
213+
fs.readFileSync(path.join(dir, 'package.json'), { encoding: 'utf-8' }),
214+
);
215+
216+
// only try prebuild-install for packages that actually use it or if
217+
// explicitly configured via environment variables
218+
const envPrefix = getPrebuildEnvPrefix(packageJson.name);
219+
if (
220+
packageJson.dependencies?.['prebuild-install'] == null &&
221+
![
222+
`${envPrefix}_binary_host`,
223+
`${envPrefix}_binary_host_mirror`,
224+
`${envPrefix}_local_prebuilds`,
225+
].some((i) => i in process.env)
226+
) {
227+
return;
228+
}
229+
206230
// parse the target node version from the binaryPath
207231
const nodeVersion = path.basename(target.binaryPath).split('-')[1];
208232

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

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

228250
const options = [
229251
'--platform',
@@ -461,7 +483,7 @@ export default function producer({
461483
try {
462484
const platformFile = nativePrebuildInstall(target, stripe.file);
463485

464-
if (fs.existsSync(platformFile)) {
486+
if (platformFile && fs.existsSync(platformFile)) {
465487
return cb(
466488
null,
467489
pipeMayCompressToNewMeter(

0 commit comments

Comments
 (0)