Skip to content

Commit a1f1818

Browse files
committed
update vite notes
1 parent d6b1fd6 commit a1f1818

2 files changed

Lines changed: 45 additions & 12 deletions

File tree

03-bundling/06-vite/01-basic/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ Our goal with these series of exercises is to show, step by step, how to configu
131131

132132
or alternatively, to customize server port:
133133

134+
```diff
135+
"build": "vite build",
136+
+ "preview": "vite preview --port 1234"
137+
```
138+
139+
or
140+
134141
```bash
135142
npm run preview -- --port 1234
136143
```
@@ -201,7 +208,8 @@ Our goal with these series of exercises is to show, step by step, how to configu
201208
- Go to `Network` tab and refresh (F5) the app to populate the requests panel (if necessary).
202209
- Check how your browser is dowloading:
203210
- Module `index.js`, this is your app only module that containes the code.
204-
- Module `client` which is a `vite` runtime to add an overlay on top of your app to give you error feedback. This module imports another one called `env.mjs` for environment variables.
211+
- Module `client` which is a `vite` runtime injected by vite dev server to establish websockets communication in order to manage and orchestrate HMT. It also adds an overlay on top of your app to give you error feedback. This module imports another one called `env.mjs` for environment variables.
212+
- `env.mjs` is a special module to inject environment variables dynamically in development. However, in production, these variable values are resolved during build time, so, their values are directly injected/replaced in the resulting code after compilation (no dynamic injection is used then).
205213
- Reload the app again several times (F5). Now check that modules requests are returning `304 Not modified` so `vite` dev server is telling your browser to grab those modules from its cache. No data is transfered over the network apart from the request response. Cache is working for source code!
206214
- Now make a new modification in `index.js` to trigger a code update and reload:
207215
_src/index.js_

03-bundling/06-vite/02-custom-css/README.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,14 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
5555
</body>
5656
```
5757

58-
- Now it's time to check it out! Run the development server:
5958

60-
```bash
61-
npm start
62-
```
63-
64-
👍🏼 `vite` supports css out of the box! No need for plugins or configuration.
65-
66-
🔍 Pay attention to the requests made by your browser, a new `mystyles.css` module is now being requested.
67-
You can also check `Elements` tab to see how your new styles are injected in the `<head>` tag.
68-
69-
- Finally, let's check the production bundle as well. Run the build script like this:
59+
- Let's start checking the result for production. Run the build script like this:
7060

7161
```bash
7262
npm run build
7363
```
64+
65+
👍🏼 `vite` supports css out of the box! No need for plugins or configuration.
7466

7567
🔍 Open file `dist/index.html` and notice how there is a new `<link>` tag created for us to reference the CSS file:
7668

@@ -79,3 +71,36 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
7971
```
8072

8173
🔍 Also notice our CSS file has been renamed to `index-<hash>.css` and its content has been minified.
74+
75+
- Let's run now a preview of the built bundles for production:
76+
77+
```bash
78+
npm run preview
79+
```
80+
81+
🔍 Now check the network requests to see how the browser is downloading the new `css` bundle as `index-<hash>.css`. ⚠ Pay attention to the icon of the assets downloaded.
82+
83+
- Now it's time to check it out in development flow:
84+
85+
```bash
86+
npm start
87+
```
88+
89+
🔍 Pay attention to the requests made by your browser, a new `mystyles.css` module is now being requested. If you do a change in the css file like:
90+
91+
```diff
92+
.red-background {
93+
+ background-color: red;
94+
}
95+
```
96+
97+
Check how now the module has been hot replaced with a new version with the timestamp:
98+
`mystyles.css?t=<timestamp>` ... like any other ES module ... wait ... ⚠ look at the icon of the resource downloaded 💥... it is not CSS, it is just another module!
99+
100+
🔍 Click the resource and check its content:
101+
- It contains a runtime made out by utilities imported from `@vite/client` like `updateStyle` and `removeStyle`.
102+
- This runtime enables hot-injection of new CSS in the application without having to refresh the whole page.
103+
- It uses HMR APIs to replace styles in the browser, removing the old ones and adding the new ones.
104+
105+
106+
🔍 You can also check `Elements` tab to see how your new styles are injected in the `<head>` tag.

0 commit comments

Comments
 (0)