-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjson-loader.js
40 lines (32 loc) · 1.22 KB
/
json-loader.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
let fs = require('fs');
let path = require('path');
const jsonLoader = {
execute(baseDirectory, options, loader) {
let bundle = {};
files = fs.readdirSync(baseDirectory).filter((file) => {
return path.extname(file) === '.json';
});
files.forEach((file) => {
var lang = file.replace('.json', '');
var filePath = path.join(baseDirectory, file);
loader.addDependency(filePath);
var content = fs.readFileSync(filePath);
if (options && typeof options.namespace !== 'undefined') {
bundle[lang] = {};
bundle[lang][options.namespace] = JSON.parse(content);
} else {
bundle[lang] = JSON.parse(content);
}
if (options && typeof options.parameters !== "undefined") {
bundle[lang] = this.replaceParameter(bundle[lang], options.parameters);
}
});
return bundle;
},
replaceParameter(object, replacement) {
let objectAsString = JSON.stringify(object);
objectAsString = objectAsString.replace(/\:(\w+)/g, replacement);
return JSON.parse(objectAsString);
}
}
module.exports = jsonLoader