Webpack - Course Introduction: Webpack Is An Open Source Module Bundler For Modern
Webpack - Course Introduction: Webpack Is An Open Source Module Bundler For Modern
What is Webpack?
Code Splitting (detailed later in the course) is an exciting feature that keeps Webpack
distinct from all other module bundlers.
Goals of Webpack
The Webpack goals are not limited to:
Saving webpack only as dev dependency This will create a node modules folder.
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Webpack Demo</title>
<script src="dist/bundle.js" charset="utf-8"></script>
</head>
<body>
</body>
</html>
app.js
document.write('Hello FrescoPlayers');
Let us Compile
Run this code,
Bundle.js in Webpack
Did you note, bundle.js is loaded in our html file rather than app.js ? But, there is no
bundle.js.
You might wonder How will the file be created?
package.json
{
"name": "webpack_practice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts":
{
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack ./src/app.js ./dist/bundle.js"
},
"author": "",
"license": "ISC",
"devDependencies":
{
"webpack": "^3.8.1A"
}
}
Module bundling
Automatic bundling
JavaScript minification
Essentially, this video will explain how to install webpack, set up a configuration file, and create a
bundle for production.
The main challenge with file protocol based approach is that, we are not sure if all the
assets are actually being downloaded from a server and served .
We will still get the same output without bundle.js getting created.
1. Entry
2. Output
3. Module loaders
4. Plugins.
Output : It tells the Webpack where to bundle the modules of the application and how to treat
a bundled code.
Modify the script for webpack dev server by removing the entry and outputs
in package.json package.json
{
"name": "webpack_practice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts":
{
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"devDependencies":
{
"webpack": "^2.4.1"
}
}
Now run the build
There is wide variety of loaders available which includes: - conversion on ES6 code to ES5
code using babel. - scss loader which converts SASS code to css
Loading CSS
To load CSS, you need to use
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Webpack Demo</title>
<script src="dist/bundle.js" charset="utf-8"></script>
</head>
<body>
<button type="button" name="button" id="redButton">I'm Red</button>
<button type="button" name="button" id="greenButton">I'm Green</button>
<button type="button" name="button" id="blueButton">I'm Blue</button>
</body>
</html>
main.css
body {
background-color: aquamarine;
}
button {
border: 1px solid black;
padding: 5px 10px;
font-size: 2rem;
color: ghostwhite;
border-radius: 10px;
}
#redButton {
background-color: red;
}
#greenButton {
background-color: forestgreen;
}
#blueButton {
background-color: blue;
}
Configure your webpack
Modify the app.js file
app.js
require("./css/main.css");
webpack.config.js
function sum(a, b) {
return a+b;
}
function difference(a, b) {
return a-b;
}
app.js
require("./css/main.css");
import {sum, difference} from './operations.js';
document.write("Sum of 2 and 3 is "+sum(2, 3)); // 5
document.write("Difference of 5 and 5 is "+difference(5, 2)); // 3
But this will not work unless we include the babel loader configuration
in webpack.config.js
webpack.config.js
Plugins
In Webpack, the developers are not confined to the predefined build callbacks. They
can use customized build callbacks using plugins.
Plugins are the backbone of Webpack as it fully exposes the potential of webpack.
Plugins are additional node modules that usually work on the resulting bundle.
For Example:
HTMLWebpackPlugin
First install plugin as dev dependency:
npm install html-webpack-plugin --save-dev
webpack.config.js
module.exports = {
entry: './src/app.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
loaders: [
{ test : /\.css$/, loader: "style-loader!css-loader" },
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: { presets: ['es2015'] }
}
]
}
]
},
plugins: [ new HtmlWebpackPlugin() ]
};
Modifying webpack.config.js
If you notice, all the buttons that are missing from the final index.html, will by default serve
just js and css compiled assets.
We can also give specifications for the template to generate, rather than using the default
template.
webpack.config.js
Again, run npm run build. You will notice that all buttons will be included.
More on Plugins
CSS loaders for webpack inline's the CSS rather than placing it within a separate CSS file. This is good
for page speed. However, when the amount of CSS is high, inline CSS can slow down the site since it
can not be cached. In this video, you will learn how to use the extract-text-webpack-plugin, combined
with webpack, to export the CSS into a separate file.
Dependency Graph
In Webpack, whenever a file refers to another, it is called a dependency
Webpack starts from the entry point, finds each dependency and creates the
dependency graph.
It considers all the non-code assets such as images and stylesheets and adds them to
the graph.
Based on the graph, it bundles the files together, hence the overhead of loading all the
distinct files is eliminated.
Optimization
Now that you have successfully bundled your application, What next?
The bundled application is not directly suitable for production because of the lengthy
junk comments and spaces.
You should optimize it using a webpack command to make it ready for production.
webpack -p
Minification
Modify the package.json file like this
package.json
---------------
{
"name": "webpack_practice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack ./src/app.js ./dist/bundle.js",
"prod": "webpack –p"
},
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^3.8.1"
}
}
Quiz:
1. Executing the following command will automatically create a file named: webpack_practice >> npm init
PACKAGE.JSON
2. Webpack supports third party libraries T
3. The latest version of Webpack is 2X
4. Webpack is not recommended for large web applications F
5. Which of these softwares is necessary to install webpack NODE.JS
6. The distinct feature of webpack is CODE-SPLITTING
7. Which of the following webpack feature enables Code on Demand CODE-SPLITTING
8. Which of the following code can help us make the compilation automatically BUILD...
9. Which helps in using babel with webpack BABEL-LOADER
10. Which of the following tells webpack how to treat a Bundled Code OUTPUT
11. Which of the following code can be used to install the style loader? NPM....--SAVE
12. Which of the following loader can be used to process style sheets CSS-LOADER
13. Webpack understands only __________________ JAVA SCRIPT
14. file:///home/ram/work/webpack_practice/index.html means that the file gets loaded from LOCAL
MACHINE
15. Which of the following tells webpack how to treat a Bundled Code OUTPUT
16. Which of the following features, considered the backbone of Webpack PLUGIN
17. Which of the following code snippet instantiates the plugin FrescoPlugin REQUIRE.........NEW
18. _____________ simplies the creation of HTML files to serve webpack bundles HTML
WEBPACKPLUGIN
19. Plugins are instantiable objects T
20. Webpack can be optimized with the following commandWEBPACK--OPTIMIZE-MINMISE
21. Webpack is written in JAVA SCRIPT
22. Webpack is a/an ______________ tool OPEN SOURCE
23. A plugin can be instantiated with the keyword NEW
24. Which of the following is a Node.js express server? WEBPACK-DEV-SERVER
25. Which of the following makes Webpack extensible Entry
26. In Webpack, we can useNone of the options
27. Custom plugins can be installed via ___________ NPM
28. Webpack starts building the dependency graph from It is Context dependent
29. Which of the following, can be used to define an entry point? module.exports = {entry:
'./path/to/my/entry/file.js'};
30. CoffeeScript can be transformed to JavaScript with the help of LOADER
31. Which of the following code can help us make the compilation automatically build": "webpack
./src/app.js ./dist/bundle.js
32. Which of these softwares is necessary to install webpack Node.js
33. Which of the following is a Node.js express server? webpack-dev-server