Irbisa · Cheatsheet
Frontend Tooling & Build
Bundlers, Vite and webpack, esbuild, SWC and Babel, tree shaking, Fast Refresh, source maps, environment variables, browser targets and polyfills, npm, semver and lockfiles, linting, supply-chain safety
What does a bundler do, and why do we still bundle for production when browsers support ES modules natively?
How do environment variables work in a Vite app, and why is a key in
VITE_API_KEYvisible to every user?// .env VITE_API_URL=http://localhost:8080// .env.production VITE_API_URL=https://api.example.com// .env.local API_SECRET=change-me (no prefix, git-ignored) // replaced with the literal string at build timeconst apiUrl = import.meta.env.VITE_API_URL;…What is the difference between
dependencies,devDependenciesandpeerDependenciesinpackage.json?{ "name": "@acme/date-picker", "version": "2.1.0", "dependencies": { "date-fns": "^4.1.0" },…What do
^1.2.3and~1.2.3mean inpackage.json, and why do you commit the lockfile and runnpm ciin CI?^1.2.3 >=1.2.3 <2.0.0 new minor and patch versions~1.2.3 >=1.2.3 <1.3.0 new patch versions only^0.2.3 >=0.2.3 <0.3.0 below 1.0.0 a minor bump counts as breaking1.2.3 exactly 1.2.3 npm install resolves the ranges, may rewrite package-lock.json…How does Vite work in development and in a production build, and why does its dev server start faster than webpack's?
What is Hot Module Replacement, and how does React Fast Refresh keep component state when you save a file?
Find the bug: every edit to
ThemeProvider.tsxreloads the whole page and clears the half-filled form instead of hot-updating.// ThemeProvider.tsx (main.tsx renders <ThemeProvider><App /></ThemeProvider>)import { createContext, useContext, type ReactNode } from 'react'; export const ThemeContext = createContext<'light' | 'dark'>('light'); export function ThemeProvider({ children }: { children: ReactNode }) { return <ThemeContext value="dark">{children}</ThemeContext>;} export function useTheme() { return useContext(ThemeContext);}In webpack, what is the difference between a loader and a plugin?
// webpack.config.jsconst HtmlWebpackPlugin = require('html-webpack-plugin');const MiniCssExtractPlugin = require('mini-css-extract-plugin'); module.exports = { entry: './src/main.tsx',…What is the difference between transpiling and bundling, and what do Babel, SWC, esbuild and
tsceach do in a React build?// types.tsexport interface User { id: string; name: string;}export const GUEST: User = { id: '0', name: 'Guest' };…Which of these imports can the bundler tree-shake, and what keeps the others from being removed?
// utils.ts exports formatPrice and parseCsvimport { formatPrice } from './utils';// lodash ships CommonJS, lodash-es ships ES modules with "sideEffects": falseimport { debounce } from 'lodash';import { throttle } from 'lodash-es';// dates.ts exports 40 small date helpersimport * as dates from './dates';// setupI18n.ts calls i18n.init() at the top levelimport './setupI18n'; export function bind(input: HTMLInputElement, helper: keyof typeof dates) { const fmt = dates[helper]; // chosen at runtime input.oninput = debounce(() => fmt(input.value), 300); window.onscroll = throttle(() => console.log(formatPrice(9.99)), 100);}Why does a production build put a hash in every file name, and when do you put a file in
public/instead of importing it?import logoUrl from './assets/logo.svg'; // becomes /assets/logo-<hash>.svgimport heroUrl from './assets/hero.webp'; export function Header() { return ( <header>…What are source maps, and should you ship them to production?
import { defineConfig } from 'vite';import react from '@vitejs/plugin-react';import { sentryVitePlugin } from '@sentry/vite-plugin'; export default defineConfig({ build: {…How do you decide which browsers to support, and how do
browserslist, syntax transpiling and polyfills relate?import { defineConfig } from 'vite';import react from '@vitejs/plugin-react';import legacy from '@vitejs/plugin-legacy'; export default defineConfig({ // syntax is lowered to this list; no polyfills are added…How do npm, Yarn and pnpm differ, and what is a phantom dependency?
What does ESLint catch that TypeScript does not, and how do you split the work between ESLint, Prettier and
tscin CI?// eslint.config.jsimport js from '@eslint/js';import tseslint from 'typescript-eslint';import reactHooks from 'eslint-plugin-react-hooks';import { reactRefresh } from 'eslint-plugin-react-refresh';import { defineConfig, globalIgnores } from 'eslint/config';…What does the
exportsfield inpackage.jsondo, and why does a package sometimes break when imported from ESM or CommonJS?{ "name": "@acme/date-utils", "version": "2.0.0", "type": "module", "exports": { ".": {…The dev server takes a minute to start and the production build takes ten. How do you find the cause and speed them up?
How do you protect a frontend project from a compromised or malicious npm package?
# pnpm-workspace.yamlminimumReleaseAge: 1440 # minutes: skip versions published less than a day agominimumReleaseAgeExclude: - '@acme/*' # our own packages may update at onceallowBuilds: # the only dependencies allowed to run install scripts esbuild: true…