-
Notifications
You must be signed in to change notification settings - Fork 7
/
process-index.js
41 lines (35 loc) · 1.31 KB
/
process-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
const cheerio = require('cheerio')
const fs = require('fs');
const _ = require('lodash');
const indexFilePath = 'dist/browser/index.html';
// read our index file
fs.readFile(indexFilePath, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
// load html into cheerio so we can manipulate DOM
const $ = cheerio.load(data)
var baseHref = $('base').attr('href');
if (baseHref !== '/') {
const links = $('link');
links.each((index, _element) => {
const element = $(_element);
if (element.attr('href') && element.attr('href').match(/^\/assets\//) || element.attr('href').match(/^assets\//)) {
const currentHref = element.attr('href').replace(/^\//, '');
element.attr('href', baseHref + currentHref);
}
});
const metas = $('meta');
links.each((index, _element) => {
const element = $(_element);
if (element.attr('content') && element.attr('content').match(/^\/assets\//) || element.attr('href').match(/^assets\//)) {
const currentHref = element.attr('content').replace(/^\//, '');
element.attr('content', baseHref + currentHref);
}
});
}
// now write that file back
fs.writeFile(indexFilePath, $.html(), function (err) {
if (err) return console.log(err);
});
});