This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·69 lines (54 loc) · 2.06 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
const path = require('path'),
loaderUtils = require('loader-utils'),
steed = require('steed'),
xmljs = require('xml-js');
function resolveImageSrc(loaderContext, tile, callback) {
var dirname = path.dirname(loaderContext.resourcePath),
src = tile.attributes.src;
// Resolve the image filename relative to the browserconfig file
loaderContext.resolve(dirname, src, function(err, filename) {
if (err) {
return callback(err);
}
// Ensure Webpack knows that the image is a dependency of the browserconfig
loaderContext.dependency && loaderContext.dependency(filename);
// Asynchronously pass the image through the loader pipeline
loaderContext.loadModule(filename, function(err, source, map, module) {
if (err) {
return callback(err);
}
// Update the image src property to match the generated filename
// Is it always the first key in the assets object?
tile.attributes.src = Object.keys(module.buildInfo.assets)[0]
callback(null);
});
});
}
function findElements(xmlElement, expectedName) {
return xmlElement.elements.filter(({ name }) => name === expectedName);
}
module.exports = function(source) {
const callback = this.async();
try {
var browserconfig = xmljs.xml2js(source);
var tiles = findElements(browserconfig, 'browserconfig')
.map((element) => findElements(element, 'msapplication'))
.reduce((acc, value) => [].concat(acc).concat(value), [])
.map((element) => findElements(element, 'tile'))
.reduce((acc, value) => [].concat(acc).concat(value), [])
.map((element) => element.elements)
.reduce((acc, value) => [].concat(acc).concat(value), [])
.filter((element) => element.attributes && element.attributes.src);
steed.map(tiles, resolveImageSrc.bind(null, this), (error) => {
if (error) {
return callback(error);
}
callback(null, xmljs.js2xml(browserconfig, {
spaces: 2,
indentAttributes: false,
}));
});
} catch (err) {
return callback(new Error('Invalid XML in Browserconfig'));
}
};