Build a Web Worker Bundle

Beacuase we cannot use Three.js inside a Web Worker, we need a specific bundle/build. Let's look at the Web Worker again.

example/web-worker/worker/worker.js
importScripts('path to new bundle');
onmessage = (e) => {
    const ifcData = e.data;
    
    const loaded = IFCjs.loadIfcFileItems(ifcData);
    const structured = IFCjs.constructProject(loaded);
    
    postMessage(structured);
};

We are only using two functions. The simpliest approach is to only bundle these two functions... so, let's do that!

Let's create a new src file. We can call this IFC.worker.js.

src/IFC.worker.js
export { loadIfcFileItems } from './ifc-parser/ifc-services/ifc-processor.js';
export { constructProject } from './ifc-project-builder/ifc-structure-builder.js';

Now, let's update the Rollup config file. Please note that you can have multiple bundles. In this example, we have removed the orignal bundle.

import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default [{
    input: './src/IFC.worker.js',
    external: ['chevrotain'],
    output: [{
            file: './build/IFC.worker.module.js',
            format: 'es',
            globals: {
                chevrotain: 'chevrotain'
            }
        },
        {
            file: './build/IFC.worker.js',
            format: 'iife',
            name: 'IFCjs',
            globals: {
                chevrotain: 'chevrotain'
            }
        }
    ],
    plugins: [
        babel({
            exclude: ['node_modules/**', 'libs/**', 'examples/**'],
            babelHelpers: 'bundled'
        }),
        resolve(),
        commonjs()
    ]
}];

Last updated