@@ -19,57 +19,46 @@ import type { CodeGenerator, CodeGenFunction, CodeGenOptions, CodeGenResult } fr
19
19
20
20
export class DynamicResourceError extends Error { }
21
21
22
+ /**
23
+ * @internal
24
+ */
25
+ export const DEFAULT_OPTIONS : CodeGenOptions = {
26
+ type : 'plain' ,
27
+ filename : 'vue-i18n-loader.js' ,
28
+ inSourceMap : undefined ,
29
+ locale : '' ,
30
+ isGlobal : false ,
31
+ sourceMap : false ,
32
+ env : 'development' ,
33
+ forceStringify : false ,
34
+ onError : undefined ,
35
+ onWarn : undefined ,
36
+ strictMessage : true ,
37
+ escapeHtml : false ,
38
+ allowDynamic : false ,
39
+ jit : false
40
+ }
41
+
22
42
/**
23
43
* @internal
24
44
*/
25
45
export function generate (
26
46
targetSource : string | Buffer ,
27
- {
28
- type = 'plain' ,
29
- filename = 'vue-i18n-loader.js' ,
30
- inSourceMap = undefined ,
31
- locale = '' ,
32
- isGlobal = false ,
33
- sourceMap = false ,
34
- env = 'development' ,
35
- forceStringify = false ,
36
- onError = undefined ,
37
- onWarn = undefined ,
38
- strictMessage = true ,
39
- escapeHtml = false ,
40
- allowDynamic = false ,
41
- jit = false
42
- } : CodeGenOptions
47
+ options : CodeGenOptions
43
48
) : CodeGenResult < Node > {
44
49
const value = Buffer . isBuffer ( targetSource ) ? targetSource . toString ( ) : targetSource
45
50
46
- const options = {
47
- type,
48
- source : value ,
49
- sourceMap,
50
- locale,
51
- isGlobal,
52
- inSourceMap,
53
- env,
54
- filename,
55
- forceStringify,
56
- onError,
57
- onWarn,
58
- strictMessage,
59
- escapeHtml,
60
- jit
61
- } as CodeGenOptions
62
- const generator = createCodeGenerator ( options )
63
-
51
+ const _options = Object . assign ( { } , DEFAULT_OPTIONS , options , { source : value } )
52
+ const generator = createCodeGenerator ( _options )
64
53
const ast = parseJavaScript ( value , {
65
54
ecmaVersion : 'latest' ,
66
55
sourceType : 'module' ,
67
- sourceFile : filename ,
56
+ sourceFile : _options . filename ,
68
57
allowImportExportEverywhere : true
69
58
} ) as Node
70
59
71
60
const exportResult = scanAst ( ast )
72
- if ( ! allowDynamic ) {
61
+ if ( ! _options . allowDynamic ) {
73
62
if ( ! exportResult ) {
74
63
throw new Error ( `You need to define an object as the locale message with 'export default'.` )
75
64
}
@@ -95,17 +84,17 @@ export function generate(
95
84
return {
96
85
ast,
97
86
code : value ,
98
- map : inSourceMap
87
+ map : _options . inSourceMap
99
88
}
100
89
}
101
90
}
102
91
103
- const codeMaps = _generate ( generator , ast , options )
92
+ const codeMaps = _generate ( generator , ast , _options )
104
93
105
94
const { code, map } = generator . context ( )
106
95
// prettier-ignore
107
96
const newMap = map
108
- ? mapLinesColumns ( ( map as any ) . toJSON ( ) , codeMaps , inSourceMap ) || null
97
+ ? mapLinesColumns ( ( map as any ) . toJSON ( ) , codeMaps , _options . inSourceMap ) || null
109
98
: null
110
99
return {
111
100
ast,
@@ -162,7 +151,7 @@ function _generate(
162
151
const componentNamespace = '_Component'
163
152
const variableDeclarations : string [ ] = [ ]
164
153
165
- // slice and reseuse imports and top-level variable declarations as-is
154
+ // slice and reuse imports and top-level variable declarations as-is
166
155
// NOTE: this prevents optimization/compilation of top-level variables, we may be able to add support for this
167
156
walk ( node , {
168
157
/**
0 commit comments