Browse Source

Bundle tesseract.js, worker.js and tesseract-core.js together

feature/aio
Jerome Wu 6 years ago
parent
commit
936cfa6f48
  1. 10
      package-lock.json
  2. 3
      package.json
  3. 3
      scripts/webpack.config.common.js
  4. 10
      scripts/webpack.config.dev.js
  5. 10
      scripts/webpack.config.prod.js
  6. 22
      src/browser/index.js
  7. 23
      src/browser/worker.js

10
package-lock.json generated

@ -9808,6 +9808,16 @@
"errno": "~0.1.7" "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": { "wrap-ansi": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "http://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", "resolved": "http://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz",

3
package.json

@ -44,7 +44,8 @@
"wait-on": "^3.2.0", "wait-on": "^3.2.0",
"webpack": "^4.26.0", "webpack": "^4.26.0",
"webpack-cli": "^3.1.2", "webpack-cli": "^3.1.2",
"webpack-dev-middleware": "^3.4.0" "webpack-dev-middleware": "^3.4.0",
"worker-loader": "^2.0.0"
}, },
"dependencies": { "dependencies": {
"axios": "^0.18.0", "axios": "^0.18.0",

3
scripts/webpack.config.common.js

@ -20,4 +20,7 @@ module.exports = {
}, },
], ],
}, },
node: {
fs: 'empty',
},
}; };

10
scripts/webpack.config.dev.js

@ -29,8 +29,10 @@ module.exports = [
library: 'Tesseract', library: 'Tesseract',
libraryTarget: 'umd', libraryTarget: 'umd',
}), }),
genConfig({ /*
entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'), *genConfig({
filename: 'worker.dev.js', * entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'),
}), * filename: 'worker.dev.js',
*}),
*/
]; ];

10
scripts/webpack.config.prod.js

@ -23,8 +23,10 @@ module.exports = [
library: 'Tesseract', library: 'Tesseract',
libraryTarget: 'umd', libraryTarget: 'umd',
}), }),
genConfig({ /*
entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'), *genConfig({
filename: 'worker.min.js', * entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'),
}), * filename: 'worker.min.js',
*}),
*/
]; ];

22
src/browser/index.js

@ -10,6 +10,8 @@
const check = require('check-types'); const check = require('check-types');
const resolveURL = require('resolve-url'); const resolveURL = require('resolve-url');
const axios = require('axios'); const axios = require('axios');
// eslint-disable-next-line
const TessWorker = require('worker-loader?inline=true&fallback=false!./worker');
const { defaultOptions } = require('../common/options'); const { defaultOptions } = require('../common/options');
const { version } = require('../../package.json'); const { version } = require('../../package.json');
@ -119,15 +121,17 @@ exports.defaultOptions = {
* @param {string} options.workerPath - worker script path * @param {string} options.workerPath - worker script path
*/ */
exports.spawnWorker = (instance, { workerPath }) => { exports.spawnWorker = (instance, { workerPath }) => {
let worker; const worker = new TessWorker();
if (Blob && URL) { /*
const blob = new Blob([`importScripts("${workerPath}");`], { *if (Blob && URL) {
type: 'application/javascript', * const blob = new Blob([`importScripts("${workerPath}");`], {
}); * type: 'application/javascript',
worker = new Worker(URL.createObjectURL(blob)); * });
} else { * worker = new Worker(URL.createObjectURL(blob));
worker = new Worker(workerPath); *} else {
} * worker = new Worker(workerPath);
*}
*/
worker.onmessage = ({ data }) => { worker.onmessage = ({ data }) => {
if (data.jobId.startsWith('Job')) { if (data.jobId.startsWith('Job')) {

23
src/browser/worker.js

@ -11,6 +11,8 @@
const check = require('check-types'); const check = require('check-types');
const workerUtils = require('../common/workerUtils'); const workerUtils = require('../common/workerUtils');
let TesseractCore = null;
/* /*
* register message handler * register message handler
*/ */
@ -26,21 +28,24 @@ workerUtils.setAdapter({
getCore: (corePath, res) => { getCore: (corePath, res) => {
if (check.undefined(global.TesseractCore)) { if (check.undefined(global.TesseractCore)) {
res.progress({ status: 'loading tesseract core', progress: 0 }); res.progress({ status: 'loading tesseract core', progress: 0 });
global.importScripts(corePath); // global.importScripts(corePath);
/* /*
* Depending on whether the browser supports WebAssembly, * Depending on whether the browser supports WebAssembly,
* the version of the TesseractCore will be different. * the version of the TesseractCore will be different.
*/ */
if (check.not.undefined(global.TesseractCoreWASM) && typeof WebAssembly === 'object') { /*
global.TesseractCore = global.TesseractCoreWASM; *if (check.not.undefined(global.TesseractCoreWASM) && typeof WebAssembly === 'object') {
} else if (check.not.undefined(global.TesseractCoreASM)) { * global.TesseractCore = global.TesseractCoreWASM;
global.TesseractCore = global.TesseractCoreASM; *} else if (check.not.undefined(global.TesseractCoreASM)) {
} else { * global.TesseractCore = global.TesseractCoreASM;
throw Error('Failed to load TesseractCore'); *} else {
} * throw Error('Failed to load TesseractCore');
*}
*/
TesseractCore = require('tesseract.js-core/tesseract-core.wasm.js');
res.progress({ status: 'loading tesseract core', progress: 1 }); 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))), b64toU8Array: s => new Uint8Array(atob(s).split('').map(c => c.charCodeAt(0))),
writeFile: (path, data, type) => { writeFile: (path, data, type) => {

Loading…
Cancel
Save