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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
import Icon from './index'; import Icon from './Icon';
export function onAppCreated({ app }) { export function onAppCreated({ app }) {
app.component('fes-icon', Icon); 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 { join } from 'path';
import { winPath } from '@umijs/utils'; import { winPath } from '@umijs/utils';
@ -35,36 +35,21 @@ export default (api) => {
api.writeTmpFile({ api.writeTmpFile({
path: absFilePath, path: absFilePath,
content: Mustache.render( 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), REPLACE_USER_CONFIG: JSON.stringify(userConfig),
HAS_LOCALE: api.pkg.dependencies?.['@webank/fes-plugin-locale'] 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插入到路由配置中作为跟路由 // 把BaseLayout插入到路由配置中作为跟路由
api.modifyRoutes(routes => [ 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 absRuntimeFilePath = join(namespace, 'runtime.js');
const selectLangComponentFilePath = join(namespace, 'views/SelectLang.vue');
const langConfigFilePath = join(namespace, 'langUConfigMap');
function getLocaleFileBasePath() { function getLocaleFileBasePath() {
return join(api.paths.absSrcPath, api.config.singular ? 'locale' : 'locales'); return join(api.paths.absSrcPath, api.config.singular ? 'locale' : 'locales');
} }
@ -45,7 +41,7 @@ export default (api) => {
api.writeTmpFile({ api.writeTmpFile({
path: absoluteFilePath, path: absoluteFilePath,
content: Mustache.render( content: Mustache.render(
readFileSync(join(__dirname, 'template/core.tpl'), 'utf-8'), readFileSync(join(__dirname, 'runtime/core.tpl'), 'utf-8'),
{ {
REPLACE_LOCALES: locales, REPLACE_LOCALES: locales,
REPLACE_DEFAULT_OPTIONS: JSON.stringify(defaultOptions) REPLACE_DEFAULT_OPTIONS: JSON.stringify(defaultOptions)
@ -56,25 +52,15 @@ export default (api) => {
api.writeTmpFile({ api.writeTmpFile({
path: absRuntimeFilePath, path: absRuntimeFilePath,
content: readFileSync( content: readFileSync(
join(__dirname, 'template/runtime.tpl'), join(__dirname, 'runtime/runtime.tpl'),
'utf-8' 'utf-8'
) )
}); });
api.writeTmpFile({ api.copyTmpFiles({
path: langConfigFilePath, namespace,
content: readFileSync( path: join(__dirname, 'runtime'),
join(__dirname, 'template/langUConfigMap.js'), ignore: ['.tpl']
'utf-8'
)
});
api.writeTmpFile({
path: selectLangComponentFilePath,
content: readFileSync(
join(__dirname, 'views/SelectLang.vue'),
'utf-8'
)
}); });
}); });

View File

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

View File

@ -58,20 +58,20 @@ export default (api) => {
api.writeTmpFile({ api.writeTmpFile({
path: absCoreFilePath, 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 ...tmpFiles
}) })
}); });
api.writeTmpFile({ api.writeTmpFile({
path: absRuntimeFilePath, 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({ api.writeTmpFile({
path: absInitlaStateFilePath, 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'; import { join } from 'path';
export default (api) => { export default (api) => {
@ -38,20 +38,10 @@ export default (api) => {
api.onGenerateFiles(() => { api.onGenerateFiles(() => {
if (generatedOnce) return; if (generatedOnce) return;
generatedOnce = true; generatedOnce = true;
const cwd = join(__dirname, './template'); api.copyTmpFiles({
const files = api.utils.glob.sync('**/*', { namespace,
cwd path: join(__dirname, 'template'),
}); ignore: ['request.js']
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);
}
}); });
}); });

View File

@ -1,6 +1,8 @@
import assert from 'assert'; import assert from 'assert';
import { dirname, join } from 'path'; import { dirname, join } from 'path';
import { existsSync, readFileSync, writeFileSync } from 'fs'; import {
existsSync, statSync, readFileSync, writeFileSync, copyFileSync
} from 'fs';
export default function (api) { export default function (api) {
[ [
@ -40,7 +42,7 @@ export default function (api) {
}) { }) {
assert( assert(
api.stage >= api.ServiceStage.pluginReady, 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); const absPath = join(api.paths.absTmpPath, path);
api.utils.mkdirp.sync(dirname(absPath)); 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);
}
});
}
});
} }