Using Webpack with Typescript – DEV Community



What’s Webpack all about?

Webpack is a Bundling and constructing orchestration instrument.
It’s a instrument that helps to serve optimized initiatives by bundling, cleansing, and minifying code to scale back the downloadable information and supply one bundled file that has all our code inside it.



When to make use of it?

When engaged on massive initiatives, We are going to most likely find yourself with lots of information in our listing. Along with that, we might want to export and import these information to attach them collectively.

If we check out a easy net utility that has some information and parts. these information and parts will likely be served to the browser individually as proven beneath.

Now as these information get greater in dimension, they’ll trigger some latency and make the appliance slower as a result of these parts and information make HTTP requests individually.

Right here comes the ability of Webpack. It’ll look into the entire information of the appliance. It’ll compress and minify all the things, then serve it as proven beneath. web with webpack



Easy utilization of Webpack with Typescript

To begin off with utilizing Webpack with typescript. Some dependencies ought to be put in.

npm i -D webpack webpack-cli ts-loader typescript webpack-dev-server
Enter fullscreen mode

Exit fullscreen mode

These dependencies work hand at hand to compile typescript code to javascript with the assistance of ts-loader and typescript, then bundle the js code utilizing webpack.

Configuring webpack

webpack.config.js ought to be created within the root listing.

The very first thing we are able to do on this config file is specifying the entry file of the appliance which is normally app.ts or index.ts

module.exports = {
  entry: './src/app.ts'
}
Enter fullscreen mode

Exit fullscreen mode

Now we now have the entry file setup, we are able to configure details about how and the place this bundle file will likely be.
Normally, when compiling typescript code, the compilation will likely be saved in a folder known as dist (might be renamed) as we specify that within the tsconfig.json.
Similar for Webpack config, it must know the place to precisely bundle. Due to this fact we set absolutely the path of the bundle vacation spot.

const path = require('path');

module.exports = {
  entry: './src/app.ts',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist') 
}
}
Enter fullscreen mode

Exit fullscreen mode

After setting all that, Webpack must know the best way to compile these typescript information. we are able to set a easy common expression for Webpack to search out all typescript information. After setting that we specify that ts-loader that we put in earlier will deal with the compilation to Javascript through the bundling course of as the next:

const path = require('path');

module.exports = {
  entry: './src/app.ts',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist') 
},
  module: {
    guidelines : [
     {
       test: //.ts$/, 
       use: 'ts-loader',
       exclude: /node_modules/

     }
  ]
}
}
Enter fullscreen mode

Exit fullscreen mode

We will add completely different presets for various file extensions when bundling resembling CSS information or photos by including guidelines to the module.

Extra to study Webpack: https://webpack.js.org/concepts/

Add a Comment

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