Skip to content

Commit

Permalink
Add lazyLoad to spacepack
Browse files Browse the repository at this point in the history
  • Loading branch information
NotNite committed Dec 9, 2023
1 parent 4b93f9c commit ba23af8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/core-extensions/src/spacepack/webpackModules/spacepack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ export const spacepack: Spacepack = {
)
)?.[0]?.[1] ?? null
);
},

lazyLoad: (find: string | RegExp | (string | RegExp)[], match: RegExp) => {
const module = Array.isArray(find)
? spacepack.findByCode(...find)
: spacepack.findByCode(find);
if (module.length < 1) return Promise.reject("Find failed");

const findId = module[0].id;
const findCode = webpackRequire.m[findId].toString().replace(/\n/g, "");

const matchResult = findCode.match(match);
if (!matchResult) return Promise.reject("Match failed");

const matchId = matchResult[1];
return webpackRequire.el(matchId).then(() => webpackRequire(matchId));
}
};

Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/coreExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export type Spacepack = {
...strings: (string | RegExp)[]
// eslint-disable-next-line @typescript-eslint/ban-types
) => Function | null;
lazyLoad: (
find: string | RegExp | (string | RegExp)[],
match: RegExp
) => Promise<any>;
};

export type NoticeProps = {
Expand Down

0 comments on commit ba23af8

Please sign in to comment.