然而,在 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'
}),
]
};
非常感谢任何帮助。
谢谢:)
var test_worker_1 = __webpack_require__('./src/test.worker.ts'); var worker = new test_worker_1.default();
。它应该编译为... new test_worker_1();
。 - Nicholas Albionconstructor() { super(""); } }
- Shen liangexport default null
这一行代码让我既高兴又难过,因为它挽救了我的理智。谢谢! - elpoto