Skip to content

Commit

Permalink
Merge pull request #54 from nicketson/revert_state
Browse files Browse the repository at this point in the history
Partially revert 86c9ff1 Add static template hoisting to SSR
  • Loading branch information
ryansolid committed Feb 26, 2021
2 parents bee6515 + 8c3190a commit 67e27e1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/babel-plugin-jsx-dom-expressions/src/dom/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ function transformAttributes(path, results) {
) {
// can only hydrate delegated events
hasHydratableEvent = true;
const events = path.state.events;
const events =
attribute.scope.getProgramParent().data.events ||
(attribute.scope.getProgramParent().data.events = new Set());
events.add(ev);
let handler = value.expression;
const resolveable = detectResolvableEventHandler(attribute, handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ function registerTemplate(path, results) {
const { hydratable } = config;
let decl;
if (results.template.length) {
const templates = path.state.templates;
const templates =
path.scope.getProgramParent().data.templates ||
(path.scope.getProgramParent().data.templates = []);
let templateDef, templateId;
if ((templateDef = templates.find(t => t.template === results.template))) {
templateId = templateDef.id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import config from "../config";

// add to the top/bottom of the module.
export default path => {
if (path.state.events.size) {
if (path.scope.data.events) {
registerImportMethod(path, "delegateEvents");
path.node.body.push(
t.expressionStatement(
t.callExpression(t.identifier("_$delegateEvents"), [
t.arrayExpression(Array.from(path.state.events).map(e => t.stringLiteral(e)))
t.arrayExpression(Array.from(path.scope.data.events).map(e => t.stringLiteral(e)))
])
)
);
}
if (path.state.templates.length) {
if (path.scope.data.templates) {
const appendTemplates = config.generate === "ssr" ? appendTemplatesSSR : appendTemplatesDOM;
appendTemplates(path, path.state.templates);
appendTemplates(path, path.scope.data.templates);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ export default path => {
}
path.skip();
}
path.state = {
templates: [],
events: new Set()
}
};
7 changes: 5 additions & 2 deletions packages/babel-plugin-jsx-dom-expressions/src/ssr/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export function createTemplate(path, result) {
template = t.arrayExpression(strings);
}

const found = path.state.templates.find(tmp => {
const templates =
path.scope.getProgramParent().data.templates ||
(path.scope.getProgramParent().data.templates = []);
const found = templates.find(tmp => {
if (t.isArrayExpression(tmp[1]) && t.isArrayExpression(template)) {
return tmp[1].elements.every(
(el, i) => template.elements[i] && el.value === template.elements[i].value
Expand All @@ -28,7 +31,7 @@ export function createTemplate(path, result) {
});
if (!found) {
id = path.scope.generateUidIdentifier("tmpl$");
path.state.templates.push([id, template]);
templates.push([id, template]);
} else id = found[0];

return t.callExpression(
Expand Down

0 comments on commit 67e27e1

Please sign in to comment.