Unnecessary code in bundles
Loskir opened this issue · 5 comments
Due to the lack of PURE annotations, extra code is included in bundles. Example: var tokens = new Map(...)
is included and immediately thrown away (tokens
var is not used, but the bundler is not sure if new Map
can be safely omitted).
As I suppose tokens
is indeed pure, this issue can be fixed by adding a few /* @__PURE__ */
annotations like this:
Line 89 in 28a9539
export const tokens = /* @__PURE__ */ new Map(
/* @__PURE__ */ [...clockAgnostic, ...clock24, ...clock12].map((format) => {
return [format[0], format]
}),
)
More info:
Bundler — vite 5.1.4
I also experience this with Vite not being able to treeshake tempo.
currently I use format
& parse
in a vue component but when I check the compiled source I'm also able to find other tempo functions in it
@WilcoSp can you identify if those functions are being used in format/parse? In general everything should be able to tree shake pretty well, perhaps with the exception of this issue (Map) which we can annotate. format/parse are the largest functions in the library as you might imagine, and they do use some of the functions in the library.
with esBuilds 'keepNames' settings I was able to identify the following functions being bundled in:
-
iso8601 (date)
-
normalize (date)
-
date (format/parse/offset)
-
monthEnd (monthDays)
-
monthDays (parse)
-
fixedLengthByOffset (parse)
-
normStr (ap)
-
fill (format)
-
createPartMap (fill)
-
minsToOffset (offset)
-
offsetToMins (applyOffset)
-
validOffset (parse)
-
escapeTokens (formatStr)
-
isNumeric (validate)
-
validate (parse)
-
ap (parse, range)
-
applyOffset (applyOffset)
-
deviceTZ (format)
-
relativeTime (offset)
-
offset (format)
-
parts (format/parse)
-
styleParts (parts)
-
guessPattern (styleParts)
-
partStyle (guessPattern)
-
removeOffset (format)
-
deviceLocale (format)
-
formatStr (parse)
-
fourDigitYear (parse)
-
range (parse)
-
parseParts (parse)
-
format (by range)
bold are directly imported by parse/format
italic are used by imported functions of parse/format
between () is by which function(s) it gets used
after doing this it does seem everything gets used when using parse/format. but I do wonder how the bundle size does get around the +-12kb (not gzipped), +-5kb gzipped.
sorry about what I had said about not being tree shakable, but what might had happened yesterday is that vite wasn't able to split tempo when using "addSecond" at a file that gets bundled into index.js, while for example date-fns was able to be split by vite
tomorrow I'll make an example vue project to replicate the splitting issue
I've created the repo to test splitting of Tempo with Vite and documented what I've observed into the readme
@justin-schroeder this may help to get Tempo into smaller bundles with at least Vite