-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmetalsmith.js
113 lines (103 loc) · 3.24 KB
/
metalsmith.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* eslint-disable import/no-extraneous-dependencies */
import { performance } from 'perf_hooks';
import browserSync from 'browser-sync';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import Metalsmith from 'metalsmith';
import markdown from '@metalsmith/markdown';
import layouts from '@metalsmith/layouts';
import drafts from '@metalsmith/drafts';
import permalinks from '@metalsmith/permalinks';
import metadata from '@metalsmith/metadata';
import htmlMinifier from 'metalsmith-html-minifier';
import assets from 'metalsmith-static-files';
// ESM does not currently import JSON modules by default.
// Ergo we'll JSON.parse the file manually
import * as fs from 'fs';
const { dependencies } = JSON.parse( fs.readFileSync( './package.json' ) );
/* eslint-disable no-underscore-dangle */
const __dirname = dirname( fileURLToPath( import.meta.url ) );
const isProduction = process.env.NODE_ENV === 'production';
// functions to extend Nunjucks environment
const spaceToDash = ( string ) => string.replace( /\s+/g, '-' );
const condenseTitle = ( string ) => string.toLowerCase().replace( /\s+/g, '' );
const UTCdate = ( date ) => date.toUTCString( 'M d, yyyy' );
const blogDate = ( date ) => date.toLocaleString( 'en-US', { year: 'numeric', month: 'long', day: 'numeric' } );
const trimSlashes = ( string ) => string.replace( /(^\/)|(\/$)/g, '' );
const thisYear = () => new Date().getFullYear();
// Define engine options for the inplace and layouts plugins
const templateConfig = {
engineOptions: {
smartypants: true,
smartLists: true,
filters: {
spaceToDash,
condenseTitle,
UTCdate,
blogDate,
trimSlashes,
thisYear
}
}
};
function noop() { };
// to use a plugin conditionally, use this pattern:
// .use( isProduction ? htmlMinifier() : noop ) )
let devServer = null;
let t1 = performance.now();
function msBuild() {
return (
Metalsmith( __dirname )
.clean( true )
.watch( isProduction ? false : [ 'src', 'layouts' ] )
.source( './src/content' )
.destination( './build' )
.clean( true )
.env( 'NODE_ENV', process.env.NODE_ENV )
// .env( 'DEBUG', process.env.DEBUG )
.metadata( {
msVersion: dependencies.metalsmith,
nodeVersion: process.version
} )
.use( isProduction ? noop : drafts() )
.use(
metadata( {
site: 'src/content/data/site.json',
nav: 'src/content/data/navigation.json'
} )
)
.use( markdown() )
.use( permalinks() )
.use( layouts( templateConfig ) )
.use(
assets( {
source: 'src/assets/',
destination: 'assets/'
} )
)
.use( isProduction ? htmlMinifier() : noop )
);
}
const ms = msBuild();
ms.build( ( err ) => {
if ( err ) {
throw err;
}
/* eslint-disable no-console */
console.log( `Build success in ${ ( ( performance.now() - t1 ) / 1000 ).toFixed( 1 ) }s` );
if ( ms.watch() ) {
if ( devServer ) {
t1 = performance.now();
devServer.reload();
} else {
devServer = browserSync.create();
devServer.init( {
host: 'localhost',
server: './build',
port: 3000,
injectChanges: false,
reloadThrottle: 0
} );
}
}
} );