Skip to content

Commit

Permalink
Updated paths for use with Webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Aug 29, 2024
1 parent 583daa5 commit a331240
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
24 changes: 21 additions & 3 deletions js/worker/generalWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,26 @@ let oemCurrent = 2;
let langArrCurrent = ['eng'];

let vanillaMode_ = false;
const corePath = vanillaMode_ ? '../tess/core_vanilla/' : '../tess/core/';

// Explicitly setting these paths with `URL` is necessary for this to work with Webpack.
// While Tesseract.js users are advised to always point `corePath` to a directory rather than a file,
// pointing to a file should be fined here.
// First, we never want to use the LSTM-only version, as every recognition mode (aside from some fringe advanced options) use the Legacy engine.
// Second, >99% of devices now support the SIMD version, so only using the SIMD version is fine.
let corePath;
if (vanillaMode_) {
corePath = new URL('../../tess/core_vanilla/tesseract-core-simd.wasm.js', import.meta.url).href;
} else {
corePath = new URL('../../tess/core/tesseract-core-simd.wasm.js', import.meta.url).href;
}

const workerPath = new URL('../../tess/worker.min.js', import.meta.url).href;

// Custom build is currently only used for browser version, while the Node.js version uses the published npm package.
// If recognition capabilities are ever added for the Node.js version, then we should use the same build for consistency. .
const tessConfig = typeof process === 'undefined' ? {
corePath,
workerPath: '../../tess/worker.min.js',
workerPath,
// langPath: '/tess/tessdata_dist',
legacyCore: true,
legacyLang: true,
Expand Down Expand Up @@ -100,7 +113,12 @@ const reinitialize = async ({ langs, oem, vanillaMode }) => {
// The worker only needs to be created from scratch if the build of Tesseract being used changes,
// or if it was never created in the first place.
if (changeVanilla || !worker) {
tessConfig.corePath = vanillaMode_ ? '../tess/core_vanilla/' : '../tess/core/';
if (vanillaMode_) {
tessConfig.corePath = new URL('../../tess/core_vanilla/tesseract-core-simd.wasm.js', import.meta.url).href;
} else {
tessConfig.corePath = new URL('../../tess/core/tesseract-core-simd.wasm.js', import.meta.url).href;
}

if (worker) await worker.terminate();
worker = await Tesseract.createWorker(langArrCurrent, oemCurrent, tessConfig, initConfigs);
} else {
Expand Down
10 changes: 6 additions & 4 deletions mupdf/libmupdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,13 @@ function findWasmBinary() {

var wasmBinaryFile;

function relToAbsPath(fileName) {
const url = new URL(fileName, import.meta.url);
return url.protocol == "file:" ? url.host + url.pathname : url.href;
const wasmBinaryFileURL = new URL("./libmupdf.wasm", import.meta.url);
if (wasmBinaryFileURL.protocol === "file:") {
if (!(typeof process === "undefined")) wasmBinaryFile = wasmBinaryFileURL.pathname;
} else {
wasmBinaryFile = wasmBinaryFileURL.href;
}
wasmBinaryFile = relToAbsPath("./libmupdf.wasm");

function getBinarySync(file) {
if (file == wasmBinaryFile && wasmBinary) {
return new Uint8Array(wasmBinary);
Expand Down

0 comments on commit a331240

Please sign in to comment.