我已经使用 webpack webworker 加载器有一段时间了。我们现在正在迁移到 Angular 7 并开始使用 typescript。
在遵循了许多指南和教程以及这个惊人的问题后: https ://github.com/webpack-contrib/worker-loader/issues/94#issuecomment-336596520
我不断收到错误,并且没有让 worker-loader 使用 typescript。
[at-loader] 中的错误 ....../import/worker/index.ts:4:58 TS2306:文件 '....../import/worker/import.worker.ts' 不是模块.
[at-loader] 中的错误 ......./import/import.component.ts:13:11 TS2304:找不到名称“MyWorkerImport”。
我目前的设置如下:
网络包配置:
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": []
}
打字/custom.d.ts
declare module 'worker-loader!*' {
class WebpackWorker extends Worker {
constructor();
}
export = WebpackWorker;
}
工人/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;
工人/index.ts
import { MyWorker} from './types';
import MyWorkerImport = require('./import.worker');
export { MESSAGE_TYPE } from './types';
export default MyWorkerImport as typeof MyWorker;
工人/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;
}
导入.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”。