harrywan 2717e539d1 * feat(plugin-layout): layout支持运行时配置menus (#113)
* feat(plugin-layout): 支持运行时配置menus

* refactor(plugin-model): 去掉provide

* refactor(plugin-layout): 优化runtimeConfig,重新实现运行时menus配置方式,修复多页时setup执行两次的bug

* feat(plugin-layout): 菜单支持配置默认展开等

* refactor: 优化
2022-04-11 19:40:16 +08:00

87 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { readFileSync } from 'fs';
import { join } from 'path';
import { winPath } from '@fesjs/utils';
const namespace = 'plugin-layout';
export default (api) => {
const {
utils: { Mustache }
} = api;
const helper = require('./node/helper');
api.describe({
key: 'layout',
config: {
schema(joi) {
return joi.object();
},
onChange: api.ConfigChangeType.regenerateTmpFiles
}
});
api.addRuntimePluginKey(() => 'layout');
const absFilePath = join(namespace, 'index.js');
const absRuntimeFilePath = join(namespace, 'runtime.js');
api.onGenerateFiles(async () => {
const { name } = api.pkg;
const HAS_LOCALE = api.hasPlugins(['@fesjs/plugin-locale']);
// .fes配置
const userConfig = {
title: name,
footer: 'Created by Fes.js',
...(api.config.layout || {})
};
const iconNames = helper.getIconNamesFromMenu(userConfig.menus);
const iconsString = iconNames.map(
iconName => `import { ${iconName} } from '@fesjs/fes-design/icon'`
);
api.writeTmpFile({
path: join(namespace, 'icons.js'),
content: `
${iconsString.join(';\n')}
export default {
${iconNames.join(',\n')}
}`
});
api.writeTmpFile({
path: absFilePath,
content: Mustache.render(
readFileSync(join(__dirname, 'runtime/index.tpl'), 'utf-8'),
{
REPLACE_USER_CONFIG: JSON.stringify(userConfig),
HAS_LOCALE
}
)
});
api.copyTmpFiles({
namespace,
path: join(__dirname, 'runtime'),
ignore: ['.tpl']
});
});
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
// 把BaseLayout插入到路由配置中作为根路由
api.modifyRoutes(routes => [
{
path: '/',
component: winPath(
join(api.paths.absTmpPath || '', absFilePath)
),
children: routes
}
]);
};