mike-north / ember-lodash

Lodash as ES6 Modules for Ember.js Apps
MIT License
75 stars 34 forks source link

Ember helpers? #82

Open SaladFork opened 7 years ago

SaladFork commented 7 years ago

I've found that in many of my projects that I use ember-lodash, I end up writing Ember helpers that mostly just call a Lodash function and pass along arguments. For example:

// app/helpers/lodash-start-case.js

import Ember from 'ember';
import _string from 'lodash/string';

export function lodashStartCase([string]) {
    return _string.startCase(string);
}

export default Ember.Helper.helper(lodashStartCase);

This allows me to take advantage of Lodash transformations in my template (without having to make a CP):

<h1>{{lodash-start-case title}}</h1>

Would there be interest in a PR that adds some of these helpers to the addon?

tchak commented 7 years ago

How would you select which ones you expose? Until we have tree shaking in ember-cli I would be against this addition.

MartinMalinda commented 7 years ago

Ember composable helpers have a feature where you can select which helpers to expose

https://github.com/DockYard/ember-composable-helpers/blob/master/index.js

olsonpm commented 7 years ago

@tchak - I apologize for deviating from the subject, but what are you trying to gain from tree-shaking with ember? As I see it,

  1. Tree shaking is very brittle at the moment and doesn't work with lodash which is why they recommend a babel plugin to transform
// from
import { map, invoke } from 'lodash';

// to
import map from 'lodash/map';
import invoke from 'lodash/invoke';

2) The code required for an ember app is already very large and would likely negate any size-reduction via tree-shaking.

MartinMalinda commented 7 years ago

@olsonpm some tree shaking can already be done via ember-browserify and broccoli-rollup. For example using ember-date-fns instead of ember-moment can save you quite a lot of code and it is a significant decrease in filesize.

glimmer-application-pipeline also uses rollup underneath and importing 3rd party modules is very effective

olsonpm commented 7 years ago

@MartinMalinda - Tree shaking as a whole does not work well. It can't tree shake lodash-es for many reasons. Here's an extremely simple example of tree-shaking failing.

This problem is not specific to rollup - it's that tree-shaking is difficult. Rich harris was working on a a different method to determine whether code causes side-effects, but the last commit of that branch was in january so I assume that work was deemed not time-effective.

MartinMalinda commented 7 years ago

It could be responsibility of each addon how to handle tree shaking - whether to tree shake 3rd party lib code, whether to tree shake ember modules that being exported.

If there 100 helpers the addon is exporting, it is quite straightforward to check which are used and which not in the parent app. They are generally pure functions without side effects.

I agree that having one global switch for tree shaking is probably not a good idea.

olsonpm commented 7 years ago

I see - I confused your mentioning of "tree shaking" to mean one provided by a bundler. Removing code using heuristics specific to ember is very reasonable.