Skip to content

Unnecessary code in bundles #37

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

Closed
Loskir opened this issue Mar 8, 2024 · 5 comments
Closed

Unnecessary code in bundles #37

Loskir opened this issue Mar 8, 2024 · 5 comments

Comments

@Loskir
Copy link

Loskir commented Mar 8, 2024

image image

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:

export const tokens = new Map(

export const tokens = /* @__PURE__ */ new Map(
  /* @__PURE__ */ [...clockAgnostic, ...clock24, ...clock12].map((format) => {
    return [format[0], format]
  }),
)

More info:
Bundler — vite 5.1.4

@WilcoSp
Copy link
Contributor

WilcoSp commented Mar 26, 2024

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

@justin-schroeder
Copy link
Member

@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.

@WilcoSp
Copy link
Contributor

WilcoSp commented Mar 26, 2024

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

@WilcoSp
Copy link
Contributor

WilcoSp commented Mar 26, 2024

tomorrow I'll make an example vue project to replicate the splitting issue

@WilcoSp
Copy link
Contributor

WilcoSp commented Mar 27, 2024

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

link: https://github.com/WilcoSp/tempo-split-test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants