-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
perf: mark functions as @__NO_SIDE_EFFECTS__
#995
perf: mark functions as @__NO_SIDE_EFFECTS__
#995
Conversation
Co-authored-by: serkodev <[email protected]>
Thank you Anthony and SerKo. What bundlers support this comment? Only Rollup? Shouldn't Hint: My tests with import * as v from 'valibot'
const LoginSchema = /* @__PURE__ */ v.object({
email: v.pipe(v.string(), v.email()),
password: v.pipe(v.string(), v.minLength(8)),
}) |
The Sorry for the misleading. In the test code, the Here is the correct version, it will be tree-shaken successfully: import * as v from 'valibot';
const LoginSchema = v.object({
email: /* @__PURE__ */ v.pipe(v.string(), v.email()),
password: /* @__PURE__ */ v.pipe(v.string(), v.minLength(8)),
}); |
Yeah, theoretically, you need mark every call to be pure to ensure it's complete side-effects free: import * as v from 'valibot';
const LoginSchema = /* @__PURE__ */ v.object({
email: /* @__PURE__ */ v.pipe(/* @__PURE__ */ v.string(), /* @__PURE__ */ v.email()),
password: /* @__PURE__ */ v.pipe(/* @__PURE__ */ v.string(), /* @__PURE__ */ v.minLength(8)),
}); It's just that Rollup is trying to be smart and looking into the implementation to infer that
|
Thank you! |
v1.0.0-beta.10 is available |
This PR adds
@__NO_SIDE_EFFECTS__
notation for all the schema functions:https://github.com/javascript-compiler-hints/compiler-notations-spec/blob/main/no-side-effects-notation-spec.md
This would improve tree-shaking of Valibot on users side. For example, the snippet from the docs:
Because the
object()
can not be treated as side-effects free, evenLoginSchema
is not used, the final bundle still contains an orphanobject()
call with it's all dependencies (bundle ~3KB).One solution is to add
/* @__PURE__ */
on the user side,The downside is this would require the user's manual effort on every schema.
With
@__NO_SIDE_EFFECTS__
, it will give a hint to the bundler to knowobject()
is side-effects free, and eventually make the whole file shakable (bundle 0KB)Example of bundling using Rollup: https://stackblitz.com/edit/node-5rxpc1jl
This PR is made from the discussion with @serkodev