From b59b171e43a170c0ccf84d36ab4ba2db403e25c5 Mon Sep 17 00:00:00 2001 From: Peter Makra <6892971+mcraa@users.noreply.github.com> Date: Mon, 19 Dec 2022 21:27:17 +0100 Subject: [PATCH] patch: handle ext2fs with webpack --- webpack.config.ts | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/webpack.config.ts b/webpack.config.ts index 548c95f8..59776e7d 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -15,7 +15,7 @@ */ import * as CopyPlugin from 'copy-webpack-plugin'; -import { readdirSync } from 'fs'; +import { readdirSync, existsSync } from 'fs'; import * as _ from 'lodash'; import * as os from 'os'; import outdent from 'outdent'; @@ -77,6 +77,27 @@ function renameNodeModules(resourcePath: string) { ); } +function findExt2fsFolder(): string { + const ext2fs = 'node_modules/ext2fs'; + const biFsExt2fs = 'node_modules/balena-image-fs/node_modules/ext2fs'; + + if (existsSync(ext2fs)) { + return ext2fs; + } else if (existsSync(biFsExt2fs)) { + return biFsExt2fs; + } else { + throw Error('ext2fs not found'); + } +} + +function makeExt2FsRegex(): RegExp { + const folder = findExt2fsFolder(); + const libpath = '/lib/libext2fs\\.js&'; + + const regex = folder.concat(libpath).split('/').join('/'); + return new RegExp(regex); +} + function findUsbPrebuild(): string[] { const usbPrebuildsFolder = path.join('node_modules', 'usb', 'prebuilds'); const prebuildFolders = readdirSync(usbPrebuildsFolder); @@ -152,6 +173,7 @@ function slashOrAntislash(pattern: RegExp): RegExp { } function replace(test: RegExp, ...replacements: ReplacementRule[]) { + console.log(test.source); return { loader: 'string-replace-loader', // Handle windows path separators @@ -322,7 +344,7 @@ const commonConfig = { // Use the libext2fs.wasm file in the generated folder // The way to find the app directory depends on whether we run in the renderer or in the child-writer // We use __dirname in the child-writer and electron.remote.app.getAppPath() in the renderer - replace(/node_modules\/ext2fs\/lib\/libext2fs\.js$/, { + replace(makeExt2FsRegex(), { search: 'scriptDirectory = __dirname + "/";', replace: fetchWasm('ext2fs', 'lib'), }), @@ -385,7 +407,7 @@ const guiConfigCopyPatterns = [ to: 'modules/node-raspberrypi-usbboot/blobs', }, { - from: 'node_modules/ext2fs/lib/libext2fs.wasm', + from: `${findExt2fsFolder()}/lib/libext2fs.wasm`, to: 'modules/ext2fs/lib/libext2fs.wasm', }, { @@ -394,6 +416,8 @@ const guiConfigCopyPatterns = [ }, ]; +console.log(guiConfigCopyPatterns); + if (os.platform() === 'win32') { // liblzma.dll is required on Windows for lzma-native guiConfigCopyPatterns.push({