-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
122 lines (109 loc) · 3.81 KB
/
index.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
'use strict';
const minify = require('html-minifier').minify;
const sysPath = require('path');
const mkdirp = require('mkdirp');
const fs = require('fs');
const fm = require('front-matter');
const DEFAULT_PATTERN = /\.html$/;
const DEFAULT_HTMLMIN_OPTIONS = {
caseSensitive: false,
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: false,
collapseWhitespace: true,
conservativeCollapse: false,
html5: true,
includeAutoGeneratedTags: false,
keepClosingSlash: false,
minifyCSS: true,
minifyJS: true,
preserveLineBreaks: false,
preventAttributesEscaping: false,
processConditionalComments: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true
};
const DEFAULT_DESTINATION_FN = path => path.replace(/^app[\/\\]/, '');
const DEFAULT_FRONT_MATTER_SEPARATOR = '---';
const performMinify = (htmlPages, data) => {
if (htmlPages.disabled) {
if (htmlPages.forceRemoveFrontMatter) {
const frontmatter = fm(data);
return frontmatter.body;
} else {
return data;
}
} else {
if (!htmlPages.preserveFrontMatter && !htmlPages.removeFrontMatter) {
return minify(data, htmlPages.htmlMinOptions);
} else {
if (htmlPages.removeFrontMatter) {
// strip out front matter
const frontmatter = fm(data);
return minify(frontmatter.body, htmlPages.htmlMinOptions);
} else {
// minify and add back front matter
const frontmatter = fm(data);
return htmlPages.frontMatterSeparator + '\n' +
frontmatter.frontmatter + '\n' +
htmlPages.frontMatterSeparator + '\n' +
minify(frontmatter.body, htmlPages.htmlMinOptions);
}
}
}
};
class HtmlPages {
constructor(config) {
if (config === undefined) config = {};
if (config.plugins === undefined) config.plugins = {};
const pluginConfig = config.plugins.htmlPages || {};
this.publicPath = config.paths.public;
this.destinationFn = pluginConfig.destination || DEFAULT_DESTINATION_FN;
this.disabled = !config.optimize || pluginConfig.disabled;
this.pattern = pluginConfig.pattern || DEFAULT_PATTERN;
this.forceRemoveFrontMatter = !!pluginConfig.forceRemoveFrontMatter;
this.removeFrontMatter = this.forceRemoveFrontMatter || !!pluginConfig.removeFrontMatter;
this.preserveFrontMatter = !!pluginConfig.preserveFrontMatter;
this.frontMatterSeparator = pluginConfig.frontMatterSeparator || DEFAULT_FRONT_MATTER_SEPARATOR;
this.htmlMinOptions = pluginConfig.htmlMin ?
Object.assign({}, pluginConfig.htmlMin) :
DEFAULT_HTMLMIN_OPTIONS;
this.compileAssets = pluginConfig.compileAssets;
}
compile(file, path, callback) {
let err, error;
try {
const result = performMinify(this, file);
const destinationPath = sysPath.join(this.publicPath, this.destinationFn(path));
const destinationDir = sysPath.dirname(destinationPath);
mkdirp.sync(destinationDir);
return fs.writeFileSync(destinationPath, result);
} catch (_error) {
err = _error;
console.error('Error while processing \'${path}\': ${err.toString()}');
return error = err;
} finally {
return callback(error, '');
}
}
compileStatic(file) {
const data = file.data;
const path = file.path;
return new Promise(resolve => {
resolve({
data: this.compileAssets ? performMinify(this, data) : data,
path: sysPath.join(this.publicPath, this.destinationFn(path))
});
});
}
}
HtmlPages.prototype.brunchPlugin = true;
HtmlPages.prototype.type = 'template';
HtmlPages.prototype.extension = 'html';
HtmlPages.prototype.staticTargetExtension = 'html';
module.exports = HtmlPages;