Open SLaks opened 1 month ago
If it's really important to reduce bundle size right now before undertaking such an ambitious refactor, we could do a minor refactor and move Sedra
class (and related parshiyot
array) from @hebcal/core
to @hebcal/hdate
and your app could be changed to depend only on the latter. Or make a @hebcal/sedra
package that depends only on the latter. Either way it could be done in a way that doesn't break compatibility for @hebcal/core
clients because we would still roll it up within@hebcal/core
I use HebrewCalendar.calendar()
here, so that won't help. On further thought, though, I could probably drop that and instead loop over all dates and simply filter out those that have no leinings.
If I do that, I could probably fix this much more simply by making getSedra_()
public.
Either way (even if do the full split/refactor), I would also need to refactor hebcal/hebcal-leyning to remove all references to HebrewCalendar
; see hebcal/hebcal-leyning#467.
In particular, it calls HebrewCalendar.getHolidaysOnDate()
. The simplest approach there is to move it to holidays.ts
without changing the API at all (and make the original wrap the moved version). I don't know how much code holidays.ts
itself depends on, but it doesn't seem to have any large, non-core dependencies (HolidayEvent
and staticHolidays
are pretty large, but those are always required), so it probably isn't worth doing any further refactors (dropping YomKippurKatanEvent
and modern.ts
wouldn't save much code)
Tree shaking should then automatically remove the large dependencies (the entire HebrewCalendar
class and everything it references), since I would no longer reference it at all.
I don't think this is particularly urgent for me right now, but I did start working on it.
Looping over all dates is pretty easy. You'll find this idiom in a few places:
const startAbs = HDate.hebrew2abs(year, TISHREI, 1);
const endAbs = HDate.hebrew2abs(year + 1, TISHREI, 1) - 1;
for (let absDt = startAbs; absDt <= endAbs; absDt++) {
const events = HebrewCalendar.getHolidaysOnDate(absDt);
if (events) {
// ...
}
}
Taking a look at this, I have a question for you about how to avoid pulling the Zmanim dependency without breaking backwards compatibility
holidays.ts
depends on HolidayEvent
, which is used for most of your typical holidaysHolidayEvent
has optional properties startEvent
and endEvent
which are both of type TimedEvent
. For a calendar that includes no zmanim, these will always be undefined
. However, if the user specifies a location for candle-lighting times, the event instance for a fast day (for example Tzom Gedaliah) will have those two properties as references to a "Fast begins" event and a "Fast ends" event. You'll see where these properties get set in https://github.com/hebcal/hebcal-es6/blob/main/src/candles.ts#L76TimedEvent
depends on Location
and Zmanim
, which pulls in the huge dependencies you are trying to avoidIf we wanted to remove that dependency, how would we handle it? As I'm relatively new to TS, I'm totally open to your suggestions! Do we remove startEvent
and endEvent
properties from HolidayEvent
, and create a FastDayEvent
subclass that has those properties?
Done in https://github.com/hebcal/hebcal-es6/commit/c31e0076f0f3669258e0cca0f78f391ab0e078f6
Sorry; I already implemented all of this (except refactoring the calendar()
API; I'm going to follow your advice and use a loop instead).
I'm now testing it against leyning.
Oh, sorry, I didn't realize you were working on this today. I apologize that my getHolidaysOnDate refactor probably caused a bit of a merge mess for you
Your refactor is actually a duplicate of https://github.com/hebcal/hebcal-es6/commit/d0be1bb04c73922018685b42b4b9195451b3cf08, except that I didn't touch getHolidaysForYearArray
From my POV, this is complete. With https://github.com/hebcal/hebcal-es6/pull/481 and https://github.com/hebcal/hebcal-leyning/pull/473, https://github.com/SLaks/tikkun.io/tree/use-modular works.
The refactor shrinks the Tikkun's index chunk from 258KB to 151KB!
Possible next steps (I probably will not do these):
calendar()
as described above (I stopped using this function entirely, as you recommended; it's probably faster without).
calendar.ts
, so that the new modular version can be imported from a nice path.@hebcal/hdate
uses (internally only) dist/esm
, whereas I used dist/es
; you may want to rename either one to be consistent.@hebcal/core
and try to use @hebcal/leyning
, since those two imports will use different .d.ts
files (so the types won't match). I did not actually try this. Since I dropped ts-imp
, I should no longer need that.Thanks for this update. So glad this is working.
I think I can safely revert https://github.com/hebcal/hebcal-es6/commit/048f73f0708af283951940a7d81a394c6db2ea58 as you're the one who originally asked for a way to append suffixes in https://github.com/hebcal/hebcal-leyning/issues/432 and if that's no longer an issue, we may as well go back to the simpler build process and avoid duplicate .d.ts
Motivation
I wrote some very simple code to run in a browser and print a full year of פרשה names:
When compiled for production using https://vitejs.dev/ with default settings, this produced 150 kB of optimized JS!
Since all functions are contained in the
HebrewCalendar
class and theEvent
hierarchy, Tree Shaking cannot see which APIs are actually used.Suggestion
It ought to be possible to build a new modular API in which application code only
import
s features that are actually used. This should allow tree shaking to entirely drop unused features (eg, emojis, zmanim calculations, unused locales, etc).This would be a new API that requires explicit
import
s to enable more calculations and methods. The existing API would remain as a wrapper to avoid breaking existing code. Applications that want to benefit from tree shaking would need to migrate to the new API to gain the benefits.Details
Making code tree-shakable involves a number of changes (these changes can be made independently for incremental improvements, but each incremental change would require clients to refactor further to get more benefit):
Note: All of these suggestions are rough ideas; I have not reviewed the code carefully enough to be sure that each of these make sense and would actually allow non-trivial amounts of code to be tree-shaken
HebrewCalendar
with separateexport
s to drop unused methods.tachanun.ts
andhallel.ts
easily.he
andhe-x-nonikud
, it would also make sense to movehe-x-nonikud
to a separate file generated at build time, so that projects that only use it can drop nikud entirely.flags
enum with an array ofimport
able objects that contain theirEvent
subclasses and the relevant parts ofgetHolidaysForYear_()
getHolidaysForYear_()
; we should try to estimate how much code this could actually save first.HebrewCalendar.calendar()
with an array ofimport
ed functions or plugins to specify which events you want returned.candleLighting
oromer
), which should be a very big win.Event
subclasses based on the plugins you pass.import
only what they need), and you can then slowly split apart the underlying code to be more modular. This allows you to make incremental improvements under the hood without forcing application code to change further.getHolidaysForYear_()
based on plugins as well for further savings.Event
and subclasses (eg,getEmoji()
; I don't know if there are any other costly members) to a top-levelexport
ed function that takes theevent
as a parameter instead.export
ed function that addsgetEmoji()
toEvent.prototype
, so that it is still available as a member function, but only if this isimport
ed.prototype
declaration, if imported).import
able installer function asasserts Event is {prototype: {getEmoji(): string}}
Let me know if you need help understanding, designing, prototyping, or implementing this.