Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack: Rewrite lesson with in-house tutorial (alongside ES6 Modules rewrite) #27961

Merged
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b733a69
Write intro and LO items
MaoShizhong May 9, 2024
814d70a
Write general bundling intro section
MaoShizhong May 9, 2024
7dad7d4
Begin webpack tutorial section
MaoShizhong May 9, 2024
ecafbfe
Add upcoming section headings
MaoShizhong May 9, 2024
f189421
Write JS bundling instructions
MaoShizhong May 9, 2024
f8442d7
Add HtmlWebpackPlugin setup instructions
MaoShizhong May 9, 2024
795d210
Write CSS loading sections
MaoShizhong May 9, 2024
934aaaf
Set out image loading subsections
MaoShizhong May 9, 2024
5a0bd0a
Write html-loader instructions
MaoShizhong May 10, 2024
b8fe052
Restructure Webpack image handling section
MaoShizhong May 10, 2024
26c8316
Add note on necessity of loaders/plugins
MaoShizhong May 10, 2024
be785f6
Add note about Webpack hashed asset names
MaoShizhong May 10, 2024
7b78c28
Add instructions for setting up webpack-dev-server
MaoShizhong May 10, 2024
4a0b3fe
Round off Webpack lesson contents
MaoShizhong May 10, 2024
0a841c4
Tweak code examples formatting
MaoShizhong May 10, 2024
047cb2f
Add assignment and KC content
MaoShizhong May 10, 2024
3d9e9b2
Fix grammar and spelling across the file
MaoShizhong May 10, 2024
fc5a8ee
Add co-author
MaoShizhong May 10, 2024
57731dd
Rename new webpack lesson to overwrite existing
MaoShizhong May 10, 2024
83ee4e2
Fix list child spacing
MaoShizhong May 10, 2024
1cce35e
Amend Restaurant Page instructions to account for Webpack rewrite
MaoShizhong May 11, 2024
fdcf5cb
Remove npm script step
MaoShizhong May 11, 2024
4ce4f52
Fix code examples
MaoShizhong May 12, 2024
fec423b
Add link and KC for source map summary
MaoShizhong May 12, 2024
d89d9fa
Reference npm lesson for package.json and gitignore details
MaoShizhong Jul 1, 2024
2a70b62
Remove redundant duplicate setup instructions from Restaurant Page
MaoShizhong Jul 1, 2024
972ff87
Merge branch 'main' into feature/webpack-lesson-rewrite
MaoShizhong Jul 1, 2024
f130d54
Add note box on extension resolution for imports
MaoShizhong Jul 6, 2024
9581f45
Format extension resolution note box
MaoShizhong Jul 6, 2024
db24e2c
Add small section clarifying reasons for importing CSS
MaoShizhong Jul 11, 2024
0ef6904
Merge branch 'main' into feature/webpack-lesson-rewrite
MaoShizhong Aug 11, 2024
f666f7b
Fix typo
MaoShizhong Aug 11, 2024
aed488b
Remove unnecessary blank lines between lis
MaoShizhong Aug 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,49 @@ Let's use what we've learned and take a chance to continue practicing DOM manipu
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.

</div>

### Assignment

<div class="lesson-content__panel" markdown="1">

1. Start the project the same way you began the webpack tutorial project.
1. Run `npm init` in your project directory to generate a `package.json` file.
MaoShizhong marked this conversation as resolved.
Show resolved Hide resolved

1. Run `npm install webpack webpack-cli --save-dev` to install webpack to the node_modules directory of your project.
1. Run `npm init -y` in your project directory to generate a `package.json` file.

1. Create a `src` and `dist` directory with the following contents:
1. An `index.js` file in `src`.
1. Install and set up Webpack like how things were shown in the previous lesson.

1. An `index.html` file in `src`. This file will not need a script tag, because we're using `html-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.
- Remember, you only need to install and configure the things you need for your project. For example, if you do not plan to have local image files linked in your HTML template, you will not need to install and configure `html-loader`.

1. Create a `webpack.config.js` file that looks just like our file from the [Webpack "Getting Started" tutorial](https://webpack.js.org/guides/getting-started/#using-a-configuration). Don't forget to add the `html-webpack-plugin` config to your `webpack.config.js` and set its `template` option with a path to `src/index.html`.
- Make sure you have a `webpack.config.js` file with any necessary configuration in it, such as HtmlWebpackPlugin, Webpack dev server etc.

1. Create a `.gitignore` file in the root of your project. It should contain `node_modules` and `dist` on separate lines.
1. Create a `src` directory with the following contents:

1. An `index.js` file, which will be your entry point in `webpack.config.js`.

1. 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">`.
1. A `template.html`. This file will not need a script tag, because we're using `html-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. Make sure your `webpack.config.js` file has its `HtmlWebpackPlugin`'s `template` option set to use this file.

1. Inside of `src/index.js` write a `console.log` or `alert` statement and then run `npx webpack`. Load up `dist/index.html` in a browser to make sure everything is working correctly.
1. Create a `.gitignore` file in the root of your project. It should contain `node_modules` and `dist` on separate lines.

1. Set up an HTML skeleton inside of `src/template.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">`.

- Quick tip: If you run `npx webpack --watch` you will not have to rerun webpack every time you make a change.
1. Inside of `src/index.js` write a `console.log` or `alert` statement and then run `npx webpack serve`. Open [https://localhost:8080](https://localhost:8080) in your browser and check your JavaScript is running.

1. 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.

1. 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 to `div#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 of `index.js`.

1. 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](https://web.archive.org/web/20221024060550/https://eckben.github.io/bearysBreakfastBar/) for visual inspiration.
1. Next, set up your restaurant site to use tabbed browsing to access the Menu and Contact pages. Look at the behavior of this [student's live preview site](https://web.archive.org/web/20221024060550/https://eckben.github.io/bearysBreakfastBar/) for visual inspiration.

1. 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.

1. 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 of `div#content` and then runs the correct 'tab module' to populate it with the new contents again.

1. 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 the `dist` folder, but GH pages is looking for an `index.html` in the root directory of your project.

1. Follow the instructions on this [gist about deploying your dist subdirectory to GitHub pages](https://gist.github.com/cobyism/4730490). 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 the `scripts` 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 added `test` script.
- For Example:
1. Let's deploy to GitHub pages! First, we will need to bundle our application into `dist` by running `npx webpack`. Unfortunately, we need to do a little more work to deploy to GitHub pages, because GitHub Pages tries to look for an `index.html` *in the root of your project*, but yours is inside `dist`! We will need to do a few steps to push *the contents* of your `dist` directory to its own branch on GitHub, which will then have a root-level `index.html` for GitHub pages to serve.

`"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](https://www.youtube.com/watch?v=REdzp64dijs).
1. Follow the instructions in this [gist about deploying your dist subdirectory to GitHub pages](https://gist.github.com/cobyism/4730490). EZPZ!

1. Recall that the **source branch** for GitHub Pages is set in your repository's settings.
1. Recall that the **source branch** for GitHub Pages is set in your repository's settings. Get this changed to the `gh-pages` branch.

</div>
Loading
Loading