Introduction
Webpack is the module bundler for modern javascript-oriented applications. It has a configuration-based setup. There are four basic concepts or core parts in Webpack which are Entry, Output, Loaders, and plugins. We will discuss these concepts to have a better idea about the core functions Webpack performs.
- Entry
- Output
- Loaders
- Plugins
Entry
Webpack basically creates a graph of all of your dependencies and for every application, there is a single starting point which is called the entry point. It tells Webpack where to start and bundle all related dependencies.
In Webpack we define an entry point using the entry property in a configuration file.
module.exports = {
entry: {
"polyfills": "./Angular2/polyfills.ts",
"vendor": "./Angular2/vendor.ts",
"app": "./Angular2/main.ts"
},
},
In the entry object, we declare the key as the name of the file and the value as the path associated with it.
Output
You can think of it like someone has interacted with a web application in a way to input some data and execute the command by pressing the button so that after processing it will show you processed output in any form which you initiated just like in Webpack if there is an entry point to tell the webpack but after creating a bundle there must be some destination to place these bundles in webpack and we use output property to place the bundle files.
module.exports = {
output: {
path: "./Views/Home",
filename: '../../angularBundle/[name].[hash].build.js'
},
};
In the output object, we use the path where to create the directory if it's already not created and the filename as file naming syntax starting with the directory_name/file_name_from_entry_key_value.randomhashvalue.js
Loaders
The basic goal of the loaders is to load the type of all assets/files and every file is a module in webpack language which is processed and converted to javascript as webpack only if it understands javascript.
For the transformation process webpack has loaders for all the file types supported like for .ts file types 'awesome-typescript-loader', 'source-map-loader','tslint-loader' webpack uses these loaders, and so on.
To register the file types we will use module property and use the rules where we define the file name in test and loader type in loader property like this.
module: {
rules: [{
test: /\.ts$/,
loaders: ['awesome-typescript-loader', 'source-map-loader', 'tslint-loader']
},
{
test: /\.(png|jpg|gif|woff|woff2|ttf|svg|eot)$/,
loader: 'file-loader?name=assets/[name]-[hash:6].[ext]'
},
{
test: /favicon.ico$/,
loader: 'file-loader?name=/[name].[ext]'
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.scss$/,
exclude: /node_modules/,
loaders: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.html$/,
loader: 'raw-loader'
}
],
}
Plugins
The fourth and last functional part of the webpack is the plugins which will aid the webpack in doing some processing and actions after creating the bundle if the created bundle is not a minified webpack use some uglified plugin to minify code and other related plugins as per user need.
The plugin property is used to add a new plugin initialized with a new keyword.
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: ['app', 'polyfills', /*'vendor'*/ ]
}),
new CleanWebpackPlugin(
['./AngularBundle', ]
),
new HtmlWebpackPlugin({
template: "./Views/Home/loader",
filename: "./Index.cshtml",
inject: false,
}),
new CopyWebpackPlugin([{
from: './angular2/images/*.*',
to: 'assets/',
flatten: true
}])
]
Let’s move step by step to configure our app for webpack.
Step 1. Create the package.json on the root of the application.
using the command prompt to run the command
npm init -f
Step 2. Installing Development Dependencies
The development dependencies are those libraries, which are required only to develop the application. For Example, javascript libraries for unit tests, minification, and module bundlers are required only at the time of development of the application.
Our Angular 2 application needs Typescript. Webpack module, loaders & plugins etc.
Install the required webpack npm package.
npm install webpack webpack-dev-server --save-dev
Step 3. Install webpack loaders
npm install angular2-template-loader awesome-typescript-loader css-loader file-loader
html-loader null-loader raw-loader style-loader to-string-loader --save-dev
Step 4. Install webpack plugins
npm install html-webpack-plugin webpack-merge extract-text-webpack-plugin --save-dev
Step 5. install other dependencies
npm install rimraf --save-dev
Step 6. Configuring our application
Create the file tsconfig.json in the root folder of our project and copy the following code into it.


Behnam DivsalarPosted Sep 14, 2017, 2:37 AM
I followed all steps but doesn't bundle at the end, would you please provide a working source code ?
Filipe AgratiPosted Aug 11, 2017, 6:30 AM
This could be a good article but you need to provide your code. I tried to follow and I got many errors. Thinks about that.
Thiruppathi RPosted Apr 12, 2017, 12:13 AM
Nice article for angular js 2,Thank you ..