@@ -6,35 +6,40 @@ import fs from 'fs';
66import pkg from './package.json' ;
77
88const SRC_DEFAULT = '_javascript' ;
9- const DIST_DEFAULT = 'assets/js/dist' ;
10-
119const SRC_PWA = `${ SRC_DEFAULT } /pwa` ;
12- const DIST_PWA = '_app ' ;
10+ const DIST = 'assets/js/dist ' ;
1311
1412const 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+
1818const 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
6167export 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] ;
0 commit comments