- Published on
Modules and Webpack Integration
214 words2 min read
- Authors
- Name
- Curtis Warcup
Modules
Modules are a file that makes some values available to other files and/or consumes values from other files.
The most common types of module system are:
common js | ES Modules |
---|---|
require() | import X from 'X' |
module.exports | export default 123 |
Bundlers are used to take multiple modules and bundle them into a single file. An example is webpack.
Webpack
Example: npm install --save-exact webpack@5.11.1 webpack-cli@4.3.0
// message.js
module.exports = 'Hi there!'
//index.js
const message = require('./message')
console.log(message)
"scripts": {
"build": "webpack --mode=development"
In terminal, run npm run build
, which will generate a file called main.js
in a new file called 'dist' the root of the project.
The bundler does the following:
- Reads the contents of the entry file (index.js)
- Automatically finds all the different require/import/export statements
- Automatically finds all the different modules that are required
- Linked these files together into a single output file with all values being correctly communicated between the files.