lwc icon indicating copy to clipboard operation
lwc copied to clipboard

Reduce verbosity in `template.stylesheets` generated code

Open nolanlawson opened this issue 4 months ago • 0 comments

Currently our generated code for templates looks like this:

tmpl.stylesheets = [];
if (_implicitStylesheets) {
  tmpl.stylesheets.push.apply(tmpl.stylesheets, _implicitStylesheets);
}
if (_implicitScopedStylesheets) {
  tmpl.stylesheets.push.apply(tmpl.stylesheets, _implicitScopedStylesheets);
}

This is a lot of boilerplate. Now that all our supported browsers support rest/spread, we can probably do something like this instead:

tmpl.stylesheets = [
  ...(_implicitStylesheets ?? EmptyArray),
  ...(_implicitScopedStylesheets ?? EmptyArray)
];

(where EmptyArray is some shared constant that resolves to [] – to avoid excessive GC).

This might have some mild perf and compat considerations, so this would need to be done carefully.

nolanlawson avatar Apr 15 '24 21:04 nolanlawson