Skip to content

Commit

Permalink
Add support for Yarn Plug'n'Play filesystem layout #3888
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Dec 10, 2023
1 parent 004fff9 commit 1592f96
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

Requires libvips v8.15.0

### v0.33.1 - TBD

* Add support for Yarn Plug'n'Play filesystem layout.
[#3888](https://github.com/lovell/sharp/issues/3888)

### v0.33.0 - 29th November 2023

* Drop support for Node.js 14 and 16, now requires Node.js >= 18.17.0
Expand Down
2 changes: 0 additions & 2 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pnpm add sharp
```

```sh
# yarn v3+ (Plug'n'Play is unsupported)
yarn config set nodeLinker node-modules
yarn add sharp
```

Expand Down
12 changes: 11 additions & 1 deletion lib/libvips.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
'use strict';

const { spawnSync } = require('node:child_process');
const { createHash } = require('node:crypto');
const semverCoerce = require('semver/functions/coerce');
const semverGreaterThanOrEqualTo = require('semver/functions/gte');
const detectLibc = require('detect-libc');

const { engines } = require('../package.json');
const { engines, optionalDependencies } = require('../package.json');

const minimumLibvipsVersionLabelled = process.env.npm_package_config_libvips || /* istanbul ignore next */
engines.libvips;
Expand Down Expand Up @@ -97,6 +98,14 @@ const isRosetta = () => {
return false;
};

const sha512 = (s) => createHash('sha512').update(s).digest('hex');

const yarnLocator = () => {
const identHash = sha512(`imgsharp-libvips-${buildPlatformArch()}`);
const npmVersion = semverCoerce(optionalDependencies[`@img/sharp-libvips-${buildPlatformArch()}`]).version;
return sha512(`${identHash}npm:${npmVersion}`).slice(0, 10);
};

/* istanbul ignore next */
const spawnRebuild = () =>
spawnSync(`node-gyp rebuild --directory=src ${isEmscripten() ? '--nodedir=emscripten' : ''}`, {
Expand Down Expand Up @@ -164,6 +173,7 @@ module.exports = {
buildSharpLibvipsLibDir,
runtimePlatformArch,
log,
yarnLocator,
spawnRebuild,
globalLibvipsVersion,
pkgConfigPath,
Expand Down
7 changes: 5 additions & 2 deletions src/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'vips_version': '<!(node -p "require(\'../lib/libvips\').minimumLibvipsVersion")',
'platform_and_arch': '<!(node -p "require(\'../lib/libvips\').buildPlatformArch()")',
'sharp_libvips_version': '<!(node -p "require(\'../package.json\').optionalDependencies[\'@img/sharp-libvips-<(platform_and_arch)\']")',
'sharp_libvips_yarn_locator': '<!(node -p "require(\'../lib/libvips\').yarnLocator()")',
'sharp_libvips_include_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsIncludeDir()")',
'sharp_libvips_cplusplus_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsCPlusPlusDir()")',
'sharp_libvips_lib_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsLibDir()")'
Expand Down Expand Up @@ -157,7 +158,8 @@
'-Wl,-rpath,\'@loader_path/../../sharp-libvips-<(platform_and_arch)/lib\'',
'-Wl,-rpath,\'@loader_path/../../../sharp-libvips-<(platform_and_arch)/<(sharp_libvips_version)/lib\'',
'-Wl,-rpath,\'@loader_path/../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'',
'-Wl,-rpath,\'@loader_path/../../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\''
'-Wl,-rpath,\'@loader_path/../../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'',
'-Wl,-rpath,\'@loader_path/../../../../../@img-sharp-libvips-<(platform_and_arch)-npm-<(sharp_libvips_version)-<(sharp_libvips_yarn_locator)/node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\''
]
}
}],
Expand All @@ -176,7 +178,8 @@
'-Wl,-rpath=\'$$ORIGIN/../../sharp-libvips-<(platform_and_arch)/lib\'',
'-Wl,-rpath=\'$$ORIGIN/../../../sharp-libvips-<(platform_and_arch)/<(sharp_libvips_version)/lib\'',
'-Wl,-rpath=\'$$ORIGIN/../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'',
'-Wl,-rpath=\'$$ORIGIN/../../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\''
'-Wl,-rpath=\'$$ORIGIN/../../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'',
'-Wl,-rpath,\'$$ORIGIN/../../../../../@img-sharp-libvips-<(platform_and_arch)-npm-<(sharp_libvips_version)-<(sharp_libvips_yarn_locator)/node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\''
]
}
}],
Expand Down
6 changes: 6 additions & 0 deletions test/unit/libvips.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,10 @@ describe('libvips binaries', function () {
libvips.log(new Error('problem'));
});
});

it('yarn locator hash', () => {
const locatorHash = libvips.yarnLocator();
assert.strictEqual(locatorHash.length, 10);
assert.strict(/[a-f0-9]{10}/.test(locatorHash));
});
});

0 comments on commit 1592f96

Please sign in to comment.