Webpack worker-loader fails to compile typescript worker












2














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 :)










share|improve this question



























    2














    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 :)










    share|improve this question

























      2












      2








      2







      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 :)










      share|improve this question













      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 :)






      const ctx: Worker = self as any;
      ctx.addEventListener('message', event => {
      console.log(event);
      setTimeout(() => ctx.postMessage({
      foo: 'boo'
      }), 5000);
      });





      const ctx: Worker = self as any;
      ctx.addEventListener('message', event => {
      console.log(event);
      setTimeout(() => ctx.postMessage({
      foo: 'boo'
      }), 5000);
      });





      import TestWorker from './test.worker.ts';

      const test = new TestWorker();
      test.postMessage({});





      import TestWorker from './test.worker.ts';

      const test = new TestWorker();
      test.postMessage({});





      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'
      }),
      ]
      };





      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'
      }),
      ]
      };






      typescript webpack worker-loader






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 7 '18 at 8:55









      Dmitry MatveevDmitry Matveev

      3,7951932




      3,7951932
























          2 Answers
          2






          active

          oldest

          votes


















          3














          Ok I figured it out.



          First I needed to move worker loader before the ts-loader and did not need to specify array in worker use property and just keep it as described in documentation of worker-loader.



          module: {
          rules: [
          {test: /.worker.ts$/, loader: 'worker-loader'},
          {test: /.tsx?$/, loader: "ts-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'] },
          ]
          },


          Then in my worker I also needed to export anything otherwise typescript (2.8.3) would complain that it can't find a module and I export default null as any to avoid confusing ts even further.



          worker.js



          const ctx: Worker = self as any;
          ctx.addEventListener('message', event => {
          console.log(event);
          setTimeout(() => ctx.postMessage({
          foo: 'boo'
          }), 5000);
          });
          export default null as any;


          index.js



          import TestWorker from './test.worker.ts';
          const test = new TestWorker('');
          test.postMessage({});





          share|improve this answer

















          • 1




            Thanks. That solved my problem
            – Antonio Pantano
            Sep 25 '18 at 9:29



















          1














          Thanks Dmitry, you helped me get much further along in getting worker-loader to build my Typescript project.



          Even so I had three more issues I needed to solve in order to get it to work:




          • The build would hang when I added worker-loader above ts-loader/awesome-typescript-loader. I fixed this by making the import paths be much more specific for types that were imported/exported from the web worker module. I found out by trying to comment out the content of the web worker entry file, and then uncomment one line at a time, to see what would and wouldn't build.


          • The second thing I did wrong was that I added worker-loader both as a rule my webpack config, and in the import statement. I was basically running worker-loader twice on the web worker module. Doh. So either add worker-loader to your webpack config or add 'worker-loader!' to the import/require statement. I added worker-loader to my webpack config and the just imported like this: import MyWorker = require('./my-worker');


          • I got a typeerror MyWorker is not a constructor when I tried to do new MyWorker(), and had to cast MyWorker to any for the Typescript type checker to swallow it: new (MyWorker as any)();



          Hope this helps somebody.






          share|improve this answer





















          • yep I solved last problem by exporting as any from worker too export default null as any;
            – Dmitry Matveev
            Dec 3 '18 at 22:29











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50210416%2fwebpack-worker-loader-fails-to-compile-typescript-worker%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Ok I figured it out.



          First I needed to move worker loader before the ts-loader and did not need to specify array in worker use property and just keep it as described in documentation of worker-loader.



          module: {
          rules: [
          {test: /.worker.ts$/, loader: 'worker-loader'},
          {test: /.tsx?$/, loader: "ts-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'] },
          ]
          },


          Then in my worker I also needed to export anything otherwise typescript (2.8.3) would complain that it can't find a module and I export default null as any to avoid confusing ts even further.



          worker.js



          const ctx: Worker = self as any;
          ctx.addEventListener('message', event => {
          console.log(event);
          setTimeout(() => ctx.postMessage({
          foo: 'boo'
          }), 5000);
          });
          export default null as any;


          index.js



          import TestWorker from './test.worker.ts';
          const test = new TestWorker('');
          test.postMessage({});





          share|improve this answer

















          • 1




            Thanks. That solved my problem
            – Antonio Pantano
            Sep 25 '18 at 9:29
















          3














          Ok I figured it out.



          First I needed to move worker loader before the ts-loader and did not need to specify array in worker use property and just keep it as described in documentation of worker-loader.



          module: {
          rules: [
          {test: /.worker.ts$/, loader: 'worker-loader'},
          {test: /.tsx?$/, loader: "ts-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'] },
          ]
          },


          Then in my worker I also needed to export anything otherwise typescript (2.8.3) would complain that it can't find a module and I export default null as any to avoid confusing ts even further.



          worker.js



          const ctx: Worker = self as any;
          ctx.addEventListener('message', event => {
          console.log(event);
          setTimeout(() => ctx.postMessage({
          foo: 'boo'
          }), 5000);
          });
          export default null as any;


          index.js



          import TestWorker from './test.worker.ts';
          const test = new TestWorker('');
          test.postMessage({});





          share|improve this answer

















          • 1




            Thanks. That solved my problem
            – Antonio Pantano
            Sep 25 '18 at 9:29














          3












          3








          3






          Ok I figured it out.



          First I needed to move worker loader before the ts-loader and did not need to specify array in worker use property and just keep it as described in documentation of worker-loader.



          module: {
          rules: [
          {test: /.worker.ts$/, loader: 'worker-loader'},
          {test: /.tsx?$/, loader: "ts-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'] },
          ]
          },


          Then in my worker I also needed to export anything otherwise typescript (2.8.3) would complain that it can't find a module and I export default null as any to avoid confusing ts even further.



          worker.js



          const ctx: Worker = self as any;
          ctx.addEventListener('message', event => {
          console.log(event);
          setTimeout(() => ctx.postMessage({
          foo: 'boo'
          }), 5000);
          });
          export default null as any;


          index.js



          import TestWorker from './test.worker.ts';
          const test = new TestWorker('');
          test.postMessage({});





          share|improve this answer












          Ok I figured it out.



          First I needed to move worker loader before the ts-loader and did not need to specify array in worker use property and just keep it as described in documentation of worker-loader.



          module: {
          rules: [
          {test: /.worker.ts$/, loader: 'worker-loader'},
          {test: /.tsx?$/, loader: "ts-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'] },
          ]
          },


          Then in my worker I also needed to export anything otherwise typescript (2.8.3) would complain that it can't find a module and I export default null as any to avoid confusing ts even further.



          worker.js



          const ctx: Worker = self as any;
          ctx.addEventListener('message', event => {
          console.log(event);
          setTimeout(() => ctx.postMessage({
          foo: 'boo'
          }), 5000);
          });
          export default null as any;


          index.js



          import TestWorker from './test.worker.ts';
          const test = new TestWorker('');
          test.postMessage({});






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 7 '18 at 9:52









          Dmitry MatveevDmitry Matveev

          3,7951932




          3,7951932








          • 1




            Thanks. That solved my problem
            – Antonio Pantano
            Sep 25 '18 at 9:29














          • 1




            Thanks. That solved my problem
            – Antonio Pantano
            Sep 25 '18 at 9:29








          1




          1




          Thanks. That solved my problem
          – Antonio Pantano
          Sep 25 '18 at 9:29




          Thanks. That solved my problem
          – Antonio Pantano
          Sep 25 '18 at 9:29













          1














          Thanks Dmitry, you helped me get much further along in getting worker-loader to build my Typescript project.



          Even so I had three more issues I needed to solve in order to get it to work:




          • The build would hang when I added worker-loader above ts-loader/awesome-typescript-loader. I fixed this by making the import paths be much more specific for types that were imported/exported from the web worker module. I found out by trying to comment out the content of the web worker entry file, and then uncomment one line at a time, to see what would and wouldn't build.


          • The second thing I did wrong was that I added worker-loader both as a rule my webpack config, and in the import statement. I was basically running worker-loader twice on the web worker module. Doh. So either add worker-loader to your webpack config or add 'worker-loader!' to the import/require statement. I added worker-loader to my webpack config and the just imported like this: import MyWorker = require('./my-worker');


          • I got a typeerror MyWorker is not a constructor when I tried to do new MyWorker(), and had to cast MyWorker to any for the Typescript type checker to swallow it: new (MyWorker as any)();



          Hope this helps somebody.






          share|improve this answer





















          • yep I solved last problem by exporting as any from worker too export default null as any;
            – Dmitry Matveev
            Dec 3 '18 at 22:29
















          1














          Thanks Dmitry, you helped me get much further along in getting worker-loader to build my Typescript project.



          Even so I had three more issues I needed to solve in order to get it to work:




          • The build would hang when I added worker-loader above ts-loader/awesome-typescript-loader. I fixed this by making the import paths be much more specific for types that were imported/exported from the web worker module. I found out by trying to comment out the content of the web worker entry file, and then uncomment one line at a time, to see what would and wouldn't build.


          • The second thing I did wrong was that I added worker-loader both as a rule my webpack config, and in the import statement. I was basically running worker-loader twice on the web worker module. Doh. So either add worker-loader to your webpack config or add 'worker-loader!' to the import/require statement. I added worker-loader to my webpack config and the just imported like this: import MyWorker = require('./my-worker');


          • I got a typeerror MyWorker is not a constructor when I tried to do new MyWorker(), and had to cast MyWorker to any for the Typescript type checker to swallow it: new (MyWorker as any)();



          Hope this helps somebody.






          share|improve this answer





















          • yep I solved last problem by exporting as any from worker too export default null as any;
            – Dmitry Matveev
            Dec 3 '18 at 22:29














          1












          1








          1






          Thanks Dmitry, you helped me get much further along in getting worker-loader to build my Typescript project.



          Even so I had three more issues I needed to solve in order to get it to work:




          • The build would hang when I added worker-loader above ts-loader/awesome-typescript-loader. I fixed this by making the import paths be much more specific for types that were imported/exported from the web worker module. I found out by trying to comment out the content of the web worker entry file, and then uncomment one line at a time, to see what would and wouldn't build.


          • The second thing I did wrong was that I added worker-loader both as a rule my webpack config, and in the import statement. I was basically running worker-loader twice on the web worker module. Doh. So either add worker-loader to your webpack config or add 'worker-loader!' to the import/require statement. I added worker-loader to my webpack config and the just imported like this: import MyWorker = require('./my-worker');


          • I got a typeerror MyWorker is not a constructor when I tried to do new MyWorker(), and had to cast MyWorker to any for the Typescript type checker to swallow it: new (MyWorker as any)();



          Hope this helps somebody.






          share|improve this answer












          Thanks Dmitry, you helped me get much further along in getting worker-loader to build my Typescript project.



          Even so I had three more issues I needed to solve in order to get it to work:




          • The build would hang when I added worker-loader above ts-loader/awesome-typescript-loader. I fixed this by making the import paths be much more specific for types that were imported/exported from the web worker module. I found out by trying to comment out the content of the web worker entry file, and then uncomment one line at a time, to see what would and wouldn't build.


          • The second thing I did wrong was that I added worker-loader both as a rule my webpack config, and in the import statement. I was basically running worker-loader twice on the web worker module. Doh. So either add worker-loader to your webpack config or add 'worker-loader!' to the import/require statement. I added worker-loader to my webpack config and the just imported like this: import MyWorker = require('./my-worker');


          • I got a typeerror MyWorker is not a constructor when I tried to do new MyWorker(), and had to cast MyWorker to any for the Typescript type checker to swallow it: new (MyWorker as any)();



          Hope this helps somebody.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 12:32









          SammiSammi

          9991119




          9991119












          • yep I solved last problem by exporting as any from worker too export default null as any;
            – Dmitry Matveev
            Dec 3 '18 at 22:29


















          • yep I solved last problem by exporting as any from worker too export default null as any;
            – Dmitry Matveev
            Dec 3 '18 at 22:29
















          yep I solved last problem by exporting as any from worker too export default null as any;
          – Dmitry Matveev
          Dec 3 '18 at 22:29




          yep I solved last problem by exporting as any from worker too export default null as any;
          – Dmitry Matveev
          Dec 3 '18 at 22:29


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50210416%2fwebpack-worker-loader-fails-to-compile-typescript-worker%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu