Webpack: Setting up SCSS, TypeScript, ReactJS TSX, and ES6+ Compilation

In today’s web development world, tooling has become an indispensable aspect. One such tool that has gained tremendous popularity is Webpack. Webpack can manage, compile and bundle your JavaScript, TypeScript, SCSS, and more with a load of other cool features. In this comprehensive guide, we’ll walk you through how to set up Webpack to compile SCSS, TypeScript, ReactJS TSX, and ES6+.

Table of Contents:

What is Webpack?

Webpack is a static module bundler for modern JavaScript applications. It’s a highly configurable tool that helps developers to manage dependencies, optimize files, run tasks during the development process, and much more.

Why Use Webpack?

Webpack is used primarily for two reasons:

  1. Handling Dependencies: Webpack allows all assets and modules in your application to express dependencies on each other, making sure they are loaded in the correct order.
  2. Optimization: Webpack has a wide array of plugins and loaders that enable developers to minify code, optimize images, compile modern JavaScript (ES6+) to ES5 and much more.

Now that we know what Webpack is and why it’s helpful, let’s dive into the configuration.

Prerequisites

Before starting, make sure you have:

  1. Node.js and npm installed on your computer.
  2. Basic understanding of TypeScript, ReactJS, SCSS, and ES6+.
  3. An existing JavaScript/TypeScript project to work on.

Step 1: Setting up the Project and Installing Dependencies

Navigate to your project folder in the terminal and initialize a new Node.js project if you haven’t done so:

npm init -y

Next, install Webpack and Webpack CLI as development dependencies:

npm install --save-dev webpack webpack-cli

Step 2: Webpack Configuration for TypeScript and ReactJS TSX

For TypeScript and ReactJS TSX, you’ll need to install ts-loader, typescript, and @types/react:

npm install --save-dev typescript ts-loader @types/react

Now, let’s create a webpack.config.js file in the root directory of your project and set up our TypeScript configuration:

const path = require('path');

module.exports = {
  entry: './src/index.tsx',
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

This configuration tells Webpack to:

  1. Start bundling our application from the src/index.tsx file.
  2. Use the ts-loader to transpile TypeScript files to JavaScript.
  3. Resolve tsx, ts, and js extensions.

We’ll also need to create a tsconfig.json file to configure TypeScript compilation:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es6",
    "jsx": "react"
  },
  "include": [
    "./src/**/*"
  ]
}

Step 3: Webpack Configuration for SCSS

For compiling SCSS files, we need to install sass-loader, sass, css-loader, and style-loader:

npm install --save-dev sass-loader sass css-loader style-loader

Add the following rule to your webpack.config.js module rules array:

{
  test: /\.scss$/,
  use: ['style-loader', 'css-loader', 'sass-loader'],
  exclude: /node_modules/,
}

This configuration tells Webpack to use:

  1. sass-loader to convert SCSS to CSS.
  2. css-loader to handle CSS import in JavaScript.
  3. style-loader to inject CSS into the DOM.

Step 4: Webpack Configuration for ES6+

Babel is a great tool for transpiling ES6+ JavaScript code into ES5 compatible code. To set it up, we need to install babel-loader, @babel/core, and @babel/preset-env:

npm install --save-dev babel-loader @babel/core @babel/preset-env

Add the following rule to your webpack.config.js module rules array:

{
  test: /\.m?js$/,
  exclude: /(node_modules|bower_components)/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['@babel/preset-env']
    }
  }
}

This configuration tells Webpack to use babel-loader for all .js files except those within node_modules or bower_components.

Step 5: Running the Build

Add the following to your scripts section in package.json:

"scripts": {
  "build": "webpack"
}

You can now run npm run build to create a bundled JavaScript file within the dist directory.

Conclusion

Setting up Webpack might be a daunting task at first, but it’s worth the effort. With Webpack, you can organize your project, manage dependencies, and utilize modern JavaScript and CSS features while maintaining compatibility with older browsers.

By using this guide, you’ll set up a Webpack project that can handle TypeScript, ReactJS TSX, SCSS, and ES6+. However, always remember that Webpack is highly configurable, and this is just the start. As you get comfortable with it, you can start exploring other loaders and plugins to make your project more efficient.

Here are some of the advantages of using Webpack:

Pros:

  1. Highly configurable and can handle various file types and conversions.
  2. Helps in optimizing the website for faster load times and better performance.
  3. Supports hot reloading which means the page is automatically updated when you make changes in your files.

However, there are a few potential downsides you should be aware of:

Cons:

  1. Webpack’s configuration can become complex as the project grows.
  2. It may have a steep learning curve for beginners.
  3. Debugging can be difficult at times due to the complexity of the configuration.

All said, Webpack is a powerful tool that can enhance your web development process. It’s worth taking the time to learn and implement in your projects.

Member since January 2, 2019

As a seasoned WordPress developer with expertise in various tech stacks and languages, I bring years of experience to every project I handle. My passion for coding and dedication to delivering exceptional work ensures that each project I take on is of the highest quality. I specialize in creating custom themes, developing plugins, and building full-scale web systems. By staying up-to-date with the latest industry trends and best practices, I incorporate cutting-edge solutions into my work.

Comments

  • yobar vashoi mamu 1 year ago

    What an awesome tutorial!
    – every piece of a code separated so not a single file from the top to bottom
    – no example on a github so there is no way of knowing if it’s even working
    Continue doing such a great job!

    • Andrei 1 year ago

      The tutorial is meant to help you learn the process of preparing the Webpack configuration and not in any case to be a ready-to-copy script from GitHub.

      If you’d like a quick setup without going deep in config files like this, take a look at “Laravel Mix”. That will let you achieve the same but with just a few lines of code.

Your email address will not be published. Required fields are marked *