Skip to content

Commit

Permalink
Merge pull request #16 from farm-fe/chore/version
Browse files Browse the repository at this point in the history
chore: bump version
  • Loading branch information
ErKeLost authored May 26, 2024
2 parents dd682dc + c593603 commit 0870a5d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-eagles-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'farmup': patch
---

optimize build performance
22 changes: 20 additions & 2 deletions src/core/proxyCompiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export class ProxyCompiler {

private lastResources: string[] = [];
private _preProxyFnList: (keyof Compiler)[] = [];
private alreadyProxyFnList: Set<keyof Compiler> = new Set();

start(compiler: Compiler) {
const isRestart = !!this.compiler;
this.compiler = compiler;

if (this._preProxyFnList.length) {
Expand All @@ -21,6 +23,15 @@ export class ProxyCompiler {

this._preProxyFnList = [];
}

if (isRestart) {
const proxyFnList = this.alreadyProxyFnList;
this.alreadyProxyFnList = new Set();
for (const fnName of proxyFnList) {
this.proxyCompiler(fnName);
}
}

this.on('resources', (r) => {
this.lastResources = Object.keys(r.result);
});
Expand All @@ -30,11 +41,18 @@ export class ProxyCompiler {
return this.lastResources;
}

proxyCompiler<K extends keyof Compiler>(fnName: K) {
private proxyCompiler<K extends keyof Compiler>(fnName: K) {
if (!this.compiler) {
this._preProxyFnList.push(fnName);
return;
}

if (this.alreadyProxyFnList.has(fnName)) {
return;
}

this.alreadyProxyFnList.add(fnName);

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
proxyCompilerFn(this.compiler, fnName, (...args: any[]) => this.event.emit(fnName, ...args));
}
Expand All @@ -46,7 +64,7 @@ export class ProxyCompiler {
OFT extends Record<keyof T, (...args: any) => any> = {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
[KK in keyof T]: T[KK] extends (...args: any[]) => any ? T[KK] : never;
},
}
>(fnName: K, fn: (context: FnContext<Parameters<OFT[K]>, ReturnType<OFT[K]>>) => void) {
this.proxyCompiler(fnName as keyof Compiler);
this.event.on(fnName.toString(), fn);
Expand Down
4 changes: 3 additions & 1 deletion src/core/proxyCompiler/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import type { Compiler } from '@farmfe/core';
export function defineProperty<O, K extends keyof O, V extends O[K]>(obj: O, key: K, value: V): V {
const origin = obj[key];

const descriptor = Object.getOwnPropertyDescriptor(obj, key);
Object.defineProperty(obj, key, {
value,
...descriptor,
});

return origin as V;
Expand All @@ -19,7 +21,7 @@ export function proxyCompilerFn<
},
K extends keyof OFT = keyof OFT,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
F extends (...args: any) => any = OFT[K],
F extends (...args: any) => any = OFT[K]
>(compiler: T, fnName: K, callback: F) {
const handler = ((...args: Parameters<OFT[K]>) => {
const r = origin.bind(compiler)(...args);
Expand Down

0 comments on commit 0870a5d

Please sign in to comment.