|
| 1 | +import defu from 'defu' |
1 | 2 | import { normalize } from 'pathe'
|
2 |
| -import { isString, isBoolean, isArray } from '@intlify/shared' |
| 3 | +import { toArray } from '../utils/misc' |
3 | 4 |
|
4 | 5 | import type { PluginOptions } from '../types'
|
5 | 6 | import type { TranslationDirectiveResolveIndetifier } from '../vue'
|
6 | 7 |
|
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 | +} |
57 | 27 |
|
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 |
59 | 58 |
|
60 | 59 | 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) |
65 | 63 |
|
66 | 64 | const translationIdentifiers = new Map<
|
67 | 65 | string,
|
68 | 66 | TranslationDirectiveResolveIndetifier
|
69 | 67 | >()
|
70 | 68 |
|
71 |
| - const transformI18nBlock = |
72 |
| - typeof options.transformI18nBlock === 'function' |
73 |
| - ? options.transformI18nBlock |
74 |
| - : null |
| 69 | + const vueI18nAliasPath = getVueI18nAliasPath(res) |
75 | 70 |
|
76 | 71 | return {
|
| 72 | + ...res, |
77 | 73 | include,
|
78 | 74 | exclude,
|
79 |
| - module: moduleType, |
80 |
| - onlyLocales, |
81 |
| - forceStringify, |
82 |
| - defaultSFCLang, |
83 |
| - globalSFCScope, |
84 |
| - runtimeOnly, |
85 |
| - dropMessageCompiler, |
86 | 75 | fullInstall,
|
87 |
| - ssrBuild, |
88 |
| - allowDynamic, |
89 |
| - strictMessage, |
90 |
| - escapeHtml, |
91 |
| - optimizeTranslationDirective, |
| 76 | + onlyLocales, |
| 77 | + vueI18nAliasPath, |
92 | 78 | translationIdentifiers,
|
93 |
| - transformI18nBlock |
| 79 | + optimizeTranslationDirective |
94 | 80 | }
|
95 | 81 | }
|
96 | 82 |
|
|
0 commit comments