提问者:小点点

使用带有typescript的webpack worker loader会导致找不到模块错误


我已经使用WebpackWebWorker加载程序一段时间了。我们现在迁移到Angular 7并开始使用typescript。

在遵循了许多指南和教程以及这篇令人惊叹的文章之后:https://github.com/webpack-contrib/worker-loader/issues/94#issuecomment-336596520

我不断出现错误,无法让worker loader使用typescript。

错误在[at-loader]....../导入/工人/index.ts: 4:58 TS2306:文件'....../导入/工人/import.worker.ts'不是一个模块。

错误在[at-loader]........./导入/import.component.ts:13: 11 TS2304:找不到名称'MyWorker导入'.

我当前的设置如下:

网页包配置:

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": []
}

打字/习惯。d、 ts

declare module 'worker-loader!*' {
    class WebpackWorker extends Worker {
        constructor();
    }

    export = WebpackWorker;
}

工人/进口。工人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;

工人/索引。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;
}

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]....../导入/工人/index.ts: 4:58 TS2306:文件'....../导入/工人/import.worker.ts'不是一个模块。

错误在[at-loader]........./导入/import.component.ts:13: 11 TS2304:找不到名称'MyWorker导入'.

如果我把线路改成

import MyWorker = require('worker-loader!./import.worker');

错误更改为:

[at loader]中出错/导入/工作/索引。ts:4:58 TS2307:找不到模块“工作加载程序!”/进口工人的。

[at loader]中出错/导入/导入。组成部分ts:13:11 TS2304:找不到名称“MyWorkerImport”。


共1个答案

匿名用户

好吧,一切都很简单。我的自定义打字文件没有包括在内,webpack也没有得到。

我更改了自定义。d、 ts到名为自定义的文件夹和名为索引的文件。d、 ts导致打字/自定义/索引。d、 ts

我换了tConfig。json包含相对类型并添加自定义(因为它在节点上是固定的)。

在这里输入更多信息:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html(搜索@typeRoots

新的tsconfig(注意类型类型根的区别):

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": [ "webworker", "es6", "dom" ],
        "noImplicitAny": false,
        "suppressImplicitAnyIndexErrors": true,
        "declaration": false,
        "skipLibCheck": true,
        "types": [
            "node",
            "custom"
        ],
        "typeRoots": [
            "node_modules/@types",
            "./typings"
        ]
    },
    "files": []
}

接下来,您必须将文件作为

import MyWorker = require('worker-loader!./worker/import.worker');

您可以删除整个网页包配置部分