Skip to content

Commit de2c6c2

Browse files
committed
fix(build): break node-gyp string to prevent bundler issues
Add post-processing step to break node-gyp require.resolve strings in bundled external packages. This prevents consumers trying to bundle socket-lib from encountering issues with node-gyp's non-JS files (.cs, .gyp). The transformation changes: require.resolve("node-gyp/bin/node-gyp.js") to: require.resolve("node-" + "gyp/bin/node-gyp.js") This bypasses static analysis in bundlers while maintaining runtime behavior.
1 parent 3cb88fb commit de2c6c2

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

scripts/build-externals/orchestrator.mjs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,52 @@ export async function buildExternals(options = {}) {
175175
quiet: quiet || !showDetails,
176176
})
177177

178+
// Post-process: Fix node-gyp strings to prevent bundler issues for consumers
179+
await fixNodeGypStrings(distExternalDir, { quiet })
180+
178181
return { bundledCount, totalSize }
179182
}
183+
184+
/**
185+
* Post-process bundled files to break node-gyp require.resolve strings.
186+
* This prevents consumers trying to bundle socket-lib from having issues with node-gyp.
187+
*
188+
* @param {string} dir - Directory to process
189+
* @param {object} options - Options
190+
* @param {boolean} options.quiet - Suppress output
191+
*/
192+
async function fixNodeGypStrings(dir, options = {}) {
193+
const { quiet = false } = options
194+
195+
// Find all .js files in dist/external
196+
const files = await fs.readdir(dir, { withFileTypes: true })
197+
198+
for (const file of files) {
199+
const filePath = path.join(dir, file.name)
200+
201+
if (file.isDirectory()) {
202+
// Recursively process subdirectories
203+
await fixNodeGypStrings(filePath, options)
204+
} else if (file.name.endsWith('.js')) {
205+
// Read file contents
206+
const contents = await fs.readFile(filePath, 'utf8')
207+
208+
// Check if file contains the problematic pattern
209+
if (contents.includes('node-gyp/bin/node-gyp.js')) {
210+
// Replace literal string with concatenated version
211+
const fixed = contents.replace(
212+
/require\.resolve\(["']node-gyp\/bin\/node-gyp\.js["']\)/g,
213+
'require.resolve("node-" + "gyp/bin/node-gyp.js")',
214+
)
215+
216+
await fs.writeFile(filePath, fixed, 'utf8')
217+
218+
if (!quiet) {
219+
console.log(
220+
` Fixed node-gyp string in ${path.relative(path.join(dir, '..', '..'), filePath)}`,
221+
)
222+
}
223+
}
224+
}
225+
}
226+
}

0 commit comments

Comments
 (0)