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 @@ @@ -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",

3
package.json

@ -44,7 +44,8 @@ @@ -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",

3
scripts/webpack.config.common.js

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

10
scripts/webpack.config.dev.js

@ -29,8 +29,10 @@ module.exports = [ @@ -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',
*}),
*/
];

10
scripts/webpack.config.prod.js

@ -23,8 +23,10 @@ module.exports = [ @@ -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',
*}),
*/
];

22
src/browser/index.js

@ -10,6 +10,8 @@ @@ -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 = { @@ -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')) {

23
src/browser/worker.js

@ -11,6 +11,8 @@ @@ -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({ @@ -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) => {

Loading…
Cancel
Save