-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.api.js
146 lines (141 loc) · 4.07 KB
/
node.api.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
import chalk from "chalk";
import fs from "fs-extra";
import nodePath from "path";
import config from "./src/template.config";
export default (options = {}) => ({
beforeDocumentToFile: (html, { meta }) => {
const divider = "div";
const divSplittage = html.split(divider);
if (divSplittage.length > 1) {
divSplittage[0] = divSplittage[0].replace(
new RegExp('<script charset="utf-8" ', "g"),
""
);
const lastIndex = divSplittage.length - 1;
divSplittage[lastIndex] = divSplittage[lastIndex].replace(
new RegExp(' type="text/javascript"', "g"),
""
);
return divSplittage.join(divider);
} else {
return html
.replace(new RegExp(' type="text/javascript"', "g"), "")
.replace(new RegExp('<script charset="utf-8" ', "g"), "");
}
},
afterExport: async state => {
const {
config: {
paths: { DIST }
},
staging
} = state;
fixMultilingualSitemap(DIST, staging);
applyManifestConfig(DIST);
applyBraveRewardsConfig(DIST);
applyRobotsConfig(DIST);
},
webpack: (currentWebpackConfig, state) => {
const { stage } = state
if (stage === 'dev') {
return currentWebpackConfig;
}
const newConfig = { ...currentWebpackConfig };
newConfig.optimization.splitChunks.cacheGroups.vendors = {
test: /[\\/]node_modules[\\/]/,
priority: -10,
chunks: "initial"
};
newConfig.optimization.splitChunks.cacheGroups.default = {
minChunks: 2,
priority: -20,
reuseExistingChunk: true
};
return newConfig;
}
});
function fixMultilingualSitemap(DIST, staging) {
const filename = staging ? "sitemap.staging.xml" : "sitemap.xml";
console.log(`Reading ${filename}...`);
const path = nodePath.join(DIST, filename);
const allLines = fs.readFileSync(path).toString();
fs.writeFileSync(
path,
allLines
.replace("<urlset", '<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml"')
.split("></xhtml:link")
.map(e =>
e.indexOf("<urlset") > 0
? e
: e
.split(">")
.slice(1)
.join(">")
)
.join("/>"),
() => {
console.log("Updating the file");
}
);
console.log(chalk.green(`[\u2713] ${filename} updated`));
}
function applyManifestConfig(DIST) {
const filename = "site.webmanifest";
console.log(`Reading ${filename}...`);
const path = nodePath.join(DIST, filename);
const allLines = fs.readFileSync(path).toString();
const splittage = allLines.split("$template.config.js.");
const output =
splittage[0] +
splittage
.slice(1)
.map(swap => {
const quoteSplittage = swap.split('"');
quoteSplittage[0] = config[quoteSplittage[0]];
return quoteSplittage.join('"');
})
.join("");
fs.writeFileSync(path, output, () => {
console.log("Updating the file");
});
console.log(chalk.green(`[\u2713] ${filename} updated`));
}
function applyBraveRewardsConfig(DIST) {
if (!config.optional.braveRewardsToken) {
return;
}
const filename = "./.well-known/brave-rewards-verification.txt";
console.log(`Reading ${filename}...`);
const path = nodePath.join(DIST, filename);
const allLines = fs.readFileSync(path).toString();
fs.writeFileSync(
path,
allLines
.replace("$template.config.js._siteRoot", config.siteRoot.split("//")[1])
.replace(
"$template.config.js.optional.braveRewardsToken",
config.optional.braveRewardsToken
),
() => {
console.log("Updating the file");
}
);
console.log(chalk.green(`[\u2713] ${filename} updated`));
}
function applyRobotsConfig(DIST) {
if (!config.optional.disallow) {
return;
}
const filename = "robots.txt";
console.log(`Reading ${filename}...`);
const path = nodePath.join(DIST, filename);
const allLines = fs.readFileSync(path).toString();
fs.writeFileSync(
path,
allLines + "\n" + config.optional.disallow,
() => {
console.log("Updating the file");
}
);
console.log(chalk.green(`[\u2713] ${filename} updated`));
}