|
| 1 | +# Code Splitting |
| 2 | + |
| 3 | +In this example, we are going add TailwindCSS integration. |
| 4 | + |
| 5 | +📌 We start from sample `09-tailwindcss`. |
| 6 | + |
| 7 | +# Steps to build it |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they are not already installed on your computer. |
| 12 | + |
| 13 | +> ⚠ Verify that you are running at least latest Node LTS version and npm. You can check your current version by running `node -v` and `npm -v` in a terminal/console window. Older versions may produce errors. |
| 14 | +
|
| 15 | +## Steps |
| 16 | + |
| 17 | +- We start from `09-tailwindcss`. Just copy the project and install: |
| 18 | + |
| 19 | + ```bash |
| 20 | + npm install |
| 21 | + ``` |
| 22 | + |
| 23 | +- Let's run the project: |
| 24 | + |
| 25 | + ```bash |
| 26 | + npm start |
| 27 | + ``` |
| 28 | + |
| 29 | + 🔎 Navigate to [http://localhost:5173](http://localhost:5173). |
| 30 | + |
| 31 | +Code splitting is a performance optimization technique in frontend development where the application's JavaScript is broken into smaller, separate chunks that can be loaded on demand, rather than all at once. |
| 32 | + |
| 33 | +This means parts of the code are only loaded when needed (e.g. when a user navigates to a specific page or triggers a certain action), which reduces the initial bundle size, improves load times, and enhances user experience. |
| 34 | + |
| 35 | +In this example we're going to use a module that is loaded on demand. |
| 36 | + |
| 37 | +- Let's add _src/math.ts_ file. |
| 38 | + |
| 39 | + _src/math.ts_ |
| 40 | + |
| 41 | + ```ts |
| 42 | + const randomBetween = (min: number, max: number) => |
| 43 | + Math.floor(Math.random() * (max - min + 1)) + min; |
| 44 | + |
| 45 | + export const operate = (n: number): number => { |
| 46 | + const base = Math.min(n, randomBetween(0, 50)); |
| 47 | + const multiplier = randomBetween(1, 15); |
| 48 | + return base + multiplier; |
| 49 | + }; |
| 50 | + ``` |
| 51 | + |
| 52 | + We're going to use the `operate` function to apply a math operation to our counter. |
| 53 | + |
| 54 | +- Modify `src/hello.tsx` to import the module dynamically and apply the `operate` function the the `counter` state: |
| 55 | + |
| 56 | + ```diff |
| 57 | + }, []); |
| 58 | + |
| 59 | + + const applyOperation = async () => { |
| 60 | + + const { operate } = await import("./math"); |
| 61 | + + setCounter((prevCounter) => operate(prevCounter)); |
| 62 | + + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <> |
| 66 | + <h2>Hello from React</h2> |
| 67 | + <p>Api server is {config.API_BASE}</p> |
| 68 | + <p>Feature A is {config.IS_FEATURE_A_ENABLED ? "enabled" : "disabled"}</p> |
| 69 | + <p>Counter state: {counter}</p> |
| 70 | + + <button |
| 71 | + + className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" |
| 72 | + + onClick={applyOperation} |
| 73 | + + > |
| 74 | + + Apply operation |
| 75 | + + </button> |
| 76 | + <a |
| 77 | + href="#" |
| 78 | + className="m-2 block max-w-sm p-6 bg-white border border-gray-200 rounded-lg shadow-sm hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700" |
| 79 | + > |
| 80 | + ``` |
| 81 | + |
| 82 | + Notice we imported the `math` module dynamically using `import()` syntax. Vite will extract this module into its own bundle and will rely on native `import()` to download it at the time the button is clicked. Even if the user clicks multiple times on the button the module will be downloaded once. |
| 83 | + |
| 84 | + Try yourself by clicking the button! |
| 85 | + |
| 86 | +- Perform a build using `npm run build`: |
| 87 | + |
| 88 | + ```bash |
| 89 | + npm run build |
| 90 | + ``` |
| 91 | + |
| 92 | + Notice two JavaScript files has been created under `dist/assets`: `index-[hash].js` and `math-[hash].js`. |
0 commit comments