Fix tesseract.js being part of the common chunk (#11571)

* Fix tesseract.js being part of the common chunk

Besides being 620 KB large, it also causes a modules-related runtime
error in production...

* Fix code style issue

* Fix modules error
This commit is contained in:
Eugen Rochko 2019-08-15 17:24:45 +02:00 committed by GitHub
parent 28636f43e4
commit dfe6059889
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 12 deletions

View file

@ -10,11 +10,11 @@ import { FormattedMessage, defineMessages, injectIntl } from 'react-intl';
import IconButton from 'mastodon/components/icon_button'; import IconButton from 'mastodon/components/icon_button';
import Button from 'mastodon/components/button'; import Button from 'mastodon/components/button';
import Video from 'mastodon/features/video'; import Video from 'mastodon/features/video';
import { TesseractWorker } from 'tesseract.js';
import Textarea from 'react-textarea-autosize'; import Textarea from 'react-textarea-autosize';
import UploadProgress from 'mastodon/features/compose/components/upload_progress'; import UploadProgress from 'mastodon/features/compose/components/upload_progress';
import CharacterCounter from 'mastodon/features/compose/components/character_counter'; import CharacterCounter from 'mastodon/features/compose/components/character_counter';
import { length } from 'stringz'; import { length } from 'stringz';
import { Tesseract as fetchTesseract } from 'mastodon/features/ui/util/async-components';
const messages = defineMessages({ const messages = defineMessages({
close: { id: 'lightbox.close', defaultMessage: 'Close' }, close: { id: 'lightbox.close', defaultMessage: 'Close' },
@ -148,19 +148,21 @@ class FocalPointModal extends ImmutablePureComponent {
handleTextDetection = () => { handleTextDetection = () => {
const { media } = this.props; const { media } = this.props;
this.setState({ detecting: true });
fetchTesseract().then(({ TesseractWorker }) => {
const worker = new TesseractWorker({ const worker = new TesseractWorker({
workerPath: `${assetHost}/packs/ocr/worker.min.js`, workerPath: `${assetHost}/packs/ocr/worker.min.js`,
corePath: `${assetHost}/packs/ocr/tesseract-core.wasm.js`, corePath: `${assetHost}/packs/ocr/tesseract-core.wasm.js`,
langPath: `${assetHost}/ocr/lang-data`, langPath: `${assetHost}/ocr/lang-data`,
}); });
this.setState({ detecting: true });
worker.recognize(media.get('url')) worker.recognize(media.get('url'))
.progress(({ progress }) => this.setState({ progress })) .progress(({ progress }) => this.setState({ progress }))
.finally(() => worker.terminate()) .finally(() => worker.terminate())
.then(({ text }) => this.setState({ description: removeExtraLineBreaks(text), dirty: true, detecting: false })) .then(({ text }) => this.setState({ description: removeExtraLineBreaks(text), dirty: true, detecting: false }))
.catch(() => this.setState({ detecting: false })); .catch(() => this.setState({ detecting: false }));
}).catch(() => this.setState({ detecting: false }));
} }
render () { render () {

View file

@ -133,3 +133,7 @@ export function ListAdder () {
export function Search () { export function Search () {
return import(/*webpackChunkName: "features/search" */'../../search'); return import(/*webpackChunkName: "features/search" */'../../search');
} }
export function Tesseract () {
return import(/*webpackChunkName: "tesseract" */'tesseract.js');
}

View file

@ -20,6 +20,12 @@ module.exports = (api) => {
['react-intl', { messagesDir: './build/messages' }], ['react-intl', { messagesDir: './build/messages' }],
'preval', 'preval',
], ],
overrides: [{
test: /tesseract\.js/,
presets: [
['@babel/env', { ...envOptions, modules: 'commonjs' }],
],
}],
}; };
switch (env) { switch (env) {