Building from source now requires node-addon-api in dependencies

If you want to build sharp from source against a globally-installed
libvips then you will now need to add both node-addon-api and
node-gyp to the dependencies section of your package.json file.

The binding.gyp file is "hidden" inside the src directory to
prevent various build and package manager tooling from assuming
that everyone is going to build from source every time.
This commit is contained in:
Lovell Fuller 2023-10-08 15:38:59 +01:00
parent 51e56f994c
commit f7da2e5970
9 changed files with 48 additions and 29 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
build
src/build
src/node_modules
node_modules
/coverage
npm/*/*

View File

@ -8,6 +8,10 @@ Requires libvips v8.14.5
* Drop support for Node.js 14 and 16, now requires Node.js >= 18.17.0
* Prebuilt binaries distributed via npm registry and installed via package manager.
* Building from source requires dependency on `node-addon-api`.
* Remove `sharp.vendor`.
* Make `compression` option of `heif` mandatory to help reduce HEIF vs HEIC confusion.

View File

@ -53,9 +53,15 @@ This module will be compiled from source at `npm install` time when:
Building from source requires:
* C++11 compiler
* [node-addon-api](https://www.npmjs.com/package/node-addon-api)
* [node-gyp](https://github.com/nodejs/node-gyp#installation) and its dependencies
If `node-gyp` cannot be found, try adding it to `devDependencies`.
There is an install-time check for these dependencies.
If `node-addon-api` or `node-gyp` cannot be found, try adding them via:
```sh
npm install --save node-addon-api node-gyp
```
For cross-compiling, the `--platform`, `--arch` and `--libc` npm flags
(or the `npm_config_platform`, `npm_config_arch` and `npm_config_libc` environment variables)

View File

@ -3,18 +3,27 @@
'use strict';
const { useGlobalLibvips, globalLibvipsVersion, log, gypRebuild } = require('../lib/libvips');
const { useGlobalLibvips, globalLibvipsVersion, log, spawnRebuild } = require('../lib/libvips');
const buildFromSource = (msg) => {
log(msg);
log('Attempting to build from source via node-gyp');
try {
require('node-gyp');
require('node-addon-api');
log('Found node-addon-api');
} catch (err) {
log('You might need to install node-gyp');
log('Please add node-addon-api to your dependencies');
return;
}
try {
const gyp = require('node-gyp');
log(`Found node-gyp version ${gyp().version}`);
} catch (err) {
log('Please add node-gyp to your dependencies');
return;
}
log('See https://sharp.pixelplumbing.com/install#building-from-source');
const status = gypRebuild();
const status = spawnRebuild();
if (status !== 0) {
process.exit(status);
}

View File

@ -80,8 +80,8 @@ const isRosetta = () => {
};
/* istanbul ignore next */
const gypRebuild = () =>
spawnSync('node-gyp rebuild', {
const spawnRebuild = () =>
spawnSync('node-gyp rebuild --directory=src', {
...spawnSyncOptions,
stdio: 'inherit'
}).status;
@ -146,7 +146,7 @@ module.exports = {
buildSharpLibvipsLibDir,
runtimePlatformArch,
log,
gypRebuild,
spawnRebuild,
globalLibvipsVersion,
pkgConfigPath,
useGlobalLibvips

View File

@ -14,7 +14,7 @@ const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => run
/* istanbul ignore next */
try {
// Check for local build
module.exports = require(`../build/Release/sharp-${runtimePlatform}.node`);
module.exports = require(`../src/build/Release/sharp-${runtimePlatform}.node`);
} catch (errLocal) {
try {
// Check for runtime package

View File

@ -14,7 +14,7 @@ const platform = buildPlatformArch();
const dest = path.join(__dirname, platform);
// Use same config as prebuild to copy binary files
const release = path.join(__dirname, '..', 'build', 'Release');
const release = path.join(__dirname, '..', 'src', 'build', 'Release');
const prebuildrc = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '.prebuildrc'), 'utf8'));
const include = new RegExp(prebuildrc['include-regex'], 'i');
fs.cpSync(release, path.join(dest, 'lib'), {

View File

@ -90,7 +90,7 @@
],
"scripts": {
"install": "node install/check",
"clean": "rm -rf build/ .nyc_output/ coverage/ test/fixtures/output.*",
"clean": "rm -rf src/build/ .nyc_output/ coverage/ test/fixtures/output.*",
"test": "npm run test-lint && npm run test-unit && npm run test-licensing && npm run test-types",
"test-lint": "semistandard && cpplint",
"test-unit": "nyc --reporter=lcov --reporter=text --check-coverage --branches=100 mocha",
@ -107,10 +107,9 @@
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"binding.gyp",
"install",
"lib",
"src"
"src/*.{cc,h,gyp}"
],
"repository": {
"type": "git",
@ -137,7 +136,6 @@
"dependencies": {
"color": "^4.2.3",
"detect-libc": "^2.0.2",
"node-addon-api": "^7.0.0",
"semver": "^7.5.4"
},
"optionalDependencies": {
@ -171,6 +169,7 @@
"jsdoc-to-markdown": "^8.0.0",
"license-checker": "^25.0.1",
"mocha": "^10.2.0",
"node-addon-api": "^7.0.0",
"nyc": "^15.1.0",
"prebuild": "^12.1.0",
"semistandard": "^17.0.0",

View File

@ -3,11 +3,11 @@
{
'variables': {
'vips_version': '<!(node -p "require(\'./lib/libvips\').minimumLibvipsVersion")',
'platform_and_arch': '<!(node -p "require(\'./lib/libvips\').buildPlatformArch()")',
'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()")'
'vips_version': '<!(node -p "require(\'../lib/libvips\').minimumLibvipsVersion")',
'platform_and_arch': '<!(node -p "require(\'../lib/libvips\').buildPlatformArch()")',
'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()")'
},
'targets': [{
'target_name': 'libvips-cpp',
@ -86,8 +86,8 @@
'variables': {
'conditions': [
['OS != "win"', {
'pkg_config_path': '<!(node -p "require(\'./lib/libvips\').pkgConfigPath()")',
'use_global_libvips': '<!(node -p "Boolean(require(\'./lib/libvips\').useGlobalLibvips()).toString()")'
'pkg_config_path': '<!(node -p "require(\'../lib/libvips\').pkgConfigPath()")',
'use_global_libvips': '<!(node -p "Boolean(require(\'../lib/libvips\').useGlobalLibvips()).toString()")'
}, {
'pkg_config_path': '',
'use_global_libvips': ''
@ -95,13 +95,13 @@
]
},
'sources': [
'src/common.cc',
'src/metadata.cc',
'src/stats.cc',
'src/operations.cc',
'src/pipeline.cc',
'src/utilities.cc',
'src/sharp.cc'
'common.cc',
'metadata.cc',
'stats.cc',
'operations.cc',
'pipeline.cc',
'utilities.cc',
'sharp.cc'
],
'include_dirs': [
'<!(node -p "require(\'node-addon-api\').include_dir")',