wix-incubator / react-templates

Light weight templates for react
https://wix.github.io/react-templates
MIT License
2.82k stars 207 forks source link

helper functions as external dependency module #184

Open nippur72 opened 8 years ago

nippur72 commented 8 years ago

currently react-templates relies on lodash for doing some core functions (looping etc..) but such "helper" functions could be loaded as external dependency module instead of being "embedded" in the render function.

If so, it would be possible to fine tune and possibly avoid the direct dependency from lodash.

In PR #176 I've isolated the 4 "helper functions" actually used by react-templates:

__rtmap(collection, function);  // loops over a collection
__rtclass(object);              // turns a map object into a HTML class-attribute
__rtassign(dest,...objs)        // assign properties to destination object
__rtmergeprops(dest,...objs)    // as above, but considering "style" and "className"

They can be "enabled" with the new CLI option --external-helpers modulename.

I've also written a plain vanilla helper-functions module called react-templates-helpers that falls back on lodash.

As a bonus, since the helper functions are loaded separately one by one, in future it will be possible to take advantage of ES6 tree shaking features (rollupjs, webpack etc...).

Another small advantage is that now lodash is loaded only when needed, that is, if the component is small enough and doesn't use any of the helpers, lodash is left out because not needed.