worker thread - using rollup config instead of plugin

This commit is contained in:
Jan Prochazka
2021-12-26 10:17:58 +01:00
parent a686e21c07
commit 6a7a56886c
2 changed files with 93 additions and 71 deletions

View File

@@ -7,10 +7,9 @@ import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess'; import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript'; import typescript from '@rollup/plugin-typescript';
import replace from '@rollup/plugin-replace'; import replace from '@rollup/plugin-replace';
import webWorkerLoader from 'rollup-plugin-web-worker-loader'; // import webWorkerLoader from 'rollup-plugin-web-worker-loader';
import css from 'rollup-plugin-css-only'; import css from 'rollup-plugin-css-only';
const production = !process.env.ROLLUP_WATCH; const production = !process.env.ROLLUP_WATCH;
function serve() { function serve() {
@@ -34,77 +33,98 @@ function serve() {
}; };
} }
export default { export default [
input: 'src/main.ts', {
output: { input: 'src/query/QueryParserWorker.js',
sourcemap: true, output: {
format: 'iife', sourcemap: true,
name: 'app', format: 'iife',
file: 'public/build/bundle.js', file: 'public/build/QueryParserWorker.js',
},
plugins: [
commonjs(),
resolve({
browser: true,
}),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser(),
],
}, },
plugins: [
copy({ {
targets: [ input: 'src/main.ts',
{ output: {
src: '../../node_modules/@mdi/font/css/materialdesignicons.css', sourcemap: true,
dest: 'public/build/fonts/', format: 'iife',
name: 'app',
file: 'public/build/bundle.js',
},
plugins: [
copy({
targets: [
{
src: '../../node_modules/@mdi/font/css/materialdesignicons.css',
dest: 'public/build/fonts/',
},
{
src: '../../node_modules/@mdi/font/fonts/*',
dest: 'public/build/fonts/',
},
{
src: '../../node_modules/diff2html/bundles/css/diff2html.min.css',
dest: 'public/build/',
},
],
}),
replace({
'process.env.API_URL': JSON.stringify(process.env.API_URL),
}),
svelte({
preprocess: sveltePreprocess({ sourceMap: !production }),
compilerOptions: {
// enable run-time checks when not in production
dev: !production,
}, },
{ }),
src: '../../node_modules/@mdi/font/fonts/*', // we'll extract any component CSS out into
dest: 'public/build/fonts/', // a separate file - better for performance
}, css({ output: 'bundle.css' }),
{
src: '../../node_modules/diff2html/bundles/css/diff2html.min.css',
dest: 'public/build/',
},
],
}),
replace({ // If you have external dependencies installed from
'process.env.API_URL': JSON.stringify(process.env.API_URL), // npm, you'll most likely need these plugins. In
}), // some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ['svelte'],
}),
commonjs(),
typescript({
sourceMap: !production,
inlineSources: !production,
}),
svelte({ // In dev mode, call `npm run start` once
preprocess: sveltePreprocess({ sourceMap: !production }), // the bundle has been generated
compilerOptions: { !production && serve(),
// enable run-time checks when not in production
dev: !production,
},
}),
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: 'bundle.css' }),
// If you have external dependencies installed from // Watch the `public` directory and refresh the
// npm, you'll most likely need these plugins. In // browser on changes when not in production
// some cases you'll need additional configuration - !production && livereload('public'),
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ['svelte'],
}),
commonjs(),
typescript({
sourceMap: !production,
inlineSources: !production,
}),
// In dev mode, call `npm run start` once // If we're building for production (npm run build
// the bundle has been generated // instead of npm run dev), minify
!production && serve(), production && terser(),
// Watch the `public` directory and refresh the // webWorkerLoader(),
// browser on changes when not in production ],
!production && livereload('public'), watch: {
clearScreen: true,
// If we're building for production (npm run build },
// instead of npm run dev), minify
production && terser(),
webWorkerLoader(),
],
watch: {
clearScreen: true,
}, },
}; ];

View File

@@ -26,7 +26,7 @@
import { handleCommandKeyDown } from '../commands/CommandListener.svelte'; import { handleCommandKeyDown } from '../commands/CommandListener.svelte';
import resizeObserver from '../utility/resizeObserver'; import resizeObserver from '../utility/resizeObserver';
// @ts-ignore // @ts-ignore
import QueryParserWorker from 'web-worker:./QueryParserWorker'; // import QueryParserWorker from 'web-worker:./QueryParserWorker';
import queryParserWorkerFallback from './queryParserWorkerFallback'; import queryParserWorkerFallback from './queryParserWorkerFallback';
const EDITOR_ID = `svelte-ace-editor-div:${Math.floor(Math.random() * 10000000000)}`; const EDITOR_ID = `svelte-ace-editor-div:${Math.floor(Math.random() * 10000000000)}`;
@@ -138,12 +138,14 @@
if (enabled) { if (enabled) {
if (!queryParserWorker) { if (!queryParserWorker) {
try { try {
queryParserWorker = new QueryParserWorker(); queryParserWorker = new Worker('build/QueryParserWorker.js');
// console.log('WORKER', queryParserWorker);
queryParserWorker.onmessage = e => { queryParserWorker.onmessage = e => {
processParserResult(e.data); processParserResult(e.data);
}; };
} catch (err) { } catch (err) {
console.warn('WORKER ERROR, using fallback worker', err.message); // console.error('WORKER ERROR', err);
console.log('WORKER ERROR, using fallback worker', err.message);
queryParserWorker = 'fallback'; queryParserWorker = 'fallback';
} }
} }