我正在按照worker-loader文档中的描述配置我的项目,并且我能够使用正确的 d.ts 让 TS 代码 intel 工作。
但是,在 webpack 构建期间它会抛出一个错误,我不明白为什么。
ERROR in ./src/test.worker.ts
Module parse failed: Unexpected token (1:9)
You may need an appropriate loader to handle this file type.
| const ctx: Worker = self as any;
| ctx.addEventListener('message', event => {
| console.log(event);
我的工作人员文件内容test.worker.ts
:
const ctx: Worker = self as any;
ctx.addEventListener('message', event => {
console.log(event);
setTimeout(() => ctx.postMessage({
foo: 'boo'
}), 5000);
});
申请入口index.ts
import TestWorker from './test.worker.ts';
const test = new TestWorker();
test.postMessage({});
最后这是我的webpack.config.js
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const DIR_SOURCE = path.resolve(__dirname, 'src');
const DIR_BUILD = path.resolve(__dirname, 'build');
module.exports = {
entry: `${DIR_SOURCE}/index.tsx`,
output: {
path: DIR_BUILD,
filename: 'project.bundle.js'
},
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules: [
{test: /\.tsx?$/, loader: "ts-loader" },
{test: /\.worker\.ts$/, use: ['ts-loader', 'worker-loader'] },
{test: [/\.vert$/, /\.frag$/], use: 'raw-loader'},
{test: /\.(png|jpg|gif|svg)$/, use: {loader: 'file-loader', options: {}} },
{test: /.*\.sass/, use: ['style-loader', 'css-loader', 'sass-loader'] },
]
},
plugins: [
new webpack.DefinePlugin({
'CANVAS_RENDERER': JSON.stringify(true),
'WEBGL_RENDERER': JSON.stringify(true)
}),
new HtmlWebpackPlugin({
template: 'src/index.ejs'
}),
]
};
任何帮助深表感谢。
谢谢 :)