From 66bf5dfe4ee97732b0d11ec1ed8c73c410fa3aab Mon Sep 17 00:00:00 2001 From: Kiril Vatev Date: Mon, 7 Oct 2024 01:43:35 -0400 Subject: [PATCH] compiling `fs` and `path` modules out of the wasm bundle versions --- package.json | 1 + scripts/install.js | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index c93bf43..19be8a2 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "babel-eslint": "^10.1.0", "chai": "^4.2.0", "esbuild": "^0.19.5", + "esbuild-plugins-node-modules-polyfill": "^1.6.7", "eslint": "^5.16.0", "fs-extra": "^8.1.0", "gunzip-maybe": "^1.4.2", diff --git a/scripts/install.js b/scripts/install.js index 1909ff1..1de6993 100644 --- a/scripts/install.js +++ b/scripts/install.js @@ -7,6 +7,7 @@ const tar = require('tar-stream'); const gunzip = require('gunzip-maybe'); const esbuild = require('esbuild'); +const { nodeModulesPolyfillPlugin } = require('esbuild-plugins-node-modules-polyfill'); const version = 'v1.18.2'; @@ -79,6 +80,15 @@ const autoReadStream = async stream => { platform: 'neutral' }; + const plugins = () => [ + nodeModulesPolyfillPlugin({ + modules: { + fs: 'empty', + path: 'empty' + } + }) + ]; + await esbuild.build({ ...buildOptions, outfile: path.resolve(root, 'libheif-wasm/libheif-bundle.js'), @@ -92,7 +102,8 @@ libheif = libheif.default; if (typeof exports === 'object' && typeof module === 'object') { module.exports = libheif; }` - } + }, + plugins: plugins(), }); await esbuild.build({ @@ -102,8 +113,9 @@ if (typeof exports === 'object' && typeof module === 'object') { banner: { // hack to avoid the ENVIRONMENT_IS_NODE detection // the binary is built in, so the environment doesn't matter - js: 'var process;' - } + js: 'var process, __dirname;' + }, + plugins: plugins(), }); })().then(() => { console.log(`fetched libheif ${version}`);