mirror of
https://github.com/cotes2020/jekyll-theme-chirpy.git
synced 2025-12-24 00:23:10 +00:00
build: improve the JS build for PWA (#1923)
This commit is contained in:
@@ -6,35 +6,40 @@ import fs from 'fs';
|
||||
import pkg from './package.json';
|
||||
|
||||
const SRC_DEFAULT = '_javascript';
|
||||
const DIST_DEFAULT = 'assets/js/dist';
|
||||
|
||||
const SRC_PWA = `${SRC_DEFAULT}/pwa`;
|
||||
const DIST_PWA = '_app';
|
||||
const DIST = 'assets/js/dist';
|
||||
|
||||
const banner = `/*!
|
||||
* ${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';
|
||||
|
||||
function cleanup(...directories) {
|
||||
for (const dir of directories) {
|
||||
fs.rm(dir, { recursive: true, force: true }, (err) => {
|
||||
if (err) {
|
||||
console.error(`Failed to remove directory ${dir}: ${err}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
function cleanup() {
|
||||
fs.rmSync(DIST, { recursive: true, force: true });
|
||||
console.log(`> Directory "${DIST}" has been cleaned.`);
|
||||
}
|
||||
|
||||
function build(filename, opts = {}) {
|
||||
const src = opts.src || SRC_DEFAULT;
|
||||
const dist = opts.dist || DIST_DEFAULT;
|
||||
function insertFrontmatter() {
|
||||
return {
|
||||
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 {
|
||||
input: `${src}/${filename}.js`,
|
||||
output: {
|
||||
file: `${dist}/${filename}.min.js`,
|
||||
file: `${DIST}/${filename}.min.js`,
|
||||
format: 'iife',
|
||||
name: 'Chirpy',
|
||||
banner,
|
||||
@@ -51,12 +56,13 @@ function build(filename, opts = {}) {
|
||||
}),
|
||||
nodeResolve(),
|
||||
yaml(),
|
||||
isProd && terser()
|
||||
isProd && terser(),
|
||||
jekyll && insertFrontmatter()
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
cleanup(DIST_DEFAULT, DIST_PWA);
|
||||
cleanup();
|
||||
|
||||
export default [
|
||||
build('commons'),
|
||||
@@ -65,6 +71,6 @@ export default [
|
||||
build('page'),
|
||||
build('post'),
|
||||
build('misc'),
|
||||
build('app', { src: SRC_PWA, dist: DIST_PWA }),
|
||||
build('sw', { src: SRC_PWA, dist: DIST_PWA })
|
||||
build('app', { src: SRC_PWA, jekyll: true }),
|
||||
build('sw', { src: SRC_PWA, jekyll: true })
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user