From 26d3aecbcc1f6d3461df5a086ef13ade4aca478c Mon Sep 17 00:00:00 2001 From: Jerome Wu Date: Sat, 25 May 2019 23:56:15 +0800 Subject: [PATCH] Revert "Bundle tesseract.js, worker.js and tesseract-core.js together" This reverts commit 936cfa6f4853c8964d5fb79d79a43aa5991aeefc. --- package-lock.json | 10 ---------- package.json | 3 +-- scripts/webpack.config.common.js | 3 --- scripts/webpack.config.dev.js | 10 ++++------ scripts/webpack.config.prod.js | 10 ++++------ src/browser/index.js | 22 +++++++++------------- src/browser/worker.js | 23 +++++++++-------------- 7 files changed, 27 insertions(+), 54 deletions(-) diff --git a/package-lock.json b/package-lock.json index b344a7b..1a2a3dd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9808,16 +9808,6 @@ "errno": "~0.1.7" } }, - "worker-loader": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/worker-loader/-/worker-loader-2.0.0.tgz", - "integrity": "sha512-tnvNp4K3KQOpfRnD20m8xltE3eWh89Ye+5oj7wXEEHKac1P4oZ6p9oTj8/8ExqoSBnk9nu5Pr4nKfQ1hn2APJw==", - "dev": true, - "requires": { - "loader-utils": "^1.0.0", - "schema-utils": "^0.4.0" - } - }, "wrap-ansi": { "version": "2.1.0", "resolved": "http://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", diff --git a/package.json b/package.json index 5474e33..d90ee44 100644 --- a/package.json +++ b/package.json @@ -44,8 +44,7 @@ "wait-on": "^3.2.0", "webpack": "^4.26.0", "webpack-cli": "^3.1.2", - "webpack-dev-middleware": "^3.4.0", - "worker-loader": "^2.0.0" + "webpack-dev-middleware": "^3.4.0" }, "dependencies": { "axios": "^0.18.0", diff --git a/scripts/webpack.config.common.js b/scripts/webpack.config.common.js index a6dd7d5..7f179bf 100644 --- a/scripts/webpack.config.common.js +++ b/scripts/webpack.config.common.js @@ -20,7 +20,4 @@ module.exports = { }, ], }, - node: { - fs: 'empty', - }, }; diff --git a/scripts/webpack.config.dev.js b/scripts/webpack.config.dev.js index 9efa913..8729674 100644 --- a/scripts/webpack.config.dev.js +++ b/scripts/webpack.config.dev.js @@ -29,10 +29,8 @@ module.exports = [ library: 'Tesseract', libraryTarget: 'umd', }), - /* - *genConfig({ - * entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'), - * filename: 'worker.dev.js', - *}), - */ + genConfig({ + entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'), + filename: 'worker.dev.js', + }), ]; diff --git a/scripts/webpack.config.prod.js b/scripts/webpack.config.prod.js index b088f3f..15af617 100644 --- a/scripts/webpack.config.prod.js +++ b/scripts/webpack.config.prod.js @@ -23,10 +23,8 @@ module.exports = [ library: 'Tesseract', libraryTarget: 'umd', }), - /* - *genConfig({ - * entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'), - * filename: 'worker.min.js', - *}), - */ + genConfig({ + entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'), + filename: 'worker.min.js', + }), ]; diff --git a/src/browser/index.js b/src/browser/index.js index e8b4760..1be4ee3 100644 --- a/src/browser/index.js +++ b/src/browser/index.js @@ -10,8 +10,6 @@ const check = require('check-types'); const resolveURL = require('resolve-url'); const axios = require('axios'); -// eslint-disable-next-line -const TessWorker = require('worker-loader?inline=true&fallback=false!./worker'); const { defaultOptions } = require('../common/options'); const { version } = require('../../package.json'); @@ -121,17 +119,15 @@ exports.defaultOptions = { * @param {string} options.workerPath - worker script path */ exports.spawnWorker = (instance, { workerPath }) => { - const worker = new TessWorker(); - /* - *if (Blob && URL) { - * const blob = new Blob([`importScripts("${workerPath}");`], { - * type: 'application/javascript', - * }); - * worker = new Worker(URL.createObjectURL(blob)); - *} else { - * worker = new Worker(workerPath); - *} - */ + let worker; + if (Blob && URL) { + const blob = new Blob([`importScripts("${workerPath}");`], { + type: 'application/javascript', + }); + worker = new Worker(URL.createObjectURL(blob)); + } else { + worker = new Worker(workerPath); + } worker.onmessage = ({ data }) => { if (data.jobId.startsWith('Job')) { diff --git a/src/browser/worker.js b/src/browser/worker.js index 624342c..55a6cbe 100644 --- a/src/browser/worker.js +++ b/src/browser/worker.js @@ -11,8 +11,6 @@ const check = require('check-types'); const workerUtils = require('../common/workerUtils'); -let TesseractCore = null; - /* * register message handler */ @@ -28,24 +26,21 @@ workerUtils.setAdapter({ getCore: (corePath, res) => { if (check.undefined(global.TesseractCore)) { res.progress({ status: 'loading tesseract core', progress: 0 }); - // global.importScripts(corePath); + global.importScripts(corePath); /* * Depending on whether the browser supports WebAssembly, * the version of the TesseractCore will be different. */ - /* - *if (check.not.undefined(global.TesseractCoreWASM) && typeof WebAssembly === 'object') { - * global.TesseractCore = global.TesseractCoreWASM; - *} else if (check.not.undefined(global.TesseractCoreASM)) { - * global.TesseractCore = global.TesseractCoreASM; - *} else { - * throw Error('Failed to load TesseractCore'); - *} - */ - TesseractCore = require('tesseract.js-core/tesseract-core.wasm.js'); + if (check.not.undefined(global.TesseractCoreWASM) && typeof WebAssembly === 'object') { + global.TesseractCore = global.TesseractCoreWASM; + } else if (check.not.undefined(global.TesseractCoreASM)) { + global.TesseractCore = global.TesseractCoreASM; + } else { + throw Error('Failed to load TesseractCore'); + } res.progress({ status: 'loading tesseract core', progress: 1 }); } - return TesseractCore; + return global.TesseractCore; }, b64toU8Array: s => new Uint8Array(atob(s).split('').map(c => c.charCodeAt(0))), writeFile: (path, data, type) => {