Skip to content

Reorganize the Babel plugin, add support for non-literal template variables - #44

Merged
developit merged 5 commits into
developit:babel-plugin-fix-reduxfrom
jviide:babel-plugin-fix-redux
Dec 19, 2018
Merged

developit merged 5 commits into
developit:babel-plugin-fix-reduxfrom
jviide:babel-plugin-fix-redux

Conversation

@jviide

@jviide jviide commented Dec 19, 2018 •

Copy link
Copy Markdown
Collaborator

This pull request reorganizes the Babel plugin and adds tests, with the following steps:

  1. In treeify the template is evaluated and converted to a object tree with h(tag, props, ...children) { return { tag, props, children }; }. h is intentionally as simple as possible, because there's a not-so-pretty trick: Object.assign is temporarily replaced with our own function that doesn't smush its arguments together, but just returns them as an array. This allows us to simplify props handling and do some optimizations to the output.
  2. Return Object.assign back to the value it was before evaluating the template.
  3. The resulting tree is transformed recursively, top-down in transform.

In transform the props argument is now either an array (from the Object.assign trick), null or an object. If it's an array then it's further processed: sometimes we can get rid of the spread altogether (e.g. Object.assign(foo) can just become foo). This allows us also to support non-literal template variables in spreads, e.g. <a ...${foo}> now gets transformed to 'h("a",foo,[])`.

When we actually need a spread output, like in <a b=1 ...${foo}>, the output follows (somewhat) what @babel/preset-react does. There's now a new setting useBuiltIns that is false by default that has the same meaning as in @babel/preset-react. When the flag is true the output for <a b=1 ...${foo}> is h("a",Object.assign({b:"1"},foo),[]), and if the flag is false then Babel's extends-helper is included in the output.

childMapper is now recursive and a bit simpler, as htm itself does JSX-style whitespace normalization.

@jviide

jviide commented Dec 19, 2018 •

Copy link
Copy Markdown
Collaborator Author

Modified htm to generate h("a", Object.assign({ b: "1" }, foo)) for <a b="1" ...${foo} /> instead of h("a", Object.assign({}, { b: "1" }, foo)). This allows simplifying the Babel plugin by removing most of the object flattening code.

For <a ...${foo} b="1" /> the output is still h("a", Object.assign({}, foo, { b: "1" })).

@developit
developit self-requested a review December 19, 2018 17:16
@developit

Copy link
Copy Markdown
Owner

I'm going to see if I can run the non-babel tests against the babel version.

@developit
developit merged commit 129e793 into developit:babel-plugin-fix-redux Dec 19, 2018
@jviide
jviide deleted the babel-plugin-fix-redux branch December 19, 2018 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants