react-iconpack icon indicating copy to clipboard operation
react-iconpack copied to clipboard

Call for Input re: caching

Open ryanmcgrath opened this issue 9 years ago • 0 comments

This approach is pretty much 90% of the way there, but that last naggling 10% is the caching integration with regards to Webpack/Babel/etc. I'd love to hear input from others on this. Insofar as my understanding goes:

  • babel-loader's cacheDirectory option essentially stores the outputted code into a gzip'd temp file. When re-using the cache it won't re-run babel transformers, which is the big crux of the problem here. I've poured over documentation and such but I'm unsure as to whether there's a way to force a transform to always run (I'm guessing there's not, as it'd make little sense). For reference, this is all re: Babel 6.
  • Webpack's cache: true config parameter actually half-works. It winds up accruing SVGs to inject, and when one is removed from the source code it's not actually evicted from the cache. At the moment I can't tell whether this is a bug with Webpack or not, because deleted entries are still winding up in the source code. Documentation on Webpack's internals is admittedly sketchy at best.
  • I believe watchify has the same issue as Webpack's cache, but I've not delved into it as of yet. I don't use Browserify these days but I know enough people/teams that do and would like to continue supporting it, so should probably determine the full scope of it here at some point.

A quick rundown of how the project works (and I should put this on the wiki at some point) is as follows:

  • The Babel transform runs on compile, basically accruing unique <Icon ... /> calls. It doesn't actually transform anything because it doesn't need to. This is stored in a shared packer variable, basically relying on require() semantics to use a global store - each call to require a module returns the cached output. Yes, I'm aware globals are frowned upon, but I find this works A-OK here and reduces the need for any extra 3rd party library integrations for simple storage.
  • Depending on whether the user is a Browserify or Webpack setup, they get a specific plugin - the configuration of each is pretty straightforward and shown in the gulpfile.js and webpack.config.js examples.
  • index.js is a bit of magic, but it's liberally commented and understandable. Depending on how the module is imported the return value changes - if 'browserify' or 'webpack' is passed to the require() returned function, it will return the corresponding plugin. If it's loaded by babel as a transform, it will return the necessary Babel transformer. If none of the above is happening, it returns a Webpack loader, which is happening because Webpack's internals are nigh undecipherable at a glance and it's a cheap way to inject the icon module source code. This module is shoved in during the compilation process via the Webpack plugin.

Just figured having an open issue where anyone can comment or throw input up would be helpful. Feel free to dump questions here as well.

ryanmcgrath avatar Mar 21 '16 12:03 ryanmcgrath