forked from atroo/i18next-resource-store-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (49 loc) · 1.92 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Robert Krüger
*/
var path = require("path");
var fs = require("fs");
var loaderUtils = require('loader-utils');
module.exports = function (indexContent) {
this.cacheable && this.cacheable();
var options = loaderUtils.parseQuery(this.query);
var include;
if (options.include) {
include = new RegExp(options.include);
}
var exclude;
if (options.exclude) {
exclude = new RegExp(options.exclude);
}
var baseDirectory = path.dirname(this.resource);
var subdirs = fs.readdirSync(baseDirectory).filter(function (file) {
return fs.statSync(path.join(baseDirectory, file)).isDirectory();
});
//alls sub directories match to languages
var resBundle = {},
dirname, files;
for (var i = 0, len = subdirs.length; i < len; i++) {
//all files within the sub directory map to namespaces
dirname = subdirs[i];
resBundle[dirname] = {};
//get sub files
files = fs.readdirSync(path.join(baseDirectory, dirname)).filter(function (file) {
return fs.statSync(path.join(baseDirectory, dirname, file)).isFile()
&& (!include || include.test(file))
&& (!(exclude && exclude.test(file)));
});
var filename, extname, basename, content, pathstring;
for (var j = 0, len2 = files.length; j < len2; j++) {
filename = files[j];
pathstring = path.join(baseDirectory, dirname, filename);
extname = path.extname(pathstring);
basename = path.basename(pathstring, extname);
content = fs.readFileSync(pathstring);
resBundle[dirname][basename] = JSON.parse(content);
this.addDependency(pathstring);
}
this.addContextDependency(path.join(baseDirectory, dirname));
}
return "module.exports = " + JSON.stringify(resBundle);
}