Skip to content

Commit

Permalink
feat: Throw an exception for duplicate runtime plugin keys
Browse files Browse the repository at this point in the history
  • Loading branch information
PopperLi committed Sep 4, 2023
1 parent 0b55462 commit f24b3d5
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions packages/preset-umi/src/features/tmpFiles/tmpFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,27 +430,44 @@ if (process.env.NODE_ENV === 'development') {
key: 'addRuntimePlugin',
initialValue: [api.appData.appJS?.path].filter(Boolean),
});
const validKeys = [
...new Set(
await api.applyPlugins({
key: 'addRuntimePluginKey',
initialValue: [
'patchRoutes',
'patchClientRoutes',
'modifyContextOpts',
'modifyClientRenderOpts',
'rootContainer',
'innerProvider',
'i18nProvider',
'accessProvider',
'dataflowProvider',
'outerProvider',
'render',
'onRouteChange'
]
})
)
];

function checkDuplicatePluginKeys(arr: string[]) {
const duplicates: string[] = [];
arr.reduce((prev, curr) => {
if (prev[curr]) {
duplicates.push(curr);
} else {
prev[curr] = true;
}
return prev;
}, {} as { [k: string]: boolean });
if (duplicates.length) {
throw new Error(
`The plugin key cannot be duplicated. (${duplicates.join(', ')})`,
);
}
}

const validKeys = await api.applyPlugins({
key: 'addRuntimePluginKey',
initialValue: [
'patchRoutes',
'patchClientRoutes',
'modifyContextOpts',
'modifyClientRenderOpts',
'rootContainer',
'innerProvider',
'i18nProvider',
'accessProvider',
'dataflowProvider',
'outerProvider',
'render',
'onRouteChange',
],
});

checkDuplicatePluginKeys(validKeys);

const appPluginRegExp = /(\/|\\)app.(ts|tsx|jsx|js)$/;
api.writeTmpFile({
noPluginDir: true,
Expand Down

0 comments on commit f24b3d5

Please sign in to comment.