https://webpack.js.org/guides/getting-started/
Getting Started
webpack is used to compile JavaScript modules. Once installed, you can interact with webpack either from its CLI or API. If you‘re still new to webpack, please read through the core concepts and this comparison to learn why you might use it over the other tools that are out in the community.
The minimum supported Node.js version to run webpack 5 is 10.13.0 (LTS)
Basic Setup
NPM Scripts
Given it‘s not particularly fun to run a local copy of webpack from the CLI, we can set up a little shortcut. Let‘s adjust our package.json by adding an npm script:
package.json
{
"name": "webpack-demo",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^5.4.0",
"webpack-cli": "^4.2.0"
},
"dependencies": {
"lodash": "^4.17.20"
}
}
Now the npm run build
command can be used in place of the npx
command we used earlier. Note that within scripts
we can reference locally installed npm packages by name the same way we did with npx
. This convention is the standard in most npm-based projects because it allows all contributors to use the same set of common scripts.
Now run the following command and see if your script alias works:
$ npm run build
...
[webpack-cli] Compilation finished
asset main.js 69.3 KiB [compared for emit] [minimized] (name: main) 1 related asset
runtime modules 1000 bytes 5 modules
cacheable modules 530 KiB
./src/index.js 257 bytes [built] [code generated]
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
webpack 5.4.0 compiled successfully in 1940 ms
Custom parameters can be passed to webpack by adding two dashes between the
npm run build
command and your parameters, e.g.npm run build -- --color
.
Conclusion