diff --git a/index.js b/index.js index 8afb2b1126dcc687b7ff9b631589da252c1f9c22..97975cf342ff8c204b5731840a89b8ea88b0dbbc 100644 --- a/index.js +++ b/index.js @@ -1,40 +1,26 @@ const {createWrapper} = require('./wrapper'); -let name = `@parcel/watcher-${process.platform}-${process.arch}`; -if (process.platform === 'linux') { - const { MUSL, family } = require('detect-libc'); - if (family === MUSL) { - name += '-musl'; - } else { - name += '-glibc'; - } -} +function loadPackage() { + if (process.platform === 'linux') { + let { MUSL, GLIBC, family, familySync } = require("detect-libc"); + // Bun polyfills `detect-libc` in compiled binaries. We rely on + // detect-libc@1.0.3 but the polyfilled version is 2.x. In detect-libc@2x + // there is a `familySync` function that we can use instead. + if (typeof familySync === 'function') family = familySync() -let binding; -try { - binding = require(name); -} catch (err) { - handleError(err); - try { - binding = require('./build/Release/watcher.node'); - } catch (err) { - handleError(err); - try { - binding = require('./build/Debug/watcher.node'); - } catch (err) { - handleError(err); - throw new Error(`No prebuild or local build of @parcel/watcher found. Tried ${name}. Please ensure it is installed (don't use --no-optional when installing with npm). Otherwise it is possible we don't support your platform yet. If this is the case, please report an issue to https://github.com/parcel-bundler/watcher.`); + if (family === MUSL) { + return require(`@parcel/watcher-${process.platform}-${process.arch}-musl`); + } else if (family === GLIBC) { + return require(`@parcel/watcher-${process.platform}-${process.arch}-glibc`); + } else { + throw new Error(`Unsupported libc on: ${process.platform}-${process.arch}`); } + } else { + return require(`@parcel/watcher-${process.platform}-${process.arch}`); } } -function handleError(err) { - if (err?.code !== 'MODULE_NOT_FOUND') { - throw err; - } -} - -const wrapper = createWrapper(binding); +const wrapper = createWrapper(loadPackage()); exports.writeSnapshot = wrapper.writeSnapshot; exports.getEventsSince = wrapper.getEventsSince; exports.subscribe = wrapper.subscribe;