diff --git a/index.js b/index.js index 8afb2b1126dcc687b7ff9b631589da252c1f9c22..398af238a439912d150b3573367873d2a9a311e3 100644 --- a/index.js +++ b/index.js @@ -1,41 +1,34 @@ -const {createWrapper} = require('./wrapper'); +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') { + if (process.env.PLATFORM_LIBC === "musl") { + return require(`@parcel/watcher-${process.platform}-${process.arch}-musl`) + } else if (process.env.PLATFORM_LIBC === "glibc") { + return require(`@parcel/watcher-${process.platform}-${process.arch}-glibc`) + } else { + let { MUSL, GLIBC, family, familySync } = require('detect-libc') -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.`); - } - } -} + // 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() -function handleError(err) { - if (err?.code !== 'MODULE_NOT_FOUND') { - throw err; + 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}`) } } -const wrapper = createWrapper(binding); -exports.writeSnapshot = wrapper.writeSnapshot; -exports.getEventsSince = wrapper.getEventsSince; -exports.subscribe = wrapper.subscribe; -exports.unsubscribe = wrapper.unsubscribe; +const wrapper = createWrapper(loadPackage()) +exports.writeSnapshot = wrapper.writeSnapshot +exports.getEventsSince = wrapper.getEventsSince +exports.subscribe = wrapper.subscribe +exports.unsubscribe = wrapper.unsubscribe