mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-06-04 07:49:20 +08:00
* fix: 优化类型提示 * fix: 添加 enums 接口类型声明 * feat: 配置插件api提示 Co-authored-by: wanchun <445436867@qq.com>
90 lines
2.4 KiB
JavaScript
90 lines
2.4 KiB
JavaScript
import { readFileSync } from 'fs';
|
|
import { join } from 'path';
|
|
import { name } from '../package.json';
|
|
|
|
const namespace = 'plugin-locale';
|
|
|
|
export default (api) => {
|
|
const {
|
|
utils: { Mustache },
|
|
} = api;
|
|
|
|
api.describe({
|
|
key: 'locale',
|
|
config: {
|
|
schema(joi) {
|
|
return joi.object();
|
|
},
|
|
default: {},
|
|
onChange: api.ConfigChangeType.regenerateTmpFiles,
|
|
},
|
|
});
|
|
|
|
const { getLocales } = require('./utils');
|
|
|
|
api.addRuntimePluginKey(() => 'locale');
|
|
|
|
const absoluteFilePath = join(namespace, 'core.js');
|
|
|
|
const absRuntimeFilePath = join(namespace, 'runtime.js');
|
|
|
|
function getLocaleFileBasePath() {
|
|
return join(api.paths.absSrcPath, api.config.singular ? 'locale' : 'locales');
|
|
}
|
|
|
|
// 监听 locale 文件改变,重新生成文件
|
|
api.addTmpGenerateWatcherPaths(getLocaleFileBasePath);
|
|
|
|
api.onGenerateFiles(() => {
|
|
// .fes配置
|
|
const userConfig = {
|
|
locale: 'zh-CN', // default locale
|
|
fallbackLocale: 'zh-CN', // set fallback locale
|
|
legacy: true,
|
|
baseNavigator: true, // 开启浏览器语言检测
|
|
...api.config.locale,
|
|
};
|
|
|
|
const localeConfigFileBasePath = getLocaleFileBasePath();
|
|
|
|
const locales = getLocales(localeConfigFileBasePath);
|
|
|
|
api.writeTmpFile({
|
|
path: absoluteFilePath,
|
|
content: Mustache.render(readFileSync(join(__dirname, 'runtime/core.tpl'), 'utf-8'), {
|
|
REPLACE_LOCALES: locales,
|
|
REPLACE_DEFAULT_OPTIONS: JSON.stringify(
|
|
{
|
|
locale: userConfig.locale,
|
|
fallbackLocale: userConfig.fallbackLocale,
|
|
legacy: userConfig.legacy,
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
BASE_NAVIGATOR: userConfig.baseNavigator,
|
|
VUE_I18N_PATH: 'vue-i18n',
|
|
}),
|
|
});
|
|
|
|
api.copyTmpFiles({
|
|
namespace,
|
|
path: join(__dirname, 'runtime'),
|
|
ignore: ['.tpl'],
|
|
});
|
|
});
|
|
|
|
api.addPluginExports(() => [
|
|
{
|
|
specifiers: ['useI18n', 'locale'],
|
|
source: absoluteFilePath,
|
|
},
|
|
]);
|
|
|
|
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
|
|
|
|
api.addConfigType(() => ({
|
|
source: name,
|
|
}));
|
|
};
|