Skip to content

Commit 3fedbe0

Browse files
authored
refactor: extract default options to constant object (#493)
* refactor: extract default options to constant object * chore: spelling
1 parent 263a7cd commit 3fedbe0

File tree

2 files changed

+35
-78
lines changed

2 files changed

+35
-78
lines changed

packages/bundle-utils/src/js.ts

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,57 +19,46 @@ import type { CodeGenerator, CodeGenFunction, CodeGenOptions, CodeGenResult } fr
1919

2020
export class DynamicResourceError extends Error {}
2121

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+
2242
/**
2343
* @internal
2444
*/
2545
export function generate(
2646
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
4348
): CodeGenResult<Node> {
4449
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource
4550

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)
6453
const ast = parseJavaScript(value, {
6554
ecmaVersion: 'latest',
6655
sourceType: 'module',
67-
sourceFile: filename,
56+
sourceFile: _options.filename,
6857
allowImportExportEverywhere: true
6958
}) as Node
7059

7160
const exportResult = scanAst(ast)
72-
if (!allowDynamic) {
61+
if (!_options.allowDynamic) {
7362
if (!exportResult) {
7463
throw new Error(`You need to define an object as the locale message with 'export default'.`)
7564
}
@@ -95,17 +84,17 @@ export function generate(
9584
return {
9685
ast,
9786
code: value,
98-
map: inSourceMap
87+
map: _options.inSourceMap
9988
}
10089
}
10190
}
10291

103-
const codeMaps = _generate(generator, ast, options)
92+
const codeMaps = _generate(generator, ast, _options)
10493

10594
const { code, map } = generator.context()
10695
// prettier-ignore
10796
const newMap = map
108-
? mapLinesColumns((map as any).toJSON(), codeMaps, inSourceMap) || null
97+
? mapLinesColumns((map as any).toJSON(), codeMaps, _options.inSourceMap) || null
10998
: null
11099
return {
111100
ast,
@@ -162,7 +151,7 @@ function _generate(
162151
const componentNamespace = '_Component'
163152
const variableDeclarations: string[] = []
164153

165-
// slice and reseuse imports and top-level variable declarations as-is
154+
// slice and reuse imports and top-level variable declarations as-is
166155
// NOTE: this prevents optimization/compilation of top-level variables, we may be able to add support for this
167156
walk(node, {
168157
/**

packages/bundle-utils/src/ts.ts

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,26 @@
77
import { transform } from 'esbuild'
88
import { Node } from 'estree'
99
import { CodeGenOptions, CodeGenResult } from './codegen'
10-
import { generate as generateJavaScript } from './js'
10+
import { DEFAULT_OPTIONS, generate as generateJavaScript } from './js'
1111

1212
/**
1313
* @internal
1414
*/
1515
export async function generate(
1616
targetSource: string | Buffer,
17-
{
18-
type = 'plain',
19-
filename = 'vue-i18n-loader.js',
20-
inSourceMap = undefined,
21-
locale = '',
22-
isGlobal = false,
23-
sourceMap = false,
24-
env = 'development',
25-
forceStringify = false,
26-
onError = undefined,
27-
onWarn = undefined,
28-
strictMessage = true,
29-
escapeHtml = false,
30-
allowDynamic = false,
31-
jit = false
32-
}: CodeGenOptions
17+
options: CodeGenOptions
3318
): Promise<CodeGenResult<Node>> {
3419
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource
3520

36-
const options = {
37-
type,
38-
source: value,
39-
sourceMap,
40-
locale,
41-
isGlobal,
42-
inSourceMap,
43-
env,
44-
filename,
45-
forceStringify,
46-
onError,
47-
onWarn,
48-
strictMessage,
49-
escapeHtml,
50-
allowDynamic,
51-
jit
52-
} as CodeGenOptions
53-
54-
if (options.filename && /.[c|m]?ts$/.test(options.filename)) {
21+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value })
22+
if (_options.filename && /.[c|m]?ts$/.test(_options.filename)) {
5523
const transformed = await transform(value, { loader: 'ts' })
5624

5725
if (transformed && transformed.code) {
5826
value = transformed.code
59-
options.source = transformed.code
27+
_options.source = transformed.code
6028
}
6129
}
6230

63-
return generateJavaScript(value, options)
31+
return generateJavaScript(value, _options)
6432
}

0 commit comments

Comments
 (0)