Skip to content

Commit 363a3d9

Browse files
committed
perf: tree shaking Bootstrap CSS (#1736)
1 parent ddb48ed commit 363a3d9

13 files changed

+69
-34
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ package-lock.json
1717

1818
# IDE configurations
1919
.idea
20+
.vscode
21+
!.vscode/settings.json
22+
!.vscode/extensions.json
2023

2124
# Misc
25+
_sass/dist
2226
assets/js/dist

_config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ exclude:
214214
- tools
215215
- README.md
216216
- LICENSE
217-
- rollup.config.js
217+
- "*.config.js"
218218
- package*.json
219219

220220
jekyll-archives:

_data/origin/basic.yml

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ webfonts: /assets/lib/fonts/main.css
44

55
# Libraries
66

7-
bootstrap:
8-
css: /assets/lib/bootstrap/bootstrap.min.css
9-
107
toc:
118
css: /assets/lib/tocbot/tocbot.min.css
129
js: /assets/lib/tocbot/tocbot.min.js

_data/origin/cors.yml

-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ webfonts: https://fonts.googleapis.com/css2?family=Lato:wght@300;400&family=Sour
1919

2020
# Libraries
2121

22-
bootstrap:
23-
css: https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css
24-
2522
toc:
2623
css: https://cdn.jsdelivr.net/npm/[email protected]/dist/tocbot.min.css
2724
js: https://cdn.jsdelivr.net/npm/[email protected]/dist/tocbot.min.js

_includes/head.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
{% endunless %}
7070

7171
<!-- Bootstrap -->
72-
<link rel="stylesheet" href="{{ site.data.origin[type].bootstrap.css | relative_url }}">
72+
{% unless jekyll.environment == 'production' %}
73+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
74+
{% endunless %}
7375

7476
<!-- Theme style -->
7577
<link rel="stylesheet" href="{{ '/assets/css/:THEME.css' | replace: ':THEME', site.theme | relative_url }}">

_posts/2019-08-09-getting-started.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Follow the instructions in the [Jekyll Docs](https://jekyllrb.com/docs/installat
2121

2222
There are two ways to create a new repository for this theme:
2323

24-
- [**Using the Chirpy Starter**](#option-1-using-the-chirpy-starter) - Easy to upgrade, isolates irrelevant project files so you can focus on writing.
25-
- [**GitHub Fork**](#option-2-github-fork) - Convenient for custom development, but difficult to upgrade. Unless you are familiar with Jekyll and are determined to tweak or contribute to this project, this approach is not recommended.
24+
- [**Using the Chirpy Starter**](#option-1-using-the-chirpy-starter) Easy to upgrade, isolates irrelevant project files so you can focus on writing.
25+
- [**GitHub Fork**](#option-2-github-fork) Convenient for custom development, but difficult to upgrade. Unless you are familiar with Jekyll and are determined to tweak or contribute to this project, this approach is not recommended.
2626

2727
#### Option 1. Using the Chirpy Starter
2828

@@ -32,7 +32,7 @@ Sign in to GitHub and browse to [**Chirpy Starter**][starter], click the button
3232

3333
Sign in to GitHub to [fork **Chirpy**](https://github.com/cotes2020/jekyll-theme-chirpy/fork), and then rename it to `USERNAME.github.io` (`USERNAME` means your username).
3434

35-
Next, clone your site to local machine. In order to build JavaScript files later, we need to install [Node.js][nodejs], and then run the tool:
35+
Next, clone the repository to your local machine, make sure it has [Node.js][nodejs] installed, then go to the root directory of the repo and run the following command:
3636

3737
```console
3838
$ bash tools/init
@@ -45,7 +45,7 @@ The above command will:
4545

4646
1. Check out the code to the [latest tag][latest-tag] (to ensure the stability of your site: as the code for the default branch is under development).
4747
2. Remove non-essential sample files and take care of GitHub-related files.
48-
3. Build JavaScript files and export to `assets/js/dist/`{: .filepath }, then make them tracked by Git.
48+
3. Build CSS/JS assets files and then make them tracked by Git.
4949
4. Automatically create a new commit to save the changes above.
5050

5151
### Installing Dependencies

_sass/main.bundle.scss

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import 'dist/bootstrap';
2+
@import 'main';

assets/css/jekyll-theme-chirpy.scss

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
---
22
---
33

4-
@import 'main';
4+
@import 'main
5+
{%- if jekyll.environment == 'production' -%}
6+
.bundle
7+
{%- endif -%}
8+
';
59

610
/* append your custom style below */

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
},
1414
"homepage": "https://github.com/cotes2020/jekyll-theme-chirpy/",
1515
"scripts": {
16-
"build": "npm run build:js",
16+
"build": "concurrently npm:build:*",
17+
"build:css": "purgecss -c purgecss.config.js",
1718
"build:js": "rollup -c --bundleConfigAsCjs --environment BUILD:production",
1819
"watch:js": "rollup -c --bundleConfigAsCjs -w",
19-
"lint:style": "stylelint _sass/**/*.scss",
20-
"lint:fix:style": "npm run lint:style -- --fix",
20+
"lint:scss": "stylelint _sass/**/*.scss",
21+
"lint:fix:scss": "npm run lint:style -- --fix",
2122
"test": "npm run lint:scss"
2223
},
2324
"dependencies": {
@@ -37,8 +38,10 @@
3738
"@semantic-release/changelog": "^6.0.3",
3839
"@semantic-release/exec": "^6.0.3",
3940
"@semantic-release/git": "^10.0.1",
41+
"concurrently": "^8.2.2",
4042
"conventional-changelog-conventionalcommits": "^7.0.2",
4143
"husky": "^9.0.11",
44+
"purgecss": "^6.0.0",
4245
"rollup": "^4.15.0",
4346
"rollup-plugin-license": "^3.3.1",
4447
"semantic-release": "^23.0.8",

purgecss.config.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const fs = require('fs');
2+
const DIST_PATH = '_sass/dist';
3+
4+
fs.rm(DIST_PATH, { recursive: true, force: true }, (err) => {
5+
if (err) {
6+
throw err;
7+
}
8+
9+
fs.mkdirSync(DIST_PATH);
10+
});
11+
12+
module.exports = {
13+
content: ['_includes/**/*.html', '_layouts/**/*.html', '_javascript/**/*.js'],
14+
css: ['node_modules/bootstrap/dist/css/bootstrap.min.css'],
15+
keyframes: true,
16+
variables: true,
17+
output: `${DIST_PATH}/bootstrap.css`,
18+
// The `safelist` should be changed appropriately for future development
19+
safelist: {
20+
standard: [/^collaps/, /^w-/, 'shadow', 'border', 'kbd'],
21+
greedy: [/^col-/, /tooltip/]
22+
}
23+
};

tools/init

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ init_files() {
8282
rm -rf "$temp"
8383

8484
## Cleanup image settings in site config
85-
_sedi "s/(^.*cdn:).*/\1/;s/(^avatar:).*/\1/" _config.yml
85+
_sedi "s/(^timezone:).*/\1/;s/(^.*cdn:).*/\1/;s/(^avatar:).*/\1/" _config.yml
8686
fi
8787

8888
# remove the other files
@@ -91,8 +91,8 @@ init_files() {
9191
# build assets
9292
npm i && npm run build
9393

94-
# track the js output
95-
_sedi "/^assets.*\/dist/d" .gitignore
94+
# track the CSS/JS output
95+
_sedi "/.*\/dist$/d" .gitignore
9696
}
9797

9898
commit() {

tools/release

+17-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ NODE_SPEC="package.json"
1515
CHANGELOG="docs/CHANGELOG.md"
1616
CONFIG="_config.yml"
1717

18+
CSS_DIST="_sass/dist"
1819
JS_DIST="assets/js/dist"
1920

2021
FILES=(
@@ -79,17 +80,12 @@ _check_src() {
7980
done
8081
}
8182

82-
_check_node_packages() {
83-
if [[ ! -d node_modules || "$(du node_modules | awk '{print $1}')" == "0" ]]; then
84-
npm i
85-
fi
86-
}
87-
88-
check() {
83+
init() {
8984
_check_cli
9085
_check_git
9186
_check_src
92-
_check_node_packages
87+
echo -e "> npm install\n"
88+
npm i
9389
}
9490

9591
## Bump new version to gem-spec file
@@ -115,24 +111,31 @@ prepare() {
115111
build_gem() {
116112
if $opt_pkg; then
117113
BACKUP_PATH="$(mktemp -d)"
118-
cp "$JS_DIST"/* "$BACKUP_PATH"
114+
mkdir -p "$BACKUP_PATH"/css "$BACKUP_PATH"/js
115+
[[ -d $CSS_DIST ]] && cp "$CSS_DIST"/* "$BACKUP_PATH"/css
116+
[[ -d $JS_DIST ]] && cp "$JS_DIST"/* "$BACKUP_PATH"/js
119117
fi
120118

121119
# Remove unnecessary theme settings
122-
sed -i "s/^cdn:.*/cdn:/;s/^avatar:.*/avatar:/" $CONFIG
120+
sed -i -E "s/(^timezone:).*/\1/;s/(^cdn:).*/\1/;s/(^avatar:).*/\1/" $CONFIG
123121
rm -f ./*.gem
124122

125123
npm run build
126-
git add "$JS_DIST" -f # add JS distribution files to gem
124+
# add CSS/JS distribution files to gem package
125+
git add "$CSS_DIST" "$JS_DIST" -f
126+
127+
echo -e "\n> gem build $GEM_SPEC\n"
127128
gem build "$GEM_SPEC"
128129

129-
# resume the settings
130+
echo -e "\n> Resume file changes ...\n"
130131
git reset
131132
git checkout .
132133

133134
if $opt_pkg; then
134135
# restore the dist files for future development
135-
mkdir -p "$JS_DIST" && cp "$BACKUP_PATH"/* "$JS_DIST"
136+
mkdir -p "$CSS_DIST" "$JS_DIST"
137+
cp "$BACKUP_PATH"/css/* "$CSS_DIST"
138+
cp "$BACKUP_PATH"/js/* "$JS_DIST"
136139
rm -rf "$BACKUP_PATH"
137140
fi
138141
}
@@ -157,7 +160,7 @@ merge() {
157160
}
158161

159162
main() {
160-
check
163+
init
161164

162165
if $opt_pre; then
163166
prepare

0 commit comments

Comments
 (0)