I am configuring my project as described in worker-loader documentation and I was able to get TS code intel working using the correct d.ts.
However, during webpack build it throws an error and I don't understand why.
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);
My worker file content test.worker.ts
:
const ctx: Worker = self as any;
ctx.addEventListener('message', event => {
console.log(event);
setTimeout(() => ctx.postMessage({
foo: 'boo'
}), 5000);
});
Application entry index.ts
import TestWorker from './test.worker.ts';
const test = new TestWorker();
test.postMessage({});
And finally here is my 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'
}),
]
};
Any help is much appreciated.
Thank you :)
var test_worker_1 = __webpack_require__('./src/test.worker.ts'); var worker = new test_worker_1.default();
. It should compile to... new test_worker_1();
– Prelusive