Introduction
Let’s use what we’ve learned and take a chance to continue practicing DOM manipulation by dynamically rendering a restaurant homepage! By the end, we are going to be using JavaScript alone to generate the entire contents of the website!
Note: DOM elements should be created using JavaScript but styling can be done in a separate CSS file.
.gitignore
When working with packages that are installed with npm, you don’t need to track the contents of node_modules
with git, nor push those files to GitHub. This is because the package.json
file contains all the information, so that anyone can clone your project and install them on their machine with npm install
.
You can make a .gitignore
file in the root of the project, and by writing file or directory names in it, you can tell git what things you don’t want to track. It’s customary to add node_modules
to .gitignore
, since it can get really big.
Assignment
- Start the project the same way you began the webpack tutorial project.
-
Run
npm init
in your project directory to generate apackage.json
file. -
Run
npm install webpack webpack-cli --save-dev
to install webpack to the node_modules directory of your project. -
Create a
src
anddist
directory with the following contents:-
An
index.js
file insrc
. -
An
index.html
file insrc
. This file will not need a script tag, because we’re usinghtml-webpack-plugin
, which automatically adds that in. You will also not need to link a CSS stylesheet as you should be importing it into your JavaScript and letting your webpack configuration handle bundling. -
Create a
webpack.config.js
file that looks just like our file from the Webpack “Getting Started” tutorial. Don’t forget to add thehtml-webpack-plugin
config to yourwebpack.config.js
and set itstemplate
option with a path tosrc/index.html
.
-
-
-
Create a
.gitignore
file in the root of your project. It should containnode_modules
anddist
on separate lines. -
Set up an HTML skeleton inside of
src/index.html
. Inside the body, add a<header>
element that contains a<nav>
with buttons (not links!) for different “tabs” (for example buttons for “Home”, “Menu” or “About” etc). Below the<header>
, add a single<div id="content">
. -
Inside of
src/index.js
write aconsole.log
oralert
statement and then runnpx webpack
. Load updist/index.html
in a browser to make sure everything is working correctly.- Quick tip: If you run
npx webpack --watch
you will not have to rerun webpack every time you make a change.
- Quick tip: If you run
-
Inside
div#content
, create a homepage for your restaurant. You might want to include an image, headline, and some text about how wonderful the restaurant is; you do not have to make this look too fancy. It’s okay to hard-code these into the HTML for now just to see how they look on the page. -
Now remove everything inside
div#content
from the HTML (so you still have the<header>
and<nav>
with an empty<div id="content">
below it) and instead create them by using JavaScript only, e.g. by appending each new element todiv#content
once the page is first loaded. Since we’re all set up to write our code in multiple files, let’s write this initial page-load function inside of its own module and then import and call it inside ofindex.js
. -
Next, set up your restaurant site to use tabbed browsing to access the Contact and Menu pages. Look at the behavior of this student’s live preview site for visual inspiration.
-
Put the contents of each ‘tab’ inside of its own module. Each module will export a function that creates a div element, adds the appropriate content and styles to that element and then appends it to the DOM.
-
Write the tab-switching logic inside of
index.js
. You should have event listeners for each button in the header navbar that wipes out the current contents ofdiv#content
and then runs the correct ‘tab module’ to populate it with the new contents again.
-
-
If you are using GitHub pages to host your completed page you need to do a tiny bit more work to get it to show up. After running
webpack
the full bundled version of your site is available in thedist
folder, but GH pages is looking for anindex.html
in the root directory of your project.- Follow the instructions on this gist about deploying your dist subdirectory to GitHub pages. EZPZ!
- To prevent having to copy and paste the same lengthy git command each time, you can instead create an npm script to do the work for you!
- Inside your project’s
package.json
file, within thescripts
section, add an additional entry named something of your choosing and paste in the command from the above gist surrounded by quotation marks. Follow the formatting of the already addedtest
script. -
For Example:
"scripts": { "YourScriptName": "git subtree push --prefix dist origin gh-pages" }
- Now each time you need to update your project’s live preview, you
npm run <YourScriptName>
in your project’s terminal. - To learn more about this, here’s a short tutorial video on npm scripts.
- Inside your project’s
- To prevent having to copy and paste the same lengthy git command each time, you can instead create an npm script to do the work for you!
- Recall that the source branch for GitHub Pages is set in your repository’s settings.
- Follow the instructions on this gist about deploying your dist subdirectory to GitHub pages. EZPZ!