Skip to content

Commit

Permalink
adding an index and wasm files to initialize the module
Browse files Browse the repository at this point in the history
this maintains API compatibility with the versions that used to be compiled with the older emscripten
  • Loading branch information
catdad committed Nov 5, 2023
1 parent 7f87c9d commit 554a1df
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./libheif/libheif.js')();
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "libheif-js",
"version": "1.15.1",
"version": "1.17.1",
"description": "Emscripten distribution of libheif for Node.JS and the browser",
"main": "libheif/libheif.js",
"main": "index.js",
"scripts": {
"pretest": "npm run -s images",
"test": "mocha test/**/*.test.js",
Expand Down
33 changes: 29 additions & 4 deletions test/libheif.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,34 @@ const { PNG } = require('pngjs');
const pixelmatch = require('pixelmatch');

const pkg = require('../package.json');
const libheif = require('../');

describe('libheif', () => {
describe('libheif (JS)', () => {
const moduleFile = '../';

runTests(require(moduleFile));

it('resolves to index file', () => {
const expected = path.resolve(root, 'index.js');
const actual = require.resolve(moduleFile);

expect(actual).to.equal(expected);
});
});

describe('libheif (WASM)', () => {
const moduleFile = '../wasm';

runTests(require(moduleFile));

it('resolves to wasm file', () => {
const expected = path.resolve(root, 'wasm.js');
const actual = require.resolve(moduleFile);

expect(actual).to.equal(expected);
});
});

function runTests(libheif) {
const readControl = async name => {
const buffer = await fs.readFile(path.resolve(root, `temp/${name}`));
const { data, width, height } = PNG.sync.read(buffer);
Expand All @@ -30,7 +55,7 @@ describe('libheif', () => {
it('is the correct version', () => {
expect(libheif).to.have.property('heif_get_version')
.and.to.be.a('function');
expect(libheif.heif_get_version()).to.equal('1.15.1')
expect(libheif.heif_get_version()).to.equal('1.17.1')
.and.to.equal(pkg.version);
});

Expand Down Expand Up @@ -85,4 +110,4 @@ describe('libheif', () => {
compare(control.data, arrayBuffer, control.width, control.height);
});
});
});
}
30 changes: 30 additions & 0 deletions wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// I technically don't have to do this, but I am keeping it around
// for demonstration purposes
const fs = require('fs');
const wasmBinary = fs.readFileSync('./libheif-wasm/libheif.wasm');

module.exports = require('./libheif-wasm/libheif.js')({ wasmBinary });

// NOTE: for webpack, you need to do something like:
/*
import libheif from './libheif-wasm/libheif.js'
import wasmBinary from './libheif-wasm/libheif.wasm'
export default libheif({ wasmBinary });
*/

// then add this to rules:
/*
module.exports = {
module: {
rules: [{
test: /\.wasm$/,
type: "asset/inline",
}]
},
};
*/

0 comments on commit 554a1df

Please sign in to comment.