An issue around Angular CLI + Comlink + WorkerPlugin

Suguru Inatomi
lacolaco-blog
Published in
2 min readNov 21, 2018

[Update]

My friend, Manfred Steyer, figured out the cause of the error. WorkerPlugin creates a child webpack compiler for worker module’s compilation without inheriting its parent’s plugins. Thanks a lot!

I’m trying to use Comlink with an Angular CLI app as an experiment. And to add WorkerPlugin, I’m using ngx-build-plus.

extra webpack config is very simple.

const WorkerPlugin = require(‘worker-plugin’);module.exports = {
plugins: [new WorkerPlugin()]
};

WorkerPlugin enables to refer worker script as a module with { type: “module"}.

import * as Comlink from 'comlinkjs';
const LoggerProxy: any = Comlink.proxy(
new (Worker as any)('./logger', { type: 'module' })
);

Logger is a simple class in logger/logger.js file.

export class Logger {
log(value) {
console.log(`value = ${value}`);
}
}

And logger/index.js is the entry point for the worker.

import * as Comlink from 'comlinkjs';
import { Logger } from './logger';
Comlink.expose(Logger, self);

An issue around TypeScript in worker

An error happens when I use TypeScript file as a part of the worker module. logger/logger.ts causes an error like the below;

ERROR in ./src/app/logger/index.js (./node_modules/worker-plugin/dist/loader.js!./src/app/logger/index.js)
Module build failed (from ./node_modules/worker-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/@ngtools/webpack/src/index.js):
Error: The AngularCompilerPlugin was not found. The @ngtools/webpack loader requires the plugin.
at Object.ngcLoader (/Users/laco/Works/angular-comlink/node_modules/@ngtools/webpack/src/loader.js:27:15)
...

Here is Repro.

  1. git clone & yarn install
  2. ng build --aot

I don’t have any idea why that error occurs. Currently, Angular CLI + WorkerPlugin cannot include TypeScript in a worker’s module.

I’m looking for solutions. Please let me know if you have some information.

--

--

Suguru Inatomi
lacolaco-blog

a.k.a. lacolaco / Google Developers Expert for Angular / ng-japan organizer