2

I'm trying to add Typescript to a current React, Webpack and Babel project. I'd like to be able to have support for filetypes such as, [.js, .ts, .tsx] in my project since I want to successively migrate to Typescript.

I've made some progress, but currently, I can't solve this error:

enter image description here

I'm also not 100% certain if I need to keep Babel, or if I can remove it after this setup is completed.

My tsconfig.json looks like this:

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es6", "dom", "esnext.asynciterable", "es2015"],
        "allowJs": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        // "noEmit": true,
        "strictNullChecks": true,
        // "noImplicitAny": false,
        "jsx": "preserve"
    },
    "include": ["src"],
    "exclude": ["node_modules", "build", "scripts", "jest"]
}

And my webpack.conf.js is here: https://gist.github.com/Martinnord/981769791c3e5e3a261af459b81f2733

All help is greatly appriciated since I'm pretty stuck.

Thank you!

4
  • Typescript documentation says that in order for TypeScript to understand JSX the file extension must be .tsx Commented Jul 17, 2019 at 14:25
  • @MinusFour So I should change my entry file to be .tsx? Commented Jul 17, 2019 at 14:31
  • Any file that uses JSX really Commented Jul 17, 2019 at 15:08
  • Good solution to know stackoverflow.com/questions/47508564/… Commented Jan 22, 2020 at 1:43

1 Answer 1

3

webpack.config.js example:

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/dist/',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      { test: /\.tsx?$/, loader: 'ts-loader' },
    ],
  },
  resolve: {
    extensions: ['*', '.js', '.jsx', '.ts', '.tsx'],
  },
  plugins: [new webpack.HotModuleReplacementPlugin()],
  devServer: {
    contentBase: path.resolve(__dirname, './dist'),
    hot: true,
  },
}

also, in tsconfig:

{
  ...
  "jsx": "react",
  ...
}

Full article: here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.