feat: 添加api.copyTmpFiles

plugin建议把运行时代码写在runtime目录,然后通过api.copyTmpFiles复制至项目的.fes/plugin-name目录
This commit is contained in:
万纯 2021-01-12 20:19:14 +08:00
parent efeee56c8e
commit c91d0b5845
29 changed files with 93 additions and 90 deletions

View File

@ -30,7 +30,7 @@ export default (api) => {
api.writeTmpFile({
path: absoluteFilePath,
content: Mustache.render(
readFileSync(join(__dirname, 'template/core.tpl'), 'utf-8'),
readFileSync(join(__dirname, 'runtime/core.tpl'), 'utf-8'),
{
REPLACE_ROLES: JSON.stringify(roles)
}
@ -40,7 +40,7 @@ export default (api) => {
api.writeTmpFile({
path: absRuntimeFilePath,
content: readFileSync(
join(__dirname, 'template/runtime.tpl'),
join(__dirname, 'runtime/runtime.tpl'),
'utf-8'
)
});

View File

@ -1,4 +1,4 @@
import { readFileSync, copyFileSync, statSync } from 'fs';
import { readFileSync } from 'fs';
import { join, basename } from 'path';
import optimizeSvg from './optimizeSvg';
@ -20,6 +20,7 @@ export default (api) => {
// 监听 icons 文件变更,重新生成文件
api.addTmpGenerateWatcherPaths(() => join(api.paths.absSrcPath, 'icons'));
let generatedOnce = false;
api.onGenerateFiles(async () => {
const base = join(api.paths.absSrcPath, 'icons');
const iconFiles = api.utils.glob.sync('**/*', {
@ -40,7 +41,7 @@ export default (api) => {
api.writeTmpFile({
path: `${namespace}/icons.js`,
content: api.utils.Mustache.render(
readFileSync(join(__dirname, './icons.tpl'), 'utf-8'),
readFileSync(join(__dirname, 'runtime/icons.tpl'), 'utf-8'),
{
ICON_NAMES: iconNames
}
@ -49,29 +50,18 @@ export default (api) => {
api.writeTmpFile({
path: absRuntimeFilePath,
content: api.utils.Mustache.render(readFileSync(join(__dirname, 'runtime.tpl'), 'utf-8'), {
content: api.utils.Mustache.render(readFileSync(join(__dirname, 'runtime/runtime.tpl'), 'utf-8'), {
})
});
});
let generatedOnce = false;
api.onGenerateFiles(() => {
if (generatedOnce) return;
generatedOnce = true;
const cwd = join(__dirname, './Icon');
const files = api.utils.glob.sync('**/*', {
cwd
});
const base = join(api.paths.absTmpPath, namespace);
files.forEach((file) => {
const source = join(cwd, file);
const target = join(base, file);
if (statSync(source).isDirectory()) {
api.utils.mkdirp.sync(target);
} else {
copyFileSync(source, target);
}
});
if (!generatedOnce) {
generatedOnce = true;
api.copyTmpFiles({
namespace,
path: join(__dirname, 'runtime'),
ignore: ['.tpl']
});
}
});
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);

View File

@ -1,7 +1,7 @@
<script>
import { computed } from 'vue';
// eslint-disable-next-line
import icons from './icons';
import icons from '../icons';
const noop = () => { };

View File

@ -1,4 +1,4 @@
import Icon from './index';
import Icon from './Icon';
export function onAppCreated({ app }) {
app.component('fes-icon', Icon);

View File

@ -1,4 +1,4 @@
import { readFileSync, copyFileSync, statSync } from 'fs';
import { readFileSync } from 'fs';
import { join } from 'path';
import { winPath } from '@umijs/utils';
@ -35,36 +35,21 @@ export default (api) => {
api.writeTmpFile({
path: absFilePath,
content: Mustache.render(
readFileSync(join(__dirname, 'template/index.tpl'), 'utf-8'),
readFileSync(join(__dirname, 'runtime/index.tpl'), 'utf-8'),
{
REPLACE_USER_CONFIG: JSON.stringify(userConfig),
HAS_LOCALE: api.pkg.dependencies?.['@webank/fes-plugin-locale']
}
)
});
api.copyTmpFiles({
namespace,
path: join(__dirname, 'runtime'),
ignore: ['.tpl']
});
});
let generatedOnce = false;
api.onGenerateFiles(() => {
if (generatedOnce) return;
generatedOnce = true;
const cwd = join(__dirname, '.');
const files = api.utils.glob.sync('**/*', {
cwd
});
const base = join(api.paths.absTmpPath, namespace);
files.forEach((file) => {
if (file.indexOf('template') !== -1) return;
if (file === 'index.js') return;
const source = join(cwd, file);
const target = join(base, file);
if (statSync(source).isDirectory()) {
api.utils.mkdirp.sync(target);
} else {
copyFileSync(source, target);
}
});
});
// 把BaseLayout插入到路由配置中作为跟路由
api.modifyRoutes(routes => [

View File

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 55 KiB

View File

@ -0,0 +1,13 @@
# todo-list
### theme
1. 主题light-白色
2. 主题blue-蓝色
### multiTabs
1. 刷新tab动画从点击开始到页面重新渲染完onMounted事件触发期间刷新按钮旋转
2. 控制最多打开的页面数,减少内存开销,如果能根据内存大小自动判定就更牛逼了
### 面包屑

View File

@ -22,10 +22,6 @@ export default (api) => {
const absRuntimeFilePath = join(namespace, 'runtime.js');
const selectLangComponentFilePath = join(namespace, 'views/SelectLang.vue');
const langConfigFilePath = join(namespace, 'langUConfigMap');
function getLocaleFileBasePath() {
return join(api.paths.absSrcPath, api.config.singular ? 'locale' : 'locales');
}
@ -45,7 +41,7 @@ export default (api) => {
api.writeTmpFile({
path: absoluteFilePath,
content: Mustache.render(
readFileSync(join(__dirname, 'template/core.tpl'), 'utf-8'),
readFileSync(join(__dirname, 'runtime/core.tpl'), 'utf-8'),
{
REPLACE_LOCALES: locales,
REPLACE_DEFAULT_OPTIONS: JSON.stringify(defaultOptions)
@ -56,25 +52,15 @@ export default (api) => {
api.writeTmpFile({
path: absRuntimeFilePath,
content: readFileSync(
join(__dirname, 'template/runtime.tpl'),
join(__dirname, 'runtime/runtime.tpl'),
'utf-8'
)
});
api.writeTmpFile({
path: langConfigFilePath,
content: readFileSync(
join(__dirname, 'template/langUConfigMap.js'),
'utf-8'
)
});
api.writeTmpFile({
path: selectLangComponentFilePath,
content: readFileSync(
join(__dirname, 'views/SelectLang.vue'),
'utf-8'
)
api.copyTmpFiles({
namespace,
path: join(__dirname, 'runtime'),
ignore: ['.tpl']
});
});

View File

@ -23,7 +23,7 @@ if (Array.isArray(locales)) {
const i18n = createI18n({ ...defaultOptions, messages });
// 共享出去
plugin.share("locale", { i18n, SelectLang })
plugin.share("locale", { i18n, SelectLang });
const setLocale = (locale)=>{
i18n.global.locale = locale

View File

@ -58,20 +58,20 @@ export default (api) => {
api.writeTmpFile({
path: absCoreFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'template/core.tpl'), 'utf-8'), {
content: Mustache.render(readFileSync(join(__dirname, 'runtime/core.tpl'), 'utf-8'), {
...tmpFiles
})
});
api.writeTmpFile({
path: absRuntimeFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'template/runtime.tpl'), 'utf-8'), {
content: Mustache.render(readFileSync(join(__dirname, 'runtime/runtime.tpl'), 'utf-8'), {
})
});
api.writeTmpFile({
path: absInitlaStateFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'models/initialState.tpl'), 'utf-8'), {
content: Mustache.render(readFileSync(join(__dirname, 'runtime/models/initialState.tpl'), 'utf-8'), {
})
});
});

View File

@ -1,4 +1,4 @@
import { readFileSync, copyFileSync, statSync } from 'fs';
import { readFileSync } from 'fs';
import { join } from 'path';
export default (api) => {
@ -38,20 +38,10 @@ export default (api) => {
api.onGenerateFiles(() => {
if (generatedOnce) return;
generatedOnce = true;
const cwd = join(__dirname, './template');
const files = api.utils.glob.sync('**/*', {
cwd
});
const base = join(api.paths.absTmpPath, namespace);
files.forEach((file) => {
if (['request.js'].includes(file)) return;
const source = join(cwd, file);
const target = join(base, file);
if (statSync(source).isDirectory()) {
api.utils.mkdirp.sync(target);
} else {
copyFileSync(source, target);
}
api.copyTmpFiles({
namespace,
path: join(__dirname, 'template'),
ignore: ['request.js']
});
});

View File

@ -1,6 +1,8 @@
import assert from 'assert';
import { dirname, join } from 'path';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import {
existsSync, statSync, readFileSync, writeFileSync, copyFileSync
} from 'fs';
export default function (api) {
[
@ -40,7 +42,7 @@ export default function (api) {
}) {
assert(
api.stage >= api.ServiceStage.pluginReady,
'api.writeTmpFile() should not execute in register stage.',
'api.writeTmpFile() should not execute in register stage.'
);
const absPath = join(api.paths.absTmpPath, path);
api.utils.mkdirp.sync(dirname(absPath));
@ -49,4 +51,41 @@ export default function (api) {
}
}
});
api.registerMethod({
name: 'copyTmpFiles',
fn({
namespace, path, ignore
}) {
assert(
api.stage >= api.ServiceStage.pluginReady,
'api.copyTmpFiles() should not execute in register stage.'
);
assert(
path,
'api.copyTmpFiles() should has param path'
);
assert(
namespace,
'api.copyTmpFiles() should has param namespace'
);
const files = api.utils.glob.sync('**/*', {
cwd: path
});
const base = join(api.paths.absTmpPath, namespace);
files.forEach((file) => {
const source = join(path, file);
const target = join(base, file);
if (statSync(source).isDirectory()) {
api.utils.mkdirp.sync(target);
} else if (Array.isArray(ignore)) {
if (!ignore.some(pattern => new RegExp(pattern).test(file))) {
copyFileSync(source, target);
}
} else {
copyFileSync(source, target);
}
});
}
});
}