MOCKSTACKS
EN
Questions And Answers

More Tutorials









React Webpack and TypeScript installation


webpack.config.js

module.exports = {
 entry: './src/index',
 output: {
 path: __dirname + '/build',
 filename: 'bundle.js'
 },
 module: {
 rules: [{
 test: /\.tsx?$/,
 loader: 'ts-loader',
 exclude: /node_modules/
 }]
 },
 resolve: {
 extensions: ['.ts', '.tsx']
 }
};


The main components are (in addition to the standard entry, output and other webpack properties):

The loader

For this you need to create a rule that tests for the .ts and .tsx file extensions, specify ts-loader as the loader.

Resolve TS extensions

You also need to add the .ts and .tsx extensions in the resolve array, or webpack won't see them.

tsconfig.json


This is a minimal tsconfig to get you up and running.

{
 "include": [
 "src/*"
 ],
 "compilerOptions": {
 "target": "es5",
 "jsx": "react",
 "allowSyntheticDefaultImports": true
 }
}


Let's go through the properties one by one:

include

This is an array of source code. Here we have only one entry, src/*, which specifies that everything in the src directory is to be included in compilation.

compilerOptions.target

Specifies that we want to compile to ES5 target

compilerOptions.jsx

Setting this to true will make TypeScript automatically compile your tsx syntax from < div /> to React.createElement("div").

compilerOptions.allowSyntheticDefaultImports

Handy property which will allow you to import node modules as if they are ES6 modules, so instead of doing import * as React from 'react'
const { Component } = React


you can just do

import React, { Component } from 'react'

without any errors telling you that React has no default export.

Conclusion

In this page (written and validated by ) you learned about React Webpack and TypeScript installation . What's Next? If you are interested in completing React tutorial, your next topic will be learning about: React keys.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.