@@ -165,7 +165,6 @@ async function main(args) {
165
165
166
166
// Create .bin entry point files for all packages in node_modules
167
167
await makeBins ( nodeModulesPath , '' , segmentsUp )
168
-
169
168
// export interface RunLifecycleHookOptions {
170
169
// args?: string[];
171
170
// depPath: string;
@@ -183,6 +182,29 @@ async function main(args) {
183
182
// stdio?: string;
184
183
// unsafePerm: boolean;
185
184
// }
185
+
186
+ // We need to explicitly pass `npm_config_` prefixed env-variables as configuration to the lifecycle hook (or gyp).
187
+ // 1. rules_js allow to provide per action_type environment using `lifecycle_hooks_envs`.
188
+ // 2. One of the important use-cases is to able provide mirror where prebuild binaries are stored:
189
+ // (see: https://github.com/mapbox/node-pre-gyp#download-binary-files-from-a-mirror
190
+ // by {module_name}_binary_host_mirror)
191
+ // 3. Such flags are taken (by gyp) from environment variables:
192
+ // https://github.com/mapbox/node-pre-gyp/blob/a74f5e367c0d71033620aa0112e7baf7f3515b9d/lib/util/versioning.js#L316
193
+ // 4. Unfortunetely pnpm/lifecycle drops all npm_ prefixed env-variables prior to calling lifecycle hook:
194
+ // https://github.com/pnpm/npm-lifecycle/blob/99ac0429025bdf1303879723d3fbd57c585ae8a1/index.js#L351
195
+ // and later recreates it based on explicitly given config:
196
+ // https://github.com/pnpm/npm-lifecycle/blob/99ac0429025bdf1303879723d3fbd57c585ae8a1/index.js#L408
197
+ // 5. So we need to perform reversed process: generate rawConfig based on env-variables to preserve them.
198
+ let inherited_env = { }
199
+ const npm_config_prefix = 'npm_config_'
200
+ const config_regexp = new RegExp ( '^' + npm_config_prefix , 'i' )
201
+ for ( let e in process . env ) {
202
+ if ( e . match ( config_regexp ) ) {
203
+ inherited_env [ e . substring ( npm_config_prefix . length ) ] =
204
+ process . env [ e ]
205
+ }
206
+ }
207
+
186
208
const opts = {
187
209
pkgRoot : path . resolve ( outputDir ) ,
188
210
@@ -196,11 +218,15 @@ async function main(args) {
196
218
// https://yarnpkg.com/package?name=prebuild-install:
197
219
// "... you can set environment variables npm_config_build_from_source=true, npm_config_platform,
198
220
// npm_config_arch, npm_config_target npm_config_runtime and npm_config_libc".
199
- rawConfig : {
200
- stdio : 'inherit' ,
201
- platform : platform ,
202
- arch : arch ,
203
- } ,
221
+ rawConfig : Object . assign (
222
+ { } ,
223
+ {
224
+ stdio : 'inherit' ,
225
+ platform : platform ,
226
+ arch : arch ,
227
+ } ,
228
+ inherited_env
229
+ ) ,
204
230
silent : false ,
205
231
stdio : 'inherit' ,
206
232
rootModulesDir : nodeModulesPath ,
0 commit comments