First your need to create a directory, clone the project and install the dependencies.
→ mkdir webpack-starter
→ git clone https://github.com/MenaiAla/webpack-config-starter
→ npm install
Inside index.html
, there are implicit dependencies between the <script>
. index-no-module.js
depends on lodash
being
included before it runs. This is because index-no-module.js
never explicitly declared a need for lodash; it just assumes that the global variable _ exists.
This is traditional way for managing legacy JavaScript projects and web projects in general.
There are problems with managing JavaScript projects this way:
- It is not immediately apparent that the script depends on an external library.
- If a dependency is missing, or included in the wrong order, the application will not function properly.
- If a dependency is included but not used, the browser will be forced to download unnecessary code.
- Check and read
index.html
. - Open
index.html
in your browser.
Webpack +4 does not require webpack.config.js for basic configuration
- Check and read
index.js
. - Notice that I added
import _ from "lodash"
in the script. - Check and read
index.html
indist
folder. - Notice that I removed
lodash
script fromindex.html
.
npx webpack
npx webpack
takes the scriptindex.js
and generate amain.js
as the output indist
folder.
Most of Javascript projects need complex webpack configuration.
- Check and read
webpack.config.js
. - run
npx webpack --config webpack.config.js
in your terminal.
If a webpack.config.js is present, the webpack command picks it up by default. I used the --configoption here only to show that you can pass a configuration of any name. This will be useful for more complex configurations that need to be split into multiple files.
It's bad practice to run long command npx webpack --config webpack.config.js
each time. For that reason, we created a shortcut in package.json like this :
"build": webpack
npm run build