gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:30:01 25 4
gpt4 key购买 nike

我已经使用 webpack webworker 加载器有一段时间了。我们现在正在迁移到 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: File '....../import/worker/import.worker.ts' is not a module.

ERROR in [at-loader] ......./import/import.component.ts:13:11 TS2304: Cannot find name '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;
}

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;
}

导入.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('');
}

}

我不断收到以下消息:

ERROR in [at-loader] ....../import/worker/index.ts:4:58 TS2306: File '....../import/worker/import.worker.ts' is not a module.

ERROR in [at-loader] ......./import/import.component.ts:13:11 TS2304: Cannot find name 'MyWorkerImport'.



如果我将线路更改为
import MyWorker = require('worker-loader!./import.worker');

错误变为:

ERROR in [at-loader] ....../import/worker/index.ts:4:58 TS2307: Cannot find module 'worker-loader!./import.worker'.

ERROR in [at-loader] ....../import/import.component.ts:13:11 TS2304: Cannot find name 'MyWorkerImport'.

最佳答案

好的,一切都简单明了。我的自定义类型文件不包括在内,webpack 没有得到它。
我更改了 custom.d.ts到名为 custom 的文件夹和一个名为 index.d.ts 的文件导致 typings/custom/index.d.ts我更改了 tsconfig.json 以包含相关类型并添加自定义(因为它已固定在节点上)。
更多关于打字的信息:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html (搜索 @typeRoots )
新的 tsconfig(注意 typestypeRoots 的区别):

{
"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');
你可以删除整个 webpack 配置部分

关于angular - 使用带有 typescript 的 webpack worker-loader 导致找不到模块错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53818157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com