This content originally appeared on DEV Community and was authored by CodeOzz
Welcome to my new academy, Webpack Academy !
I will try to share my webpack knowledges !
Let's start !
Fast presentation
From webpack doc:
At its core, webpack is a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles.
To summarise, Webpack
will help you to handle import/export of your file in your project, and it will put all of your code in one file called bundle.
Why use webpack?
Legit question, if you have ever build javascript project, you should now the problems about import js
file in your application !
I will show you a brief illustration of what we did before !
<script src="jquery.min.js"></script>
<script src="jquery.some.plugin.js"></script>
<script src="main.js"></script>
We need to import all dependencies on by one and in the correct order
, if we want to import a new lib, we need to import it in the correct order
, because if this new lib depends on jquery
, we need to import it after import jquery
!
You think you can handle it, yes you can but imagine if your need to import 100 libs, this can be CHAOTIC.
At this moment we create manually bundle file
// build-script.js
const scripts = [
"jquery.min.js",
"jquery.some.plugin.js",
"main.js",
].concat().uglify().writeTo("bundle.js")
<script src="bundle.js"></script>
We fix the multiple import of scripts, but don't fix the dependencies between libs.
Web + Pack = Webpack
Webpack handle only .js
and .json
file natively. But we will see in the next article how to handler other file !
You only need to give an entry point to webpack, and it will create a dependencies tree ? from this file and check all other file !
From now, we don't need to handle dependencies order by our hands !
Example
Context: We have two files that export one variable:
first.js
export const first = 1
second.js
export const second = 2
We have another file that import both file
three.js
import { first } from './first'
import { second } from './second'
export const three = first + second
And finally the main file that will console.log the three
variable
main.js
import { three } from './three'
console.log(three)
With the old tips we should use something like this:
// build-script.js
const scripts = [
"one.js",
"second.js",
"three.js",
"main.js",
].concat().uglify().writeTo("bundle.js")
<script src="bundle.js"></script>
But with webpack
, we don't need to handle dependencie order !
Let's run our code with the webpack.config.js
const path = require("path")
const config = {
mode: "development",
// Webpack start from this entry point
entry: {
myApp: [
"./src/main.js",
],
},
// This is the output of Webpack
output: {
// From current folder + dist folder that will contains all bundle
path: path.resolve(__dirname, "dist/"),
filename: "bundle.js"
},
}
module.exports = config
Don't be afraid by this config file
, during the academy we will add and change few, but a the end you will understand all line !
To summarise this config, we indicate to webpack
the entry point of our project, and we indicate the output file name and path !
Let run webpack
command ! (you need to install webpack CLI before)
And finally go to dist folder and check the bundle.js
file.
Try it
node dist/bundle.js
3
Yes it's work !
If you want to try, fetch my repository from this commit ! ?
https://github.com/Code-Oz/webpack-academy/tree/39152794a408bf17a99eebc7833e1fee992fb218
I hope you want to learn more about webpack
in my academy !
If you want to have nice article to read about web dev, you can subscribe to my FREE newsletter at this url -> https://codeoz.substack.com/welcome
And you can follow me on :
Twitter : https://twitter.com/code__oz
Github: https://github.com/Code-Oz
And if you want to buy me a coffee :D -> https://www.buymeacoffee.com/CodeoZ
This content originally appeared on DEV Community and was authored by CodeOzz
CodeOzz | Sciencx (2021-07-30T11:14:47+00:00) Webpack Academy #0: What is webpack and why use it?. Retrieved from https://www.scien.cx/2021/07/30/webpack-academy-0-what-is-webpack-and-why-use-it/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.