import
statements. import
gives us a few benefits.script
tags determined the order of the JavaScript code execution.import
.webpack
command is run.import
statement in the source script.js
file.import
, Webpack retrieves all dependency code. If the imported module depends on other modules, Webpack retrieves those modules too.dist
directory.node index.js
. A request is made to the server. The server responds with the compiled JS file in dist
.webpack-demo
).This is where all our files will live.index.js
,cat.js
and mouse.js
to src
folder. webpack's default entry point is the index.js
file in the src
folder.index.html
and add it to the root directory.npx webpack
in the terminal. Adding "build": "webpack"
to scripts
in package.json
and running npm run build
will also do the same thing. You should see something like this in dist/main.js
:dist
, which contains a file called main.js
. This file contains the processed code from index.js.
<script src="./dist/main.js"><script>
, to reference the new file that webpack has created for us.index.html
in your browserwebpack.config.js
) that allows us to tell webpack what to do.entry
and output
.entry
(line 5): refers to the entry point (the file that webpack starts reading from).output
(line 6): refers to where webpack will put the processed code (line 8) and what the file will be called (line 7). In this case, the file (randomName.js
) will be put in a directory called RANDOM
(The directory will be created if it does not exist). Usually we stick with convention and put a file called main.js
in a folder called dist
path.resolve
- resolves an absolute path to the directory called RANDOM