diff --git a/package-lock.json b/package-lock.json index 1a2a3dd..b344a7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9808,6 +9808,16 @@ "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 d90ee44..5474e33 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "wait-on": "^3.2.0", "webpack": "^4.26.0", "webpack-cli": "^3.1.2", - "webpack-dev-middleware": "^3.4.0" + "webpack-dev-middleware": "^3.4.0", + "worker-loader": "^2.0.0" }, "dependencies": { "axios": "^0.18.0", diff --git a/scripts/webpack.config.common.js b/scripts/webpack.config.common.js index 7f179bf..a6dd7d5 100644 --- a/scripts/webpack.config.common.js +++ b/scripts/webpack.config.common.js @@ -20,4 +20,7 @@ module.exports = { }, ], }, + node: { + fs: 'empty', + }, }; diff --git a/scripts/webpack.config.dev.js b/scripts/webpack.config.dev.js index 8729674..9efa913 100644 --- a/scripts/webpack.config.dev.js +++ b/scripts/webpack.config.dev.js @@ -29,8 +29,10 @@ 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 15af617..b088f3f 100644 --- a/scripts/webpack.config.prod.js +++ b/scripts/webpack.config.prod.js @@ -23,8 +23,10 @@ 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 1be4ee3..e8b4760 100644 --- a/src/browser/index.js +++ b/src/browser/index.js @@ -10,6 +10,8 @@ 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'); @@ -119,15 +121,17 @@ exports.defaultOptions = { * @param {string} options.workerPath - worker script path */ exports.spawnWorker = (instance, { 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); - } + 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); + *} + */ worker.onmessage = ({ data }) => { if (data.jobId.startsWith('Job')) { diff --git a/src/browser/worker.js b/src/browser/worker.js index 55a6cbe..624342c 100644 --- a/src/browser/worker.js +++ b/src/browser/worker.js @@ -11,6 +11,8 @@ const check = require('check-types'); const workerUtils = require('../common/workerUtils'); +let TesseractCore = null; + /* * register message handler */ @@ -26,21 +28,24 @@ 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'); - } + /* + *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'); res.progress({ status: 'loading tesseract core', progress: 1 }); } - return global.TesseractCore; + return TesseractCore; }, b64toU8Array: s => new Uint8Array(atob(s).split('').map(c => c.charCodeAt(0))), writeFile: (path, data, type) => {