Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve production build #158

Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@
"preact": "10.17.1",
"typescript": "$typescript",
"vite": "$vite",
"vue": "3.3.4"
"vue": "3.3.4",
"terser": "^5.31.0"
},
"peerDependencyRules": {
"allowAny": [
Expand Down
8 changes: 4 additions & 4 deletions packages/preact/preact/src/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export class ComponentFrame {
): FinalizedFormula {
const frame = ComponentFrame.#frames.get(component);

verify(
verify?.(
frame,
isPresent,
expected.when("in Preact's _diff hook").as("a tracking frame"),
expected?.when("in Preact's _diff hook").as("a tracking frame"),
);

const end = frame.#end(subscription);
Expand Down Expand Up @@ -101,10 +101,10 @@ export class ComponentFrame {
}

#end(subscription: (() => void) | undefined) {
verify(
verify?.(
this.#active,
isPresent,
expected.when("in preact's _diff hook").as("an active tracking frame"),
expected?.when("in preact's _diff hook").as("an active tracking frame"),
);

const frame = (this.#frame = this.#active.done());
Expand Down
4 changes: 2 additions & 2 deletions packages/universal/collections/src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export const cached = <T>(
): void => {
const get = descriptor.get;

verify(
verify?.(
get,
isPresent,
expected(`the target of @cached`)
expected?.(`the target of @cached`)
.toBe(`a getter`)
.butGot(() =>
typeof descriptor.value === "function" ? `a method` : `a field`,
Expand Down
2 changes: 1 addition & 1 deletion packages/universal/debug/src/call-stack/debug/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function callerStack(
"An error created in the internals of Stack.create",
).stack;

verify(stack, hasType("string"));
verify?.(stack, hasType("string"));
return callStack(stack)?.slice(internal + CALLER);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/universal/modifier/src/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export function ElementPlaceholder<E extends anydom.Element>(

initialize(value: anydom.Element): void {
const element = verified(REFS.get(ref), isPresent);
verify(
verify?.(
value,
(anyElement): anyElement is E => anyElement instanceof type,
expected(`A ref (${describe(description)})`)
expected?.(`A ref (${describe(description)})`)
.toBe(`initialized with an instance of ${type.name}`)
.butGot(() => `an instance of ${value.constructor.name}`),
);
Expand Down
12 changes: 6 additions & 6 deletions packages/universal/verify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import type { VerifyFn } from "./src/verify.js";
import {
expected as expectedDev,
verified as verifiedDev,
verify as verifyDev
} from "./src/verify.js";

export {
verified
} from "./src/verify.js";

const noop: unknown = () => { };

export {
Expand All @@ -23,13 +26,10 @@
export { type TypeOf } from "./src/assertions/types.js";
export { type Expectation, VerificationError } from "./src/verify.js";

export const expected: typeof expectedDev = import.meta.env.DEV ? expectedDev : (noop as typeof expectedDev)
export const expected: typeof expectedDev = import.meta.env.DEV ? expectedDev : null as any

Check warning on line 29 in packages/universal/verify/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Unsafe assignment of an `any` value

Check failure on line 29 in packages/universal/verify/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
export const hasType: typeof hasTypeDev = import.meta.env.DEV ? hasTypeDev : (noop as typeof hasTypeDev)
export const isOneOf: typeof isOneOfDev = import.meta.env.DEV ? isOneOfDev : (noop as typeof isOneOfDev)

export const verify: VerifyFn = import.meta.env.DEV
? verifyDev
: (noop as VerifyFn);
export const verified: (typeof verifiedDev)["noop"] = import.meta.env.DEV
? verifiedDev
: (noop as typeof verifiedDev);
: null as unknown as VerifyFn;
19 changes: 19 additions & 0 deletions packages/universal/verify/src/assertions/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,22 @@ export function isNullable<In, Out extends In>(

return verify;
}


if (import.meta.env.DEV) {
expected.associate(isPresent, expected.toBe("present"));
expected.associate(hasItems, expected.toHave(`at least one item`));
expected.associate(
isWeakKey,
expected
.toBe("an object or function")
.butGot((value) => (value === null ? "null" : typeof value)),
);
expected.associate(
isObject,
expected
.toBe("an object")
.butGot((value) => (value === null ? "null" : typeof value)),
);

}
92 changes: 45 additions & 47 deletions packages/universal/verify/src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ export function verify<Value, Narrow extends Value>(
check: (input: Value) => input is Narrow,
error?: Expectation<Value>,
): asserts value is Narrow;
export function verify(
export function verify(): void {
// eslint-disable-next-line prefer-rest-params
const params = [...arguments] as Parameters<typeof verifyFunc>;
if (import.meta.env.DEV) {
verifyFunc(...params)
}
}
function verifyFunc(
value: unknown,
check: (input: unknown) => boolean,
error?: Expectation<unknown>,
Expand All @@ -40,33 +47,20 @@ export function verify(
}
}

verify.noop = (() => {
/** noop */
}) as unknown as Exclude<typeof verify, "noop">;

function noop(): void {
return;
}

verify.noop = noop as unknown as VerifyFn;

export function verified<T, U extends T>(
value: T,
check: (input: T) => input is U,
error?: Expectation<T>,
): U {
verify(value, check, error);
return value;
}

verified.noop = <const T, const U extends T>(
value: T,
_check: (input: T) => input is U,
_error?: Expectation<T>,
): U => {
return value as U;
};
): U;

export function verified(v: unknown): unknown {
if (import.meta.env.DEV) {
// eslint-disable-next-line prefer-rest-params
const [value, check, error] = [...arguments] as Parameters<typeof verified>;
Copy link
Contributor Author

@patricklx patricklx May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this hides extra function params from terser, so terser can optimize the prod build and inline this function.
terser will then replace verified(x) with x

verify(value, check, error);
}
return v;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class Expectation<In = any> {
static create<In>(description?: string): Expectation<In> {
Expand Down Expand Up @@ -221,29 +215,41 @@ export function expected(description?: string): Expectation {
return Expectation.create(description);
}

expected.as = expected;
expected.toBe = (kind: string): Expectation => expected().toBe(kind);
expected.toHave = (items: string): Expectation => expected().toHave(items);
expected.when = (situation: string): Expectation => expected().when(situation);
expected.butGot = <In>(
kind: string | ((input: In) => string),
): Expectation<In> => expected().butGot(kind);
if (import.meta.env.DEV) {
expected.as = expected;
expected.toBe = (kind: string): Expectation => expected().toBe(kind);
expected.toHave = (items: string): Expectation => expected().toHave(items);
expected.when = (situation: string): Expectation => expected().when(situation);
expected.butGot = <In>(
kind: string | ((input: In) => string),
): Expectation<In> => expected().butGot(kind);


// eslint-disable-next-line @typescript-eslint/no-explicit-any
expected.associate = <Check extends (input: In) => any, In>(
check: Check,
expectation: Expectation<In>,
): Check extends infer C ? C : never => {
ASSOCIATED.set(check, expectation);
return check as Check extends infer C ? C : never;
};
expected.updated = <In, NewIn = In>(
check: (input: In) => boolean,
updater: Updater<In, NewIn>,
): Expectation<NewIn> => {
const expectation = ASSOCIATED.get(check) ?? expected();

return expectation.update(updater);
};
}


// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyFn = (...args: any[]) => any;

// eslint-disable-next-line @typescript-eslint/consistent-generic-constructors
const ASSOCIATED: WeakMap<AnyFn, Expectation> = new WeakMap();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
expected.associate = <Check extends (input: In) => any, In>(
check: Check,
expectation: Expectation<In>,
): Check extends infer C ? C : never => {
ASSOCIATED.set(check, expectation);
return check as Check extends infer C ? C : never;
};

interface Updater<In, NewIn = In> {
description?: (description: string | undefined) => string | undefined;
to?: (to: To | undefined) => string | To | undefined;
Expand All @@ -253,11 +259,3 @@ interface Updater<In, NewIn = In> {
when?: (when: string | undefined) => string | undefined;
}

expected.updated = <In, NewIn = In>(
check: (input: In) => boolean,
updater: Updater<In, NewIn>,
): Expectation<NewIn> => {
const expectation = ASSOCIATED.get(check) ?? expected();

return expectation.update(updater);
};
59 changes: 54 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions workspace/dev-compile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"rollup": "^4.1.4",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-ts": "^3.4.5",
"rollup-plugin-prettier": "^4.1.1",

Check failure on line 29 in workspace/dev-compile/package.json

View workflow job for this annotation

GitHub Actions / Lint

Expected object keys to be in natural ascending order. 'rollup-plugin-prettier' should be before 'rollup-plugin-ts'
"typescript": "^5.2.2"
},
"devDependencies": {
Expand Down
Loading
Loading