feat: 优化export插件中对待导出文件的地址计算

This commit is contained in:
万纯 2020-12-02 15:33:53 +08:00
parent de14cee9a7
commit 0c88e947e5
4 changed files with 32 additions and 28 deletions

View File

@ -1,5 +1,6 @@
import { lodash, winPath } from '@umijs/utils';
import assert from 'assert';
import path from 'path';
const reserveLibrarys = ['fes']; // reserve library
// todo 插件导出内容冲突问题待解决
@ -12,51 +13,49 @@ const reserveExportsNames = [
'Route'
];
export function generateExports({
item,
fesExportsHook
}) {
export function generateExports(basePath, { item, fesExportsHook }) {
assert(item.source, 'source should be supplied.');
const source = path.relative(path.basename(basePath), item.source);
assert(
item.exportAll || item.specifiers,
'exportAll or specifiers should be supplied.',
'exportAll or specifiers should be supplied.'
);
assert(
!reserveLibrarys.includes(item.source),
`${item.source} is reserve library, Please don't use it.`,
!reserveLibrarys.includes(source),
`${source} is reserve library, Please don't use it.`
);
if (item.exportAll) {
return `export * from '${winPath(item.source)}';`;
return `export * from '${winPath(source)}';`;
}
assert(
Array.isArray(item.specifiers),
`specifiers should be Array, but got ${item.specifiers.toString()}.`,
`specifiers should be Array, but got ${item.specifiers.toString()}.`
);
const specifiersStrArr = item.specifiers.map((specifier) => {
if (typeof specifier === 'string') {
assert(
!reserveExportsNames.includes(specifier),
`${specifier} is reserve name, you can use 'exported' to set alias.`,
`${specifier} is reserve name, you can use 'exported' to set alias.`
);
assert(
!fesExportsHook[specifier],
`${specifier} is Defined, you can use 'exported' to set alias.`,
`${specifier} is Defined, you can use 'exported' to set alias.`
);
fesExportsHook[specifier] = true;
return specifier;
}
assert(
lodash.isPlainObject(specifier),
`Configure item context should be Plain Object, but got ${specifier}.`,
`Configure item context should be Plain Object, but got ${specifier}.`
);
assert(
specifier.local && specifier.exported,
'local and exported should be supplied.',
'local and exported should be supplied.'
);
return `${specifier.local} as ${specifier.exported}`;
});
return `export { ${specifiersStrArr.join(', ')} } from '${winPath(
item.source,
source
)}';`;
}
@ -69,15 +68,15 @@ export default function (api) {
});
const fesExportsHook = {}; // repeated definition
const absoluteFilePath = 'core/exports.js';
api.writeTmpFile({
path: 'core/exports.js',
content:
`${fesExports
.map(item => generateExports({
item,
fesExportsHook
}))
.join('\n')}\n`
path: absoluteFilePath,
content: `${fesExports
.map(item => generateExports(absoluteFilePath, {
item,
fesExportsHook
}))
.join('\n')}\n`
});
});
}

View File

@ -9,6 +9,8 @@ export default function (api) {
utils: { Mustache }
} = api;
const absoluteFilePath = 'core/plugin.js';
api.onGenerateFiles(async () => {
const validKeys = await api.applyPlugins({
key: 'addRuntimePluginKey',
@ -31,7 +33,7 @@ export default function (api) {
].filter(Boolean)
});
api.writeTmpFile({
path: 'core/plugin.js',
path: absoluteFilePath,
content: Mustache.render(
readFileSync(join(__dirname, 'plugin.tpl'), 'utf-8'),
{
@ -56,6 +58,6 @@ export default function (api) {
api.addExports(() => ({
specifiers: ['plugin'],
source: './plugin'
source: absoluteFilePath
}));
}

View File

@ -11,11 +11,13 @@ export default function (api) {
utils: { Mustache }
} = api;
const absoluteFilePath = 'core/routes.js';
api.onGenerateFiles(async () => {
const routesTpl = readFileSync(join(__dirname, 'routes.tpl'), 'utf-8');
const routes = await api.getRoutes();
api.writeTmpFile({
path: 'core/routes.js',
path: absoluteFilePath,
content: Mustache.render(routesTpl, {
runtimePath,
routes: routesToJSON({ routes, config: api.config }),
@ -26,6 +28,6 @@ export default function (api) {
api.addExports(() => ({
specifiers: ['router'],
source: './routes.js'
source: absoluteFilePath
}));
}

View File

@ -22,12 +22,13 @@ export default (api) => {
});
const namespace = 'plugin-request';
const absoluteFilePath = `${namespace}/request.js`;
const requestTemplate = readFileSync(join(__dirname, 'template', 'request.js'), 'utf-8');
api.onGenerateFiles(() => {
// 文件写出
const { dataField = '', messageUI } = api.config.request;
api.writeTmpFile({
path: `${namespace}/request.js`,
path: absoluteFilePath,
content: requestTemplate
.replace('REPLACE_MESSAGE_UI', messageUI || 'ant-design-vue')
.replace('REPLACE_DATA_FIELD', dataField)
@ -58,7 +59,7 @@ export default (api) => {
api.addExports(() => [
{
exportAll: true,
source: `../${namespace}/request.js`
source: absoluteFilePath
}
]);
};