build: improve the JS build for PWA (#1923)

This commit is contained in:
Cotes Chung 2024-08-26 16:49:48 +08:00 committed by GitHub
parent 604e01eb36
commit fc3d101258
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 28 additions and 27 deletions

1
.gitignore vendored
View file

@ -25,4 +25,3 @@ package-lock.json
# Misc # Misc
_sass/dist _sass/dist
assets/js/dist assets/js/dist
_app

View file

@ -170,9 +170,6 @@ collections:
tabs: tabs:
output: true output: true
sort_by: order sort_by: order
app:
output: true
permalink: /:name
defaults: defaults:
- scope: - scope:

View file

@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
spec.license = "MIT" spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0").select { |f| spec.files = `git ls-files -z`.split("\x0").select { |f|
f.match(%r!^((_(includes|layouts|sass|app|(data\/(locales|origin)))|assets)\/|README|LICENSE)!i) f.match(%r!^((_(includes|layouts|sass|(data\/(locales|origin)))|assets)\/|README|LICENSE)!i)
} }
spec.metadata = { spec.metadata = {

View file

@ -6,35 +6,40 @@ import fs from 'fs';
import pkg from './package.json'; import pkg from './package.json';
const SRC_DEFAULT = '_javascript'; const SRC_DEFAULT = '_javascript';
const DIST_DEFAULT = 'assets/js/dist';
const SRC_PWA = `${SRC_DEFAULT}/pwa`; const SRC_PWA = `${SRC_DEFAULT}/pwa`;
const DIST_PWA = '_app'; const DIST = 'assets/js/dist';
const banner = `/*! const banner = `/*!
* ${pkg.name} v${pkg.version} | © ${pkg.since} ${pkg.author} | ${pkg.license} Licensed | ${pkg.homepage} * ${pkg.name} v${pkg.version} | © ${pkg.since} ${pkg.author} | ${pkg.license} Licensed | ${pkg.homepage}
*/`; */`;
const frontmatter = `---\npermalink: /:basename\n---\n`;
const isProd = process.env.BUILD === 'production'; const isProd = process.env.BUILD === 'production';
function cleanup(...directories) { function cleanup() {
for (const dir of directories) { fs.rmSync(DIST, { recursive: true, force: true });
fs.rm(dir, { recursive: true, force: true }, (err) => { console.log(`> Directory "${DIST}" has been cleaned.`);
if (err) {
console.error(`Failed to remove directory ${dir}: ${err}`);
}
});
}
} }
function build(filename, opts = {}) { function insertFrontmatter() {
const src = opts.src || SRC_DEFAULT; return {
const dist = opts.dist || DIST_DEFAULT; name: 'insert-frontmatter',
generateBundle(_, bundle) {
for (const chunkOrAsset of Object.values(bundle)) {
if (chunkOrAsset.type === 'chunk') {
chunkOrAsset.code = frontmatter + chunkOrAsset.code;
}
}
}
};
}
function build(filename, { src = SRC_DEFAULT, jekyll = false } = {}) {
return { return {
input: `${src}/${filename}.js`, input: `${src}/${filename}.js`,
output: { output: {
file: `${dist}/${filename}.min.js`, file: `${DIST}/${filename}.min.js`,
format: 'iife', format: 'iife',
name: 'Chirpy', name: 'Chirpy',
banner, banner,
@ -51,12 +56,13 @@ function build(filename, opts = {}) {
}), }),
nodeResolve(), nodeResolve(),
yaml(), yaml(),
isProd && terser() isProd && terser(),
jekyll && insertFrontmatter()
] ]
}; };
} }
cleanup(DIST_DEFAULT, DIST_PWA); cleanup();
export default [ export default [
build('commons'), build('commons'),
@ -65,6 +71,6 @@ export default [
build('page'), build('page'),
build('post'), build('post'),
build('misc'), build('misc'),
build('app', { src: SRC_PWA, dist: DIST_PWA }), build('app', { src: SRC_PWA, jekyll: true }),
build('sw', { src: SRC_PWA, dist: DIST_PWA }) build('sw', { src: SRC_PWA, jekyll: true })
]; ];

View file

@ -92,7 +92,7 @@ init_files() {
npm i && npm run build npm i && npm run build
# track the CSS/JS output # track the CSS/JS output
_sedi "/.*\/dist$/d;/^_app$/d" .gitignore _sedi "/.*\/dist$/d" .gitignore
} }
commit() { commit() {

View file

@ -17,7 +17,6 @@ CONFIG="_config.yml"
CSS_DIST="_sass/dist" CSS_DIST="_sass/dist"
JS_DIST="assets/js/dist" JS_DIST="assets/js/dist"
PWA_DIST="_app"
FILES=( FILES=(
"$GEM_SPEC" "$GEM_SPEC"
@ -118,7 +117,7 @@ build_gem() {
npm run build npm run build
# add CSS/JS distribution files to gem package # add CSS/JS distribution files to gem package
git add "$CSS_DIST" "$JS_DIST" "$PWA_DIST" -f git add "$CSS_DIST" "$JS_DIST" -f
echo -e "\n> gem build $GEM_SPEC\n" echo -e "\n> gem build $GEM_SPEC\n"
gem build "$GEM_SPEC" gem build "$GEM_SPEC"