我一直使用webpack webworker loader。现在我们正在迁移到Angular 7,开始使用typescript。
在查阅了许多指南和教程以及这篇令人惊叹的问题后:
https://github.com/webpack-contrib/worker-loader/issues/94#issuecomment-336596520我不断遇到错误,并且无法让worker-loader与typescript配合使用。
ERROR in [at-loader] ....../import/worker/index.ts:4:58 TS2306: 文件'....../import/worker/import.worker.ts'不是一个模块。
ERROR in [at-loader] ......./import/import.component.ts:13:11 TS2304: 找不到名称'MyWorkerImport'。
我的当前设置如下:
webpack配置文件:
module: {
rules: [
{
test: /\.worker\.ts$/,
use: [
{
loader: 'worker-loader',
options: {
name: '[name].[hash].js',
publicPath: '/'
}
},
{
loader: 'awesome-typescript-loader',
options: {
configFileName: rootPath + '/tsconfig.json'
}
}
]
}
]
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es6", "dom" ],
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"declaration": false,
"skipLibCheck": true,
"types": [
"node"
],
"typeRoots": [
"node_modules/@types",
"typings"
]
},
"files": []
}
typings/custom.d.ts
declare module 'worker-loader!*' {
class WebpackWorker extends Worker {
constructor();
}
export = WebpackWorker;
}
worker/import.worker.ts
import { MyWorker, MESSAGE_TYPE } from './types';
const ctx: MyWorker= self as any;
ctx.onmessage = event => {
const msg = event.data;
console.log(event);
};
ctx.postMessage({ type: MESSAGE_TYPE.READY });
export default null as any;
worker/index.ts
import { MyWorker} from './types';
import MyWorkerImport = require('./import.worker');
export { MESSAGE_TYPE } from './types';
export default MyWorkerImport as typeof MyWorker;
worker/types.d.ts
// MyWorker/types.d.ts
// Enumerate message types
export const enum MESSAGE_TYPE {
READY = 'ready',
REQUEST = 'request',
RESULT = 'result',
ERROR = 'error'
}
// Define expected properties for each message type
interface IReadyMessage {
type: MESSAGE_TYPE.READY;
}
interface IRequestMessage {
type: MESSAGE_TYPE.REQUEST;
paramA: string;
paramB: number;
}
interface IResultMessage {
type: MESSAGE_TYPE.RESULT;
data: Float32Array;
}
interface IErrorMessage {
type: MESSAGE_TYPE.ERROR;
error: string;
}
// Create a union type of all messages for convenience
type MyWorkerMessage = IReadyMessage | IRequestMessage | IResultMessage | IErrorMessage;
// Extend MessageEvent to use our messages
interface IMyMessageEvent extends MessageEvent {
data: MyWorkerMessage;
}
// Extend Worker to use our custom MessageEvent
export class MyWorkerWorker extends Worker {
public onmessage: (this: MyWorker, ev: IMyMessageEvent) => any;
public postMessage(this: MyWorker, msg: MyWorkerMessage, transferList?: ArrayBuffer[]): any;
public addEventListener(type: 'message', listener: (this: MyWorker, ev: IMyMessageEvent) => any, useCapture?: boolean): void;
public addEventListener(type: 'error', listener: (this: MyWorker, ev: ErrorEvent) => any, useCapture?: boolean): void;
}
import.component.ts
import {Component, OnInit} from '@angular/core';
import MyWorker from "./worker"
@Component({
templateUrl: './import.a2.html',
styleUrls: ['./import.a2.scss']
})
export class ImportComponent implements OnInit {
workers: MyWorker[] = [];
constructor() {}
ngOnInit(): void {}
processFile() {
const worker = new MyWorker('');
}
}
我一直收到以下消息:
错误信息 [at-loader] ....../import/worker/index.ts:4:58 TS2306: 文件 '....../import/worker/import.worker.ts' 不是一个模块。
错误信息 [at-loader] ......./import/import.component.ts:13:11 TS2304: 找不到名称 'MyWorkerImport'。
如果我将该行更改为
import MyWorker = require('worker-loader!./import.worker');
错误变为:
在[at-loader]中发生错误 ....../import/worker/index.ts:4:58 TS2307:找不到模块'worker-loader!./import.worker'。
在[at-loader]中发生错误 ....../import/import.component.ts:13:11 TS2304:找不到名称'MyWorkerImport'。