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

View File

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

View File

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

View File

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