Lab 19 - 'I Want It Now': Frontend Build Tools
Categories:
7 minute read
One of the challenges of building software for the web is the heterogeneity of the platforms that it runs on. Different browsers, different devices, different network conditions, and different user needs all complicate the web as a target. Developer Experience (DX) is a growing concern for dev teams, and there continue to be amazing leaps in tooling to improve it. Today we work with a few gateway tools, just try not to get too carried away with them.
Vite
Vite is a relatively new build tool to the scene that is designed to be fast. It has sooo many features, but we will only be using a few of them today.
Learning Objectives
Have your cake and eat it too with:
- future CSS features in today’s browsers with PostCSS
- future JS features in today’s browsers with Babel
Because frontend code runs in peoples’ browsers, and because people may not update their browsers regularly, it can be a challenge to decide how old a new feature must be before it is safe to use for a target audience. Tools like PostCSS and Babel permit us to write our code (CSS and JS, respectively) using the latest features, and then transpile it to a version that is more widely supported. This means that the tolls know about future features for web technologies (CSS and JS) and then given the developer’s intended “target audience”, the tool will know whether the feature is currently available to that target, and if not, will change the source code from what the developer wrote using the future feature, to a bit more code, but using only features that are currently available to the target. This makes the code easier to read and write, and also makes it more maintainable in the long run, while not sacrificing compatibility with older browsers.
PostCSS: using tomorrow’s CSS today
To follow changes to the CSS specification, consult the W3C’s CSS Working Group’s CSS Current Work page.
On that page you’ll find a list of specifications and their statuses, as well as links to the latest drafts of the specifications.
See for example the Selectors Level 4 specification which is currently listed as being in the Working Draft
stage, which defines among other things the :has()
pseudo-class.
With PostCSS included, vite will not only bundle your CSS files into a single minified file, but it will also transpile your CSS to be compatible with the browsers you specify. We will try out a few features future features in CSS, such as:
- custom media queries
- custom property
- custom selectors
- :has relational pseudo-class
- nesting
rebeccapurple
Babel: using tomorrow’s JS today
Babel does the same thing for JavaScript that PostCSS does for CSS. It will take your modern JavaScript code and transpile it to be compatible with the browsers you specify. We will try out a few features future features in JS, such as:
Steps
🚨 Server
tl;dr - don't use the Live Server extension
vite comes with its own development server.Setup
⚠️ Prereq
NodeJS
You must have already installed NodeJS to complete this lab.- get the code by accepting the github classroom assignment link from your instructor or use this starter code if you’re not one of our students.
- open a terminal to the same directory as the
package.json
file- one easy way to do this is to have opened the directory in vscode and press ctrl+` (or cmd+` on macOS) to open a terminal in that directory
- run
npm install
to install the dependencies of the project (which are listed in thepackage.json
file) - you should notice that a new directory called
node_modules
has been created in the project- the top-level directories within node_modules should include at least those dependencies defined in the
package.json
file, but will typically include very many more. - when you run
npm install
(ornpm i
for short), npm will look at thepackage.json
file and install the dependencies listed there, but then it will continue recursively installing your project’s direct dependencies’ dependencies, as well as the dependencies of those dependencies, and so on, until all dependencies are installed.
- the top-level directories within node_modules should include at least those dependencies defined in the
- run
npm run dev
to start the vite development server - if the browser doesn’t open automatically, you can press o in the terminal to open the browser to the correct address
Future CSS
- Notice how the
Hello Vite!
header element is purple? It’s currently JMU Purple. In order to style it this was from the css file, we must have the following three items. Inspect the element in your browser’s devtools and confirm that you can locate them:- there is some selector that matches the element (what selector do you see in the devtools inspector?)
- on that element the correct property should be identified (what property do you see in the devtools inspector?)
- the value of the property is set to JMU purple (`#450084) (what value do you see in the devtools inspector?)
- in your editor (vscode) look for the css file (it’s in the root, and it’s called
style.css
) - find the 3 things from above, but no in the css source code instead.
- what similarities and differences do you observe?
- in the css file, change things that are currently JMU purple (
#450084) to instead be
rebeccapurple` - once you save your file, you should see that the browser automatically reloads
- check in the browser devtools what color the heading is now set to
Future JS
Let’s add some code at the end of the main.js
file.
- use the
let
keyword to declare aisBig
variable and assign it the value of false - use
let
again to declare a variable callednum
and assign it the value of 100 - log the variables to the console
- reassign num to a larger number, define the number using the
_
separator for dividing the number into groups of 3 digits, like8_675_309
- log the variables to the console again
- let’s try out the logical assign operator
||=
, reassignisBig
to be true ifnum
is greater than 999isBig ||= num > 999
- log the variables to the console again
- kill the vite dev server by pressing (the same for all OSes) ctrl+c in the terminal where it’s running.
- FYI, this is a standard way to stop a running process in a terminal that you might want to remember for your other console work in web dev, and other contexts as well.
- push your code to your github classroom repository.
Tools
- In addition to having a dev server with the “live reloading” feature you’re used to from the vscode live server experience, vite also has a build command that will create a
dist
directory with the files that have been prepared for production, usually including the transpiling we’re exploring today as well as minifying. Run the build command withnpm run build
in the terminal. - find the newly created
dist
directory in the project and look through the files in it. You should see that:- in the css file, the value
rebeccapurple
has been replaced with a hex value - in the js file, the
_
separator has been removed from the large number - in the js file, the
||=
operator has been replaced with a more verbose version of the same logic.
- in the css file, the value
- take a look at
vite.config.js
:- see that the target audience for this app is set to be Firefox 69. see the commented out targets to see some of the other ways that we could specify the targets
- if there’s time, try changing the target and then running npm run build and seeing how the js and/or css changes.
- when I check the disk usage of this project at the time of this writing, the
node_modules
directory is about63MB
, while the actual source code for this project is only332KB
, and the built version is24KB