Add infrastructure to build and publish as wasm32 (#3840)

Co-authored-by: Ingvar Stepanyan <me@rreverser.com>
This commit is contained in:
Lovell Fuller
2023-11-09 14:46:07 +00:00
committed by GitHub
parent 475bf16b09
commit a8f68ba7f0
17 changed files with 241 additions and 20 deletions

View File

@@ -41,15 +41,23 @@ const runtimePlatformArch = () => `${process.platform}${runtimeLibc()}-${process
/* istanbul ignore next */
const buildPlatformArch = () => {
if (isEmscripten()) {
return 'wasm32';
}
/* eslint camelcase: ["error", { allow: ["^npm_config_"] }] */
const { npm_config_arch, npm_config_platform, npm_config_libc } = process.env;
return `${npm_config_platform || process.platform}${npm_config_libc || runtimeLibc()}-${npm_config_arch || process.arch}`;
const libc = typeof npm_config_libc === 'string' ? npm_config_libc : runtimeLibc();
return `${npm_config_platform || process.platform}${libc}-${npm_config_arch || process.arch}`;
};
const buildSharpLibvipsIncludeDir = () => {
try {
return require('@img/sharp-libvips-dev/include');
} catch {}
return require(`@img/sharp-libvips-dev-${buildPlatformArch()}/include`);
} catch {
try {
return require('@img/sharp-libvips-dev/include');
} catch {}
}
/* istanbul ignore next */
return '';
};
@@ -64,12 +72,22 @@ const buildSharpLibvipsCPlusPlusDir = () => {
const buildSharpLibvipsLibDir = () => {
try {
return require(`@img/sharp-libvips-${buildPlatformArch()}/lib`);
} catch {}
return require(`@img/sharp-libvips-dev-${buildPlatformArch()}/lib`);
} catch {
try {
return require(`@img/sharp-libvips-${buildPlatformArch()}/lib`);
} catch {}
}
/* istanbul ignore next */
return '';
};
/* istanbul ignore next */
const isEmscripten = () => {
const { CC } = process.env;
return Boolean(CC && CC.endsWith('/emcc'));
};
const isRosetta = () => {
/* istanbul ignore next */
if (process.platform === 'darwin' && process.arch === 'x64') {
@@ -81,7 +99,7 @@ const isRosetta = () => {
/* istanbul ignore next */
const spawnRebuild = () =>
spawnSync('node-gyp rebuild --directory=src', {
spawnSync(`node-gyp rebuild --directory=src ${isEmscripten() ? '--nodedir=emscripten' : ''}`, {
...spawnSyncOptions,
stdio: 'inherit'
}).status;

View File

@@ -12,7 +12,9 @@ const runtimePlatform = runtimePlatformArch();
const paths = [
`../src/build/Release/sharp-${runtimePlatform}.node`,
`@img/sharp-${runtimePlatform}/sharp.node`
'../src/build/Release/sharp-wasm32.node',
`@img/sharp-${runtimePlatform}/sharp.node`,
'@img/sharp-wasm32/sharp.node'
];
const errors = [];
@@ -47,6 +49,8 @@ if (!module.exports) {
help.push(` npm install --force @img/sharp-${runtimePlatform}`);
} else {
help.push(`- Manually install libvips >= ${minimumLibvipsVersion}`);
help.push('- Add experimental WebAssembly-based dependencies:');
help.push(' npm install --cpu=wasm32 sharp');
}
if (isLinux && /symbol not found/i.test(messages)) {
try {

View File

@@ -59,11 +59,17 @@ let versions = {
};
/* istanbul ignore next */
if (!libvipsVersion.isGlobal) {
try {
versions = require(`@img/sharp-${runtimePlatform}/versions`);
} catch (_) {
if (!libvipsVersion.isWasm) {
try {
versions = require(`@img/sharp-libvips-${runtimePlatform}/versions`);
versions = require(`@img/sharp-${runtimePlatform}/versions`);
} catch (_) {
try {
versions = require(`@img/sharp-libvips-${runtimePlatform}/versions`);
} catch (_) {}
}
} else {
try {
versions = require('@img/sharp-wasm32/versions');
} catch (_) {}
}
}