Skip to content

Commit d1f0e16

Browse files
committed
refactor: apply option defaults and use sync fs
1 parent 4dfedfb commit d1f0e16

File tree

5 files changed

+176
-234
lines changed

5 files changed

+176
-234
lines changed

packages/unplugin-vue-i18n/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@typescript-eslint/scope-manager": "^8.13.0",
3333
"@typescript-eslint/typescript-estree": "^8.13.0",
3434
"debug": "^4.3.3",
35+
"defu": "^6.1.4",
3536
"fast-glob": "^3.2.12",
3637
"js-yaml": "^4.1.0",
3738
"json5": "^2.2.3",

packages/unplugin-vue-i18n/src/core/options.ts

+59-73
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,82 @@
1+
import defu from 'defu'
12
import { normalize } from 'pathe'
2-
import { isString, isBoolean, isArray } from '@intlify/shared'
3+
import { toArray } from '../utils/misc'
34

45
import type { PluginOptions } from '../types'
56
import type { TranslationDirectiveResolveIndetifier } from '../vue'
67

7-
export function resolveOptions(options: PluginOptions) {
8-
const moduleType = options.module || 'vue-i18n'
9-
10-
// normalize for `options.onlyLocales`
11-
let onlyLocales: string[] = []
12-
if (options.onlyLocales) {
13-
onlyLocales = isArray(options.onlyLocales)
14-
? options.onlyLocales
15-
: [options.onlyLocales]
16-
}
17-
18-
// normalize for `options.include`
19-
let include = options.include
20-
let exclude = undefined
21-
if (include) {
22-
if (isArray(include)) {
23-
include = include.map(item => normalize(item))
24-
} else if (isString(include)) {
25-
include = normalize(include)
26-
}
27-
} else {
28-
exclude = '**/**'
29-
}
30-
31-
const forceStringify = !!options.forceStringify
32-
const defaultSFCLang = isString(options.defaultSFCLang)
33-
? options.defaultSFCLang
34-
: 'json'
35-
const globalSFCScope = !!options.globalSFCScope
36-
37-
const runtimeOnly = isBoolean(options.runtimeOnly)
38-
? options.runtimeOnly
39-
: true
40-
41-
const dropMessageCompiler = !!options.dropMessageCompiler
42-
43-
// prettier-ignore
44-
const fullInstall = moduleType === 'vue-i18n'
45-
? isBoolean(options.fullInstall)
46-
? options.fullInstall
47-
: true
48-
: false
49-
50-
const ssrBuild = !!options.ssr
51-
52-
const allowDynamic = !!options.allowDynamic
53-
54-
const strictMessage = isBoolean(options.strictMessage)
55-
? options.strictMessage
56-
: true
8+
/**
9+
* Creates a path to the correct vue-i18n build used as alias (e.g. `vue-i18n/dist/vue-i18n.runtime.node.js`)
10+
*/
11+
const getVueI18nAliasPath = (opts: {
12+
runtimeOnly: boolean
13+
ssr: boolean
14+
module: string
15+
}) => {
16+
const filename = [
17+
opts.module,
18+
opts.runtimeOnly ? 'runtime' : '',
19+
!opts.ssr ? 'esm-bundler' : 'node',
20+
'js'
21+
]
22+
.filter(Boolean)
23+
.join('.')
24+
25+
return `${opts.module}/dist/${filename}`
26+
}
5727

58-
const escapeHtml = !!options.escapeHtml
28+
/**
29+
* Applies default values to user options and normalizes to narrowed type
30+
*/
31+
export function resolveOptions(options: PluginOptions) {
32+
const res = defu(options, {
33+
ssr: false,
34+
module: 'vue-i18n',
35+
escapeHtml: false,
36+
onlyLocales: [] as string[],
37+
fullInstall: true,
38+
runtimeOnly: true,
39+
strictMessage: true,
40+
allowDynamic: false,
41+
globalSFCScope: false,
42+
forceStringify: false,
43+
defaultSFCLang: 'json',
44+
dropMessageCompiler: false,
45+
transformI18nBlock: undefined,
46+
optimizeTranslationDirective: false
47+
})
48+
49+
const include = res.include
50+
? toArray(res.include).map(item => normalize(item))
51+
: undefined
52+
53+
const exclude = res.include ? undefined : '**/**'
54+
55+
const onlyLocales = toArray(res.onlyLocales)
56+
57+
const fullInstall = res.module !== 'vue-i18n' ? false : res.fullInstall
5958

6059
const optimizeTranslationDirective =
61-
isString(options.optimizeTranslationDirective) ||
62-
isArray(options.optimizeTranslationDirective)
63-
? options.optimizeTranslationDirective
64-
: !!options.optimizeTranslationDirective
60+
typeof res.optimizeTranslationDirective === 'boolean'
61+
? res.optimizeTranslationDirective
62+
: toArray(res.optimizeTranslationDirective)
6563

6664
const translationIdentifiers = new Map<
6765
string,
6866
TranslationDirectiveResolveIndetifier
6967
>()
7068

71-
const transformI18nBlock =
72-
typeof options.transformI18nBlock === 'function'
73-
? options.transformI18nBlock
74-
: null
69+
const vueI18nAliasPath = getVueI18nAliasPath(res)
7570

7671
return {
72+
...res,
7773
include,
7874
exclude,
79-
module: moduleType,
80-
onlyLocales,
81-
forceStringify,
82-
defaultSFCLang,
83-
globalSFCScope,
84-
runtimeOnly,
85-
dropMessageCompiler,
8675
fullInstall,
87-
ssrBuild,
88-
allowDynamic,
89-
strictMessage,
90-
escapeHtml,
91-
optimizeTranslationDirective,
76+
onlyLocales,
77+
vueI18nAliasPath,
9278
translationIdentifiers,
93-
transformI18nBlock
79+
optimizeTranslationDirective
9480
}
9581
}
9682

0 commit comments

Comments
 (0)