forked from alienfast/i18next-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (80 loc) · 3.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const path = require('path')
const fs = require('fs')
const merge = require('lodash/merge')
const globAll = require('glob-all')
const loaderUtils = require('loader-utils')
const yaml = require('js-yaml')
function enumerateLangs (dir) {
return fs.readdirSync(dir).filter(function (file) {
return fs.statSync(path.join(dir, file)).isDirectory()
})
}
//https://github.com/jpillora/node-glob-all#usage
function findAll (globs, cwd) {
const globArray = (Array.isArray(globs) ? globs : [ globs ])
return globAll.sync(globArray, { cwd, realpath: true })
}
module.exports = function () {
this.cacheable && this.cacheable()
const options = loaderUtils.getOptions(this) || {}
if (!options.include) {
options.include = [ '**/*.json', '**/*.yml', '**/*.yaml' ]
}
if(!options.overrides) options.overrides = []
const appLocalesDir = path.dirname(this.resource) // this is the absolute path to the index.js in the top level locales dir
if (!fs.existsSync(appLocalesDir)) {
throw new Error('Directory does not exist: ' + appLocalesDir + ' for resource: ' + this.resource)
}
let appResBundle = {}
if (options.debug) {
console.info("Bundling locales from " + appLocalesDir + ' (ordered least specific to most):')
}
// needs to be ordered in least specialized to most e.g. lib locale -> app locale
if(!options.overridePathMethod) options.overridePathMethod = 'join'
const moduleLocalesDirs = options.overridePathMethod === 'replace'
? options.overrides
: options.overrides.map(override =>
path[options.overridePathMethod](appLocalesDir, override))
moduleLocalesDirs.push(appLocalesDir)
moduleLocalesDirs.forEach((localesDir) => {
// all subdirectories match language codes
const langs = enumerateLangs(localesDir)
for (let i = 0; i < langs.length; i++) {
const lang = langs[ i ]
const resBundle = {}
resBundle[ lang ] = {}
const fullLangPath = path.join(localesDir, lang)
this.addContextDependency(fullLangPath)
const langFiles = findAll(options.include, fullLangPath)
for (let j = 0; j < langFiles.length; j++) {
const fullPath = langFiles[ j ]
this.addDependency(fullPath)
if (options.debug) {
console.info("\t" + fullPath)
}
const fileContent = fs.readFileSync(fullPath)
const extname = path.extname(fullPath)
let nameSpaceName = path.basename(fullPath, extname);
let parsedContent;
if (extname === ".yaml" || extname === ".yml") {
parsedContent = yaml.safeLoad(fileContent);
} else {
parsedContent = JSON.parse(fileContent);
}
if (options.basenameAsNamespace) {
const ns = resBundle[lang] || {};
ns[nameSpaceName] = parsedContent;
resBundle[lang] = ns;
} else {
resBundle[lang] = parsedContent;
}
appResBundle = merge(appResBundle, resBundle)
}
}
})
const bundle = JSON.stringify(appResBundle)
if (options.debug) {
console.info("Final locales bundle: \n" + bundle)
}
return 'module.exports = ' + bundle
}