Skip to content

Configuration for create-react-app with Typescript? #341

Description

@jimklo

Trying to get a simple sample of threads.js working within a create-react-app that was started using the typescript enabled template.

I'm using craco to avoid ejecting, so my craco.config.js contains the following:

const ThreadsPlugin = require("threads-plugin");

module.exports = {
    module: {
        // Supposedly I don't need to enable the rules since it's already enabled in react-scripts.
        // rules: [
        //     {
        //         test: /\.ts$/,
        //         loader: "ts-loader",
        //         options: {
        //             compilerOptions: {
        //                 module: "esnext"
        //             }
        //         }
        //     }
        // ],
        plugins: [
            new ThreadsPlugin()
        ]
    }
}

I've created a simple worker as counter.ts:

import {expose} from "threads/worker";

let currentCount = 0;

const counter = {
    getCount() {
            return currentCount;
    },
    increment() {
        return ++currentCount;
    },
    decrement() {
        return --currentCount;
    }
}

expose(counter);

And in my App.tsx I'm creating a Pool and queuing a worker function:

import React, {useState} from 'react';
import logo from './logo.svg';
import './App.css';
import {Pool, spawn, Worker} from "threads";



function App() {

  const pool = Pool(() => spawn(new Worker("./counter")),8);
  const [count, setCount] = useState(0);


  const handleIncrement = () => {
      pool.queue(async counter => {
         const newCount = await counter.increment();
         setCount(newCount);
      });
  }

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React

        </a>
        Count: {count}
        <button onClick={handleIncrement}>Increment</button>
      </header>
    </div>
  );
}

export default App;

Basically what what happens is that I just keep getting an error in the browser console:
Uncaught SyntaxError: Unexpected token '<' :3000/counter:1

It would seem to me that the counter.ts file isn't passing through Babel/Webpack.

Can anyone advise on how I might add threads.js into this project?

Thanks

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions