Skip to content

Commit b3130ff

Browse files
author
Wickramaranga Abeygunawardhana
authored
Merge pull request #63 from umstek/upgrade/2021-dec
Upgrade dependencies and Readme
2 parents 47cb7de + 88f4bab commit b3130ff

File tree

4 files changed

+833
-1190
lines changed

4 files changed

+833
-1190
lines changed

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "parcel-typescript-react-tailwind",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"license": "MIT",
55
"description": "Trying-out Tailwind CSS with Parcel",
66
"scripts": {
@@ -9,18 +9,18 @@
99
"clean": "rm -rf .parcel-cache dist"
1010
},
1111
"devDependencies": {
12-
"@parcel/transformer-image": "2.0.0",
13-
"@types/react": "17.0.30",
14-
"@types/react-dom": "17.0.9",
15-
"autoprefixer": "10.3.7",
16-
"parcel": "2.0.0",
17-
"postcss": "8.3.9",
18-
"typescript": "4.4.4"
12+
"@parcel/transformer-image": "2.0.1",
13+
"@types/react": "17.0.37",
14+
"@types/react-dom": "17.0.11",
15+
"autoprefixer": "10.4.0",
16+
"parcel": "2.0.1",
17+
"postcss": "8.4.4",
18+
"typescript": "4.5.3"
1919
},
2020
"dependencies": {
2121
"postcss-import": "^14.0.1",
2222
"react": "^17.0.2",
2323
"react-dom": "^17.0.2",
24-
"tailwindcss": "^2.2.4"
24+
"tailwindcss": "^3.0.1"
2525
}
2626
}

readme.md

Lines changed: 34 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Trying-out Tailwind CSS with Parcel
22

3-
Some years ago, I was searching for a UI kit to use in one of my hobby react apps. I found some good-looking React UI kits like [Ant Design](http://ant.design), [BlueprintJS](https://blueprintjs.com) and [Evergreen](https://evergreen.segment.com) but sometimes the bloat becomes unbearable and customizability becomes a priority. [Material UI](https://material-ui.com) is said to be the most popular one, but, no thanks; not a fan of material UI. Anyway, the discussion on available react UI kits is a topic for a different post. Here what happened was that I tried to create my own UI kit with SASS and soon found out that there is a gap between my idea on how the components should look and my knowledge on how to use CSS properly.
3+
Few years ago, I was searching for a UI kit to be used in one of my hobby react apps. I found some good-looking React UI kits like [Ant Design](http://ant.design), [BlueprintJS](https://blueprintjs.com) and [Evergreen](https://evergreen.segment.com) but sometimes the bloat becomes unbearable and customizability becomes a priority. [Material UI](https://material-ui.com) is said to be the most popular one, but, no thanks; not a fan of material UI. Anyway, the discussion on available react UI kits is a topic for a different post. Here what happened was that I tried to create my own UI kit with SASS and soon found out that there is a gap between my idea on how the components should look and my knowledge on how to use CSS properly.
44

55
# What is Tailwind CSS?
66

@@ -18,27 +18,27 @@ First I’ve created the `tailwind-test` folder and initialized the project with
1818
First add parcel bundler; this takes care of how to load, process and bundle all the `.tsx`, .`css`, `.html` etc. you’re going to create.
1919

2020
```sh
21-
yarn add --dev parcel-bundler
21+
yarn add --dev parcel
2222
```
2323

2424
Then add Tailwind CSS as stated in the documentation.
2525

2626
```sh
27-
yarn add tailwindcss
27+
yarn add tailwindcss postcss autoprefixer
2828
```
2929

3030
Add the below `scripts` section to your `package.json` so that you can run, build and clean the project easily.
3131

3232
```json
3333
"scripts": {
34-
"start": "parcel ./src/index.html --open",
35-
"build": "parcel build ./src/index.html",
36-
"clean": "rm -rf dist .cache"
34+
"start": "parcel serve ./src/index.html --open",
35+
"build": "parcel build --dist-dir dist src/index.html",
36+
"clean": "rm -rf .parcel-cache dist"
3737
},
3838
```
3939

4040
Create the `src` folder and create the `index.html` file with a basic HTML5 template. You can also use `html:5` snippet/emmet if you’re using [vsocde](https://code.visualstudio.com/).
41-
Add `<div class="app"></div>` and `<script src="./main.tsx"></script>` inside body, so that React can mount your app there.
41+
Add `<div class="app"></div>` and `<script src="./main.ts"></script>` inside body, so that React can mount your app there.
4242

4343
```html
4444
<!DOCTYPE html>
@@ -51,28 +51,19 @@ Add `<div class="app"></div>` and `<script src="./main.tsx"></script>` inside bo
5151
</head>
5252
<body>
5353
<div id="app"></div>
54-
<script src="./main.tsx"></script>
54+
<script type="module" src="./main.ts"></script>
5555
</body>
5656
</html>
5757
```
5858

59-
Create the `main.tsx` and add your React app there. Note that we have added a [button](https://tailwindcss.com/components/buttons) which uses Tailwind styles with utility classes. Utility classes is [not the only way](https://tailwindcss.com/docs/extracting-components) you can add Tailwind styles. Since we’re trying Tailwind with React, utility classes is enough for us right now.
59+
Create the `main.ts` and add your React app there. Note that we have added a custom card component which uses Tailwind styles with utility classes. `src/components/Card/index.tsx` and `src/views/App.tsx` are omitted for clarity. Utility classes is [not the only way](https://tailwindcss.com/docs/reusing-styles) you can add Tailwind styles. Since we’re trying Tailwind with React, utility classes is enough for us right now.
6060

61-
```tsx
61+
```ts
6262
import * as React from "react";
63-
import { render } from "react-dom";
64-
65-
function App() {
66-
return (
67-
<div>
68-
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
69-
Hello, Tailwind!
70-
</button>
71-
</div>
72-
);
73-
}
63+
import * as ReactDOM from "react-dom";
64+
import { App } from "./views/App";
7465

75-
render(<App />, document.getElementById("app"));
66+
ReactDOM.render(React.createElement(App), document.getElementById("app"));
7667
```
7768

7869
Create `main.css` file and add the below. These are tailwind directives. This is needed to inject tailwind [styles](https://tailwindcss.com/docs/preflight) and utility classes into your CSS.
@@ -85,135 +76,27 @@ Create `main.css` file and add the below. These are tailwind directives. This is
8576
@tailwind utilities;
8677
```
8778

88-
Add `postcss.config.js` file inside the project folder (i.e.: one level up from `src` folder). Tailwind CSS is a [PostCSS](https://postcss.org/) plugin where PostCSS handles all pre/post processing of CSS you write, such as adding [vendor prefixes](https://developer.mozilla.org/en-US/docs/Glossary/Vendor_Prefix) [automatically](https://github.com/postcss/autoprefixer). Parcel has built-in support for PostCSS, but doesn’t know yet about Tailwind, so we have to configure it with the below content. Make sure you include `tailwindcss` before `autoprefixer`.
89-
90-
```js
91-
module.exports = {
92-
plugins: [
93-
require("tailwindcss"),
94-
require("autoprefixer"),
95-
require("postcss-nested"),
96-
],
97-
};
98-
```
99-
100-
Now it’s show-time. Run `yarn start`.
101-
At this moment, if you are curious why we didn’t add typescript or react, don’t worry; parcel will install them automatically — and yes, it knows that TypeScript is a dev dependency. And since you have passed the `--open` flag, it even opens the browser window for you.
102-
103-
You should see something like below.
104-
105-
![Hello, world!](https://paper-attachments.dropbox.com/s_597CE0BBFBFE1EF1D6752296A9DAB2D8A884BE13707C4980DBFA5F0EAEC2575E_1573911903692_image.png)
106-
107-
Done.
108-
109-
# Trying to style my way
110-
111-
I tried playing with it a little. Changed `hover:bg-blue-700` to `hover:bg-blue-400`. Now it displays a lighter color on mouse over.
112-
113-
```tsx
114-
<button className="bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 rounded">
115-
Hello, Tailwind!
116-
</button>
117-
```
118-
119-
![hover:bg-blue-400](https://paper-attachments.dropbox.com/s_597CE0BBFBFE1EF1D6752296A9DAB2D8A884BE13707C4980DBFA5F0EAEC2575E_1573919433381_image.png)
120-
121-
Added an [active](https://tailwindcss.com/docs/pseudo-class-variants#active) background color using `active:bg-blue-900`. (This technique with colon is called Pseudo-class Variants in Tailwind CSS; we use pseudo-class names in `className` rather than in CSS.) Nothing changed. It still shows the hover color on press! What could have gone wrong? It is actually documented right below where the example for `active:` is. We have to add `active` variant to the Tailwind config file `tailwind.config.js`.
122-
123-
```js
124-
module.exports = {
125-
// ...
126-
variants: {
127-
backgroundColor: ["active", "responsive", "hover", "focus"],
128-
},
129-
// ...
130-
};
131-
```
132-
133-
By default, [some of the variants are disabled](https://tailwindcss.com/docs/pseudo-class-variants#default-variants-reference) due to file-size considerations. Even with these disabled, Tailwind is quite [large](https://tailwindcss.com/docs/controlling-file-size). 😕
134-
Anyway, now I configured variants as the above and ran `yarn clean` to clean temp files and started again. We clearly configured tailwind! Or… did we? There is an order how we should organize variants, because, [Tailwind is no magic](https://tailwindcss.com/docs/configuring-variants/#ordering-variants); it generates CSS. CSS precedence applies here too.
135-
I enabled all the variants in the correct order for all the styles with the below. We don’t have file size considerations for this test so that is fine.
136-
137-
```js
138-
module.exports = {
139-
variants: [
140-
"responsive",
141-
"group-hover",
142-
"focus-within",
143-
"first",
144-
"last",
145-
"odd",
146-
"even",
147-
"hover",
148-
"focus",
149-
"active",
150-
"visited",
151-
"disabled",
152-
],
153-
};
154-
```
155-
156-
It works.
157-
158-
![active:bg-blue-900](https://paper-attachments.dropbox.com/s_597CE0BBFBFE1EF1D6752296A9DAB2D8A884BE13707C4980DBFA5F0EAEC2575E_1573922304511_image.png)
159-
160-
# Next steps…
161-
162-
Let’s see the file sizes…
163-
164-
![5.5 MB](https://paper-attachments.dropbox.com/s_597CE0BBFBFE1EF1D6752296A9DAB2D8A884BE13707C4980DBFA5F0EAEC2575E_1573922486261_image.png)
165-
166-
Gosh! It’s huge. This is not the size you want your production CSS bundle to be. We are using a lot of unwanted styles. But how do we reduce the bundle? [Manually selecting](https://tailwindcss.com/docs/controlling-file-size#removing-unused-theme-values) what is necessary using the configuration file is hard. We have to try [something else](https://tailwindcss.com/docs/controlling-file-size#setting-up-purgecss).
167-
168-
> Mozilla's [Firefox Send](https://send.firefox.com/) is built with Tailwind, yet somehow their CSS is only 13.1kb minified, and only 4.7kb gzipped! How?
169-
> They're using [Purgecss](https://www.purgecss.com/),…
170-
171-
~~Let’s add `purgecss`. Run `yarn add @fullhuman/postcss-purgecss -D`. Unfortunately, parcel currently fails to auto install postcss plugins just by looking at its config. I don’t think it’s in parcel’s scope.~~
172-
173-
**Update: As of `tailwindcss` version `1.4.5`, we don't need to configure purgecss this way. So do not install `purgecss` and keep your `postcss.config.js` intact. (The old way would also work, anyway.)**
79+
Add `.postcssrc` file inside the project folder (i.e.: one level up from `src` folder). Tailwind CSS is a [PostCSS](https://postcss.org/) plugin where PostCSS handles all pre/post processing of CSS you write, such as adding [vendor prefixes](https://developer.mozilla.org/en-US/docs/Glossary/Vendor_Prefix) [automatically](https://github.com/postcss/autoprefixer). Parcel has built-in support for PostCSS, but doesn’t know yet about Tailwind, so we have to configure it with the below content. Make sure you include `tailwindcss` before `autoprefixer`.
17480

17581
```js
176-
// const purgecss = require("@fullhuman/postcss-purgecss")({
177-
// // Specify the paths to all of the template files in your project
178-
// content: [
179-
// "./src/**/*.html",
180-
// "./src/**/*.{t,j}sx"
181-
// // etc.
182-
// ],
183-
// // Include any special characters you're using in this regular expression
184-
// defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
185-
// });
186-
module.exports = {
187-
plugins: [
188-
require("tailwindcss"),
189-
require("autoprefixer"),
190-
require("postcss-nested"),
191-
// ...(process.env.NODE_ENV === "production" ? [purgecss] : [])
192-
],
193-
};
82+
{
83+
"plugins": {
84+
"postcss-import": true,
85+
"tailwindcss": true,
86+
"postcss-nested": true,
87+
"autoprefixer": true
88+
}
89+
}
19490
```
19591

196-
**Update: But edit the `tailwind.config.css` to look like the below (note the _purge_):**
197-
198-
```js
199-
module.exports = {
200-
purge: ["./src/**/*.html", "./src/**/*.{j,t}sx"],
201-
theme: {
202-
// ...
203-
},
204-
variants: [
205-
//...
206-
],
207-
};
208-
```
92+
Now it’s show-time. Run `yarn` to install dependencies and `yarn start` to start. Since you have specified `--open` in `yarn start`, you’ll see the browser open with the `index.html` file.
20993

210-
Now, let’s check the file sizes…
94+
You should see a card with a title and a description.
21195

212-
![2 KB](https://paper-attachments.dropbox.com/s_597CE0BBFBFE1EF1D6752296A9DAB2D8A884BE13707C4980DBFA5F0EAEC2575E_1573927636877_image.png)
96+
# Old tailwind versions
21397

214-
It’s down to ~~two~~ (**update: a little larger than that**) kilobytes. That’s because we only have a few classes used.
215-
So what exactly does purgecss do? It basically traverses through all our `.tsx` files and finds the class names we have used. Then it removes selectors that match all unused class names from CSS (check that regex!). Ugly, but works. Of course, you have to be careful when dynamically generating CSS class names in react.
216-
At this moment, you should’ve realized that although Tailwind can make our lives easier, it also has its own drawbacks. Working with Tailwind CSS is NOT a no-brainer.
98+
Find my blogpost here
99+
https://umstek.tk/posts/trying-out-tailwindcss-with-parcel/ which includes the original content written for tailwind 1.x and then updated for tailwind 2.x.
217100

218101
# The good, the bad, and the ugly
219102

@@ -227,81 +110,29 @@ I can notice several good things about Tailwind CSS at a glance.
227110

228111
There isn’t much to complain about the library but,
229112

230-
- It can get unintuitive sometimes to configure.
231-
- Gradients, icons, animations ~~and transitions~~ (**update: transitions and transforms are there after v1.2.0**) aren’t built-in. Adding them can be complicated.
113+
- Fonts, Icons, animations aren’t built-in. Adding them can be complicated.
232114
- Advanced controls such as switches, calendars, tables, floating notifications, modals etc. are not available.
233-
- Generated CSS can get quite large if you’re using a lot of features. Using purgecss eliminates this but it’s kind of ugly because it does a string search; not proper parsing. But, again, it doesn’t know what template language/framework we’re using.
234-
- Lack of IDE support (yet). But there are vscode extensions, ~~but none for react~~ (**Update: bradlc.vscode-tailwindcss worked for react**).
235115

236-
# Demo
116+
(I had more points here for the old versions but looks like now tailwind supports pretty much everything you'll need.)
237117

238-
~~The demo below uses Tailwind plugins `tailwindcss-transforms` and `tailwindcss-transitions` because tailwind doesn’t support transforms and transitions out-of-the-box.~~
118+
# Demo
239119

240120
![Demo](https://paper-attachments.dropbox.com/s_597CE0BBFBFE1EF1D6752296A9DAB2D8A884BE13707C4980DBFA5F0EAEC2575E_1574005327689_ezgif.com-video-to-gif.gif)
241121

242122
I created a template with the above plugins as a starting point [here](https://github.com/umstek/parcel-typescript-react-tailwind) on GitHub.
243123

244124
Or, [see it in action](https://parcel-typescript-react-tailwind.vercel.app/).
245125

246-
Tailwind also supports custom themes, variants, plugins etc., but that’s outside the scope of this short post. The documentation is your friend.
247-
248-
# Updates
249-
250-
## 2020 October Update 1
251-
252-
Get ready for v2.0 upgrade, remove warning messages.
253-
254-
`tailwind.config.js`.
255-
256-
```js
257-
future: {
258-
removeDeprecatedGapUtilities: true,
259-
purgeLayersByDefault: true,
260-
},
261-
```
262-
263-
## 2021 May Update 1
264-
265-
Upgraded parcel bundler to 2.x; this requires the image URLs to be changed.
266-
267-
```diff
268-
- import cardImage from "../../static/vitalik-vynarchyk-0PePaKnEmuM-unsplash.jpg";
269-
+ import cardImage from "url:../../static/vitalik-vynarchyk-0PePaKnEmuM-unsplash.jpg";
270-
```
271-
272-
We need to ignore `.parcel-cache` from committing. (.gitignore). Changed the `clean` command too.
273-
274-
```diff
275-
+ .parcel-cache
276-
```
277-
278-
TypeScript Alias paths are not working; but maybe we can fix it? However, specify relative URL for now.
279-
280-
```diff
281-
- import "~styles/main.pcss";
282-
+ import "../styles/main.pcss";
283-
```
284-
285-
TypeScript has been upgraded to 4.x together with the parcel upgrade. I changed the `tsconfig.json`.
286-
287-
```diff
288-
- "target": "es5",
289-
- "module": "esnext",
290-
+ "moduleResolution": "node",
291-
+ "target": "ESNext",
292-
+ "module": "ESNext",
293-
```
294-
295-
After the 2.x upgrade, `tailwind.config.js` is totally new. We also now use the experimental JIT mode.
126+
# Resources
296127

297-
And we use a `.postcssrc` file instead of `postcss.config.js` to avoid breaking hot reload.
128+
https://headlessui.dev/
298129

299-
# Resources
130+
https://tailwindui.com/
300131

301132
https://www.tailwindtoolbox.com/
302133

303134
https://tailwindcomponents.com/
304135

305-
https://tailwindtemplates.io/cards/
136+
https://tailwindtemplates.io/
306137

307138
https://github.com/aniftyco/awesome-tailwindcss

tailwind.config.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = {
2-
mode: "jit",
3-
purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/**/*.html"],
4-
darkMode: false, // or 'media' or 'class'
2+
content: ["./src/**/*.{js,jsx,ts,tsx}", "./public/**/*.html"],
3+
darkMode: 'media',
54
theme: {
65
extend: {
76
scale: {
@@ -10,8 +9,5 @@ module.exports = {
109
},
1110
},
1211
},
13-
variants: {
14-
extend: {},
15-
},
1612
plugins: [],
1713
};

0 commit comments

Comments
 (0)