使用 webpack worker-loader with typescript 导致找不到模块错误
Using webpack worker-loader with typescript causes cannot find module error
我使用 webpack webworker loader 已经有一段时间了。我们现在正在迁移到 Angular 7 并开始使用 typescript。
在遵循了许多指南和教程以及这个令人惊奇的问题之后 post:
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'.
我目前的设置如下:
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('');
}
}
我不断收到以下消息:
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(注意types
和typeRoots
的区别):
{
"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 配置部分
我使用 webpack webworker loader 已经有一段时间了。我们现在正在迁移到 Angular 7 并开始使用 typescript。
在遵循了许多指南和教程以及这个令人惊奇的问题之后 post: 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'.
我目前的设置如下:
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('');
}
}
我不断收到以下消息:
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(注意types
和typeRoots
的区别):
{
"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 配置部分