11"use strict" ;
2+ var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
3+ return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
4+ } ;
25Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
36var querystring_1 = require ( "querystring" ) ;
7+ var json5_1 = __importDefault ( require ( "json5" ) ) ;
8+ var js_yaml_1 = __importDefault ( require ( "js-yaml" ) ) ;
49var loader = function ( source , sourceMap ) {
510 if ( this . version && Number ( this . version ) >= 2 ) {
611 try {
@@ -20,23 +25,30 @@ var loader = function (source, sourceMap) {
2025} ;
2126function generateCode ( source , query ) {
2227 var _a ;
23- var code = '' ;
24- var value = typeof source === 'string'
25- ? JSON . parse ( source )
26- : Buffer . isBuffer ( source )
27- ? JSON . parse ( source . toString ( ) )
28- : null ;
29- if ( value === null ) {
30- throw new Error ( 'invalid source!' ) ;
31- }
28+ var data = convert ( source , query . lang ) ;
29+ var value = JSON . parse ( data ) ;
3230 if ( query . locale && typeof query . locale === 'string' ) {
3331 value = Object . assign ( { } , ( _a = { } , _a [ query . locale ] = value , _a ) ) ;
3432 }
3533 value = JSON . stringify ( value )
3634 . replace ( / \u2028 / g, '\\u2028' )
3735 . replace ( / \u2029 / g, '\\u2029' )
3836 . replace ( / \\ / g, '\\\\' ) ;
37+ var code = '' ;
3938 code += "function (Component) {\n Component.options.__i18n = Component.options.__i18n || []\n Component.options.__i18n.push('" + value . replace ( / \u0027 / g, '\\u0027' ) + "')\n delete Component.options._Ctor\n}\n" ;
4039 return code ;
4140}
41+ function convert ( source , lang ) {
42+ var value = Buffer . isBuffer ( source ) ? source . toString ( ) : source ;
43+ switch ( lang ) {
44+ case 'yaml' :
45+ case 'yml' :
46+ var data = js_yaml_1 . default . safeLoad ( value ) ;
47+ return JSON . stringify ( data , undefined , '\t' ) ;
48+ case 'json5' :
49+ return JSON . stringify ( json5_1 . default . parse ( value ) ) ;
50+ default :
51+ return value ;
52+ }
53+ }
4254exports . default = loader ;
0 commit comments