Lint and format src/compiler/compile/render_ssr/index.js

pull/8569/head
Simon Holthausen 3 years ago
parent 35735ea403
commit 56e9dd7d2c

@ -17,9 +17,15 @@ export default function ssr(component, options) {
}); });
const { name } = component; const { name } = component;
// create $$render function // create $$render function
renderer.render(trim(component.fragment.children), Object.assign({ renderer.render(
trim(component.fragment.children),
Object.assign(
{
locate: component.locate locate: component.locate
}, options)); },
options
)
);
// TODO put this inside the Renderer class // TODO put this inside the Renderer class
const literal = renderer.pop(); const literal = renderer.pop();
// TODO concatenate CSS maps // TODO concatenate CSS maps
@ -35,7 +41,9 @@ export default function ssr(component, options) {
: null; : null;
const uses_slots = component.var_lookup.has('$$slots'); const uses_slots = component.var_lookup.has('$$slots');
const slots = uses_slots ? b`let $$slots = @compute_slots(#slots);` : null; const slots = uses_slots ? b`let $$slots = @compute_slots(#slots);` : null;
const reactive_stores = component.vars.filter((variable) => variable.name[0] === '$' && variable.name[1] !== '$'); const reactive_stores = component.vars.filter(
(variable) => variable.name[0] === '$' && variable.name[1] !== '$'
);
const reactive_store_subscriptions = reactive_stores const reactive_store_subscriptions = reactive_stores
.filter((store) => { .filter((store) => {
const variable = component.var_lookup.get(store.name.slice(1)); const variable = component.var_lookup.get(store.name.slice(1));
@ -48,7 +56,9 @@ export default function ssr(component, options) {
${`$$unsubscribe_${store_name}`} = @subscribe(${store_name}, #value => ${name} = #value) ${`$$unsubscribe_${store_name}`} = @subscribe(${store_name}, #value => ${name} = #value)
`; `;
}); });
const reactive_store_unsubscriptions = reactive_stores.map(({ name }) => b `${`$$unsubscribe_${name.slice(1)}`}()`); const reactive_store_unsubscriptions = reactive_stores.map(
({ name }) => b`${`$$unsubscribe_${name.slice(1)}`}()`
);
const reactive_store_declarations = reactive_stores.map(({ name }) => { const reactive_store_declarations = reactive_stores.map(({ name }) => {
const store_name = name.slice(1); const store_name = name.slice(1);
const store = component.var_lookup.get(store_name); const store = component.var_lookup.get(store_name);
@ -79,16 +89,20 @@ export default function ssr(component, options) {
const to_invalidate = new Set(); const to_invalidate = new Set();
for (const name of names) { for (const name of names) {
const variable = component.var_lookup.get(name); const variable = component.var_lookup.get(name);
if (variable && if (
variable &&
!variable.hoistable && !variable.hoistable &&
!variable.global && !variable.global &&
!variable.module && !variable.module &&
(variable.subscribable || variable.name[0] === '$')) { (variable.subscribable || variable.name[0] === '$')
) {
to_invalidate.add(variable.name); to_invalidate.add(variable.name);
} }
} }
if (to_invalidate.size) { if (to_invalidate.size) {
this.replace(invalidate(/** @type {any} */ ({ component }), scope, node, to_invalidate, true)); this.replace(
invalidate(/** @type {any} */ ({ component }), scope, node, to_invalidate, true)
);
} }
} }
} }
@ -118,7 +132,7 @@ export default function ssr(component, options) {
return variable.injected; return variable.injected;
}); });
const reactive_declarations = component.reactive_declarations.map((d) => { const reactive_declarations = component.reactive_declarations.map((d) => {
const body = ( /** @type {import('estree').LabeledStatement} */(d.node)).body; const body = /** @type {import('estree').LabeledStatement} */ (d.node).body;
let statement = b`${body}`; let statement = b`${body}`;
if (!d.declaration) { if (!d.declaration) {
// TODO do not add label if it's not referenced // TODO do not add label if it's not referenced
@ -162,13 +176,15 @@ export default function ssr(component, options) {
].filter(Boolean); ].filter(Boolean);
const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css'); const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css');
const js = b` const js = b`
${css.code ${
css.code
? b` ? b`
const #css = { const #css = {
code: "${css.code}", code: "${css.code}",
map: ${css_sourcemap_enabled && css.map ? string_literal(css.map.toString()) : 'null'} map: ${css_sourcemap_enabled && css.map ? string_literal(css.map.toString()) : 'null'}
};` };`
: null} : null
}
${component.extract_javascript(component.ast.module)} ${component.extract_javascript(component.ast.module)}
@ -186,24 +202,16 @@ function trim(nodes) {
let start = 0; let start = 0;
for (; start < nodes.length; start += 1) { for (; start < nodes.length; start += 1) {
const node = /** @type {import('../nodes/Text.js').default} */ (nodes[start]); const node = /** @type {import('../nodes/Text.js').default} */ (nodes[start]);
if (node.type !== 'Text') if (node.type !== 'Text') break;
break;
node.data = node.data.replace(/^\s+/, ''); node.data = node.data.replace(/^\s+/, '');
if (node.data) if (node.data) break;
break;
} }
let end = nodes.length; let end = nodes.length;
for (; end > start; end -= 1) { for (; end > start; end -= 1) {
const node = /** @type {import('../nodes/Text.js').default} */ (nodes[end - 1]); const node = /** @type {import('../nodes/Text.js').default} */ (nodes[end - 1]);
if (node.type !== 'Text') if (node.type !== 'Text') break;
break;
node.data = node.data.trimRight(); node.data = node.data.trimRight();
if (node.data) if (node.data) break;
break;
} }
return nodes.slice(start, end); return nodes.slice(start, end);
} }

Loading…
Cancel
Save