From 58634bfb222020aa2e01d733cd6cb93b83fc572d Mon Sep 17 00:00:00 2001 From: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> Date: Mon, 25 Sep 2023 03:46:22 +0100 Subject: [PATCH 1/4] Add webpack lesson content Covers general bundling, loaders and HtmlWebpackPlugin. Adjusts assignment links with new resource and NBs. --- .../webpack.md | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/javascript/organizing_your_javascript_code/webpack.md b/javascript/organizing_your_javascript_code/webpack.md index c382e0f60c3..69ab5c88e7d 100644 --- a/javascript/organizing_your_javascript_code/webpack.md +++ b/javascript/organizing_your_javascript_code/webpack.md @@ -1,7 +1,7 @@ ### Introduction We've already introduced webpack in a previous lesson. It is the go-to tool across the web for bundling and compiling JavaScript code. There _are_ other options out there, but at this time none of them are as popular or widely used as webpack. -In our last lesson, we covered the first half of what webpack can do for you: bundling your modules. Another amazing feature is webpack's ability to process and manipulate your code during the compilation step. So, for example, if you would like to use [Sass](http://sass-lang.com/) to write your CSS, webpack can do that for you. Webpack can manage your images and compress and optimize them for use on the web. Webpack can [minify and uglify](https://stackoverflow.com/questions/33708197/does-it-make-sense-to-do-both-minify-and-uglify/33708348) your code. There are tons of things webpack can do, but to access these functions we need to learn more about loaders and plugins. +In our last lesson, we covered the first half of what webpack can do for you: bundling your modules. Another amazing feature is webpack's ability to process and manipulate your code as it bundles this. So, for example, if you would like to use [Sass](http://sass-lang.com/) or [PostCSS](https://postcss.org/) to write your CSS, webpack can allow you to do that. Webpack can manage your images and compress and optimize them for use on the web. Webpack can [minify and uglify](https://stackoverflow.com/questions/33708197/does-it-make-sense-to-do-both-minify-and-uglify/33708348) your code. There are tons of things webpack can do, but to access these functions we need to learn more about loaders and plugins. ### Lesson overview @@ -9,16 +9,54 @@ This section contains a general overview of topics that you will learn in this l - Use webpack by following its documentation. - Load assets with webpack. - - Manage output with webpack. + - Manage output with webpack and HtmlWebpackPlugin. + - Configure webpack with useful features for development. + + Just in case it helps for any further discussion, I thought I'd write up a quick draft for what I meant above by including our own lesson content in the webpack lesson (since currently it just introduces the lesson overview then dives straight into linking the official tutorials in the assignment with little other context). + +### Bundling + +As briefly introduced in the previous lesson, if you give webpack a file as an entry point, it will build a dependency graph based on all the imports/exports starting there, before bundling everything into a single `.js` file in `dist`. If for whatever reason you needed it to output multiple bundles (e.g. you have multiple html pages that each need their own), then each entry point you give Webpack will produce its own output bundle. + +If you are only dealing with bundling JavaScript then this is fairly straightforward. But what if your project also includes CSS or assets like images or fonts? For CSS, you can import your `.css` file directly into your JavaScript which tells Webpack to handle bundling it. For assets like images, you can load any you use in `src` (whether they are used in your CSS or JavaScript) into `dist`. + +However, it cannot do this by itself. Webpack needs to be configured with the correct loaders and rules so that it knows what to do with any non-JavaScript it encounters whilst trying to bundle everything. If you really wanted to, with the right loaders/plugins/rules, you could even do things such as optimize images when you build your project into `dist`. In the assignment links below, you will get to see how you can install and configure these loaders and rules for CSS and assets. + +### Html-webpack-plugin + +Since we would like to keep all of our development work within `src` and leave `dist` for the production build (the code that you will actually deploy), what about handling HTML files? + +Similarly to how we will need loaders and rules for CSS and assets, we can use a plugin called `html-webpack-plugin` which will automatically build an HTML file in `dist` for us when we build our project. It will also then automatically add certain things to the HTML like our output bundle in a `