Skip to content

Commit fc3d101

Browse files
authored
build: improve the JS build for PWA (#1923)
1 parent 604e01e commit fc3d101

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ package-lock.json
2525
# Misc
2626
_sass/dist
2727
assets/js/dist
28-
_app

_config.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,6 @@ collections:
170170
tabs:
171171
output: true
172172
sort_by: order
173-
app:
174-
output: true
175-
permalink: /:name
176173

177174
defaults:
178175
- scope:

jekyll-theme-chirpy.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
1111
spec.license = "MIT"
1212

1313
spec.files = `git ls-files -z`.split("\x0").select { |f|
14-
f.match(%r!^((_(includes|layouts|sass|app|(data\/(locales|origin)))|assets)\/|README|LICENSE)!i)
14+
f.match(%r!^((_(includes|layouts|sass|(data\/(locales|origin)))|assets)\/|README|LICENSE)!i)
1515
}
1616

1717
spec.metadata = {

rollup.config.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,40 @@ import fs from 'fs';
66
import pkg from './package.json';
77

88
const SRC_DEFAULT = '_javascript';
9-
const DIST_DEFAULT = 'assets/js/dist';
10-
119
const SRC_PWA = `${SRC_DEFAULT}/pwa`;
12-
const DIST_PWA = '_app';
10+
const DIST = 'assets/js/dist';
1311

1412
const banner = `/*!
1513
* ${pkg.name} v${pkg.version} | © ${pkg.since} ${pkg.author} | ${pkg.license} Licensed | ${pkg.homepage}
1614
*/`;
1715

16+
const frontmatter = `---\npermalink: /:basename\n---\n`;
17+
1818
const isProd = process.env.BUILD === 'production';
1919

20-
function cleanup(...directories) {
21-
for (const dir of directories) {
22-
fs.rm(dir, { recursive: true, force: true }, (err) => {
23-
if (err) {
24-
console.error(`Failed to remove directory ${dir}: ${err}`);
25-
}
26-
});
27-
}
20+
function cleanup() {
21+
fs.rmSync(DIST, { recursive: true, force: true });
22+
console.log(`> Directory "${DIST}" has been cleaned.`);
2823
}
2924

30-
function build(filename, opts = {}) {
31-
const src = opts.src || SRC_DEFAULT;
32-
const dist = opts.dist || DIST_DEFAULT;
25+
function insertFrontmatter() {
26+
return {
27+
name: 'insert-frontmatter',
28+
generateBundle(_, bundle) {
29+
for (const chunkOrAsset of Object.values(bundle)) {
30+
if (chunkOrAsset.type === 'chunk') {
31+
chunkOrAsset.code = frontmatter + chunkOrAsset.code;
32+
}
33+
}
34+
}
35+
};
36+
}
3337

38+
function build(filename, { src = SRC_DEFAULT, jekyll = false } = {}) {
3439
return {
3540
input: `${src}/${filename}.js`,
3641
output: {
37-
file: `${dist}/${filename}.min.js`,
42+
file: `${DIST}/${filename}.min.js`,
3843
format: 'iife',
3944
name: 'Chirpy',
4045
banner,
@@ -51,12 +56,13 @@ function build(filename, opts = {}) {
5156
}),
5257
nodeResolve(),
5358
yaml(),
54-
isProd && terser()
59+
isProd && terser(),
60+
jekyll && insertFrontmatter()
5561
]
5662
};
5763
}
5864

59-
cleanup(DIST_DEFAULT, DIST_PWA);
65+
cleanup();
6066

6167
export default [
6268
build('commons'),
@@ -65,6 +71,6 @@ export default [
6571
build('page'),
6672
build('post'),
6773
build('misc'),
68-
build('app', { src: SRC_PWA, dist: DIST_PWA }),
69-
build('sw', { src: SRC_PWA, dist: DIST_PWA })
74+
build('app', { src: SRC_PWA, jekyll: true }),
75+
build('sw', { src: SRC_PWA, jekyll: true })
7076
];

tools/init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ init_files() {
9292
npm i && npm run build
9393

9494
# track the CSS/JS output
95-
_sedi "/.*\/dist$/d;/^_app$/d" .gitignore
95+
_sedi "/.*\/dist$/d" .gitignore
9696
}
9797

9898
commit() {

tools/release.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ CONFIG="_config.yml"
1717

1818
CSS_DIST="_sass/dist"
1919
JS_DIST="assets/js/dist"
20-
PWA_DIST="_app"
2120

2221
FILES=(
2322
"$GEM_SPEC"
@@ -118,7 +117,7 @@ build_gem() {
118117

119118
npm run build
120119
# add CSS/JS distribution files to gem package
121-
git add "$CSS_DIST" "$JS_DIST" "$PWA_DIST" -f
120+
git add "$CSS_DIST" "$JS_DIST" -f
122121

123122
echo -e "\n> gem build $GEM_SPEC\n"
124123
gem build "$GEM_SPEC"

0 commit comments

Comments
 (0)