-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (38 loc) · 1.56 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
const loaderUtils = require('loader-utils');
const get = require('lodash.get');
const omit = require('object.omit');
const flatten = require('./lib/flatten');
const createFiltersQueue = require('./lib/createFiltersQueue');
const getParamsFromFile = require('./lib/getParamsFromFile');
const validate = require('./lib/validator/validate');
const applyFilters = require('./lib/applyFilters');
module.exports = function( source ) {
if ( this.cacheable ) {
this.cacheable();
}
const options = loaderUtils.getOptions( this );
const jsonContent = JSON.parse( source.toString() );
const compileOpts = get( jsonContent, 'compile_options', {} );
const getQueue = createFiltersQueue( compileOpts.defaults );
const content = omit( jsonContent, 'compile_options' );
validate( options );
options.fsParams = getParamsFromFile( this.resourcePath, options.babelfish );
options.compileOptions = compileOpts;
const dict = flatten( content, options.fsParams.namespace );
const result = {
fallback : options.babelfish.fallback,
locales : {
[ options.fsParams.locale ] : {},
},
};
for ( let key in dict ) {
if ( !dict.hasOwnProperty( key ) ) {
continue;
}
const filtersQueue = getQueue( key );
const phrase = key.replace( /-(?:bem|tg|md)/g, '' );
result.locales[ options.fsParams.locale ][ phrase ] = applyFilters( dict[ key ], filtersQueue, options );
}
return `module.exports = ${JSON.stringify( result )}`;
};
module.exports.raw = true;