amzn / style-dictionary

A build system for creating cross-platform styles.
https://styledictionary.com
Apache License 2.0
3.87k stars 543 forks source link

Drop Lodash #1041

Closed jorenbroekema closed 5 months ago

jorenbroekema commented 11 months ago

@tahul

Mostly wanna drop this because Lodash is quite a node_modules bloating thing, and usually modern JS can do it already anyway. We don't use that much of lodash anyway, shouldn't be too much work.

lb- commented 10 months ago

By the looks of things, it's only used for the template code & examples.

I assume these need to still be supported, what would be the best approach here?

Maybe the template package to be used standalone https://www.npmjs.com/package/lodash.template (however, I think Lodash still needs to make utils available inside the function like forEach).

jorenbroekema commented 10 months ago

With regards to template, I think it would be good to just take the templates and turn them into template literals (function that returns template literal if dynamic vars are needed). ES6 has a good method for templating nowadays so then we don't need some third-party templating lib like lodash for it anymore.

Before:

export default `<?xml version="1.0" encoding="UTF-8"?>
<%
var props = dictionary.allTokens.filter(function(prop) {
  return prop.attributes.category === 'color';
});
%>
<%= fileHeader({file, commentStyle: 'xml'}) %>
<resources>
  <% props.forEach(function(prop) {
  %><color name="<%= prop.name %>"><%= prop.value %></color><% if (prop.comment) { %><!-- <%= prop.comment %> --><% } %>
  <% }); %>
</resources>`;

After:

export default (props, fileHeader) => `<?xml version="1.0" encoding="UTF-8"?>
${fileHeader}
<resources>
  ${props.reduce(
    (acc, prop) =>
      `${acc}  <color name="${prop.name}">${prop.value}</color>${
        prop.comment ? `<!-- ${prop.comment} -->` : ''
      }\n`,
    '',
  )}
</resources>`;