-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
189 lines (162 loc) · 4.18 KB
/
build.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict';
const path = require('path');
const autoprefixer = require('metalsmith-autoprefixer');
const babel = require('metalsmith-babel');
const browserifyAlt = require('metalsmith-browserify-alt');
const bourbon = require('bourbon');
const cleanCSS = require('metalsmith-clean-css');
const exec = require('child_process').exec;
const htmlMinifier = require('metalsmith-html-minifier');
const ignore = require('metalsmith-ignore');
const inlineSVG = require('metalsmith-inline-svg');
const inPlace = require('metalsmith-in-place');
const layouts = require('metalsmith-layouts');
const metalsmith = require('metalsmith');
const moveUp = require('metalsmith-move-up');
const rewrite = require('metalsmith-rewrite');
const robots = require('metalsmith-robots');
const sass = require('metalsmith-sass');
const sitemap = require('metalsmith-sitemap');
const timer = require('metalsmith-timer');
const uglify = require('metalsmith-uglify');
const devMode = process.env.NODE_ENV === 'development';
let protocol = 'https://';
let subdomain = 'www.';
let domain = '{DOMAIN_NAME}.com';
let port = '';
if (devMode) {
protocol = 'http://';
subdomain = '';
domain = 'localhost';
port = ':3000';
}
const host = protocol + subdomain + domain + port;
const metadata = {
devMode,
protocol,
subdomain,
domain,
port,
host,
};
const autoprefixerOptions = {
browsers: ['> 1%', 'last 2 versions', 'IE >= 9'],
};
const babelOptions = {
presets: ['es2015'],
};
const browserifyOptions = {
defaults: {
cache: {},
packageCache: {},
transform: [],
plugin: [],
debug: devMode,
},
};
const cleanCSSOptions = {
cleanCSS: {
roundingPrecision: 4,
keepSpecialComments: 0,
},
};
const folderPerPageOptions = {
pattern: ['*.html', '!index.html', '!google*.html'],
filename: '{path.dir}/{path.name}/index.html',
};
const layoutOptions = {
engine: 'handlebars',
pattern: '*.html',
directory: 'src_layouts',
partials: 'src_partials',
cache: false,
};
const pagesFolderOptions = {
pattern: 'pages/**/*.html',
};
const publicFolderOptions = {
pattern: 'public/**',
};
const robotsOptions = {
disallow: ['/css', '/fonts', '/js'],
sitemap: host + '/sitemap.xml',
};
let sassIncludes = [];
sassIncludes = sassIncludes.concat(bourbon.includePaths);
const sassOptions = {
outputDir: 'css/',
sourceMap: devMode,
sourceMapContents: devMode,
outputStyle: devMode ? 'expanded' : 'compressed',
includePaths: sassIncludes,
};
const sitemapOptions = {
hostname: host,
omitIndex: true,
};
const uglifyOptions = {
filter: 'js/*.js',
removeOriginal: !devMode,
sourceMap: devMode,
};
notification('{SITE_NAME}', 'Build started');
const ms = metalsmith(__dirname);
// Base settings.
ms.use(timer('init'));
ms.metadata(metadata);
ms.source('src/');
ms.destination('build/');
ms.clean(true);
// Ignore system files.
ms.use(ignore(['**/.DS_Store', '**/.keep']));
// Move files up from their organized location.
ms.use(moveUp(pagesFolderOptions));
ms.use(moveUp(publicFolderOptions));
// CSS files.
ms.use(sass(sassOptions));
ms.use(ignore(['css/*', '!css/index.css', '!css/index.css.map']));
ms.use(autoprefixer(autoprefixerOptions));
if (!devMode) {
ms.use(cleanCSS(cleanCSSOptions));
}
// JS Files.
ms.use(browserifyAlt(browserifyOptions));
ms.use(ignore(['js/**', '!js/*.js', '!js/vendor/**/*.js']));
if (!devMode) {
ms.use(babel(babelOptions));
ms.use(uglify(uglifyOptions));
}
// HTML Files.
ms.use(inPlace(layoutOptions));
ms.use(layouts(layoutOptions));
// Inline the svg files and then ignore them.
ms.use(inlineSVG());
ms.use(ignore(['svg/**']));
// Minify html in production.
if (!devMode) {
ms.use(htmlMinifier());
}
ms.use(rewrite(folderPerPageOptions));
// SEO stuff.
ms.use(robots(robotsOptions));
ms.use(sitemap(sitemapOptions));
// DO THE THING!
ms.use(timer('build complete'));
ms.build(function(err) {
if (err) {
notification('{SITE_NAME}', 'Build failed');
throw err;
} else {
notification('{SITE_NAME}', 'Build completed');
}
});
function notification(title, message) {
if (process.platform === 'darwin') {
const cmd = 'osascript -e "display notification \\"' +
message +
'\\" with title \\"' +
title +
'\\""';
exec(cmd);
}
}