fix initialisation of imported stores - fixes #2108

pull/2111/head
Richard Harris 6 years ago
parent ef3f75ad7d
commit 91332c540c

@ -441,21 +441,6 @@ export default class Component {
// imports need to be hoisted out of the IIFE
removeNode(code, content.start, content.end, content.body, node);
this.imports.push(node);
node.specifiers.forEach((specifier: Node) => {
if (specifier.local.name[0] === '$') {
this.error(specifier.local, {
code: 'illegal-declaration',
message: `The $ prefix is reserved, and cannot be used for variable and import names`
});
}
this.add_var({
name: specifier.local.name,
module: is_module,
hoistable: true
});
});
}
});
}
@ -627,24 +612,12 @@ export default class Component {
});
}
if (!/Import/.test(node.type)) {
const kind = node.type === 'VariableDeclaration'
? node.kind
: node.type === 'ClassDeclaration'
? 'class'
: node.type === 'FunctionDeclaration'
? 'function'
: null;
// sanity check
if (!kind) throw new Error(`Unknown declaration type ${node.type}`);
this.add_var({
name,
initialised: instance_scope.initialised_declarations.has(name),
writable: kind === 'var' || kind === 'let'
});
}
this.add_var({
name,
initialised: instance_scope.initialised_declarations.has(name),
hoistable: /^Import/.test(node.type),
writable: node.kind === 'var' || node.kind === 'let'
});
this.node_for_declaration.set(name, node);
});

@ -27,10 +27,13 @@ export default function ssr(
const reactive_stores = component.vars.filter(variable => variable.name[0] === '$');
const reactive_store_values = reactive_stores
.map(({ name }) => {
const assignment = `${name} = @get_store_value(${name.slice(1)});`;
const store = component.var_lookup.get(name.slice(1));
if (store.hoistable) return;
const assignment = `${name} = @get_store_value(${store.name});`;
return component.compileOptions.dev
? `@validate_store(${name.slice(1)}, '${name.slice(1)}'); ${assignment}`
? `@validate_store(${store.name}, '${store.name}'); ${assignment}`
: assignment;
});
@ -109,7 +112,16 @@ export default function ssr(
return \`${renderer.code}\`;`;
const blocks = [
reactive_stores.length > 0 && `let ${reactive_stores.map(store => store.name).join(', ')};`,
reactive_stores.length > 0 && `let ${reactive_stores
.map(({ name }) => {
const store = component.var_lookup.get(name.slice(1));
if (store.hoistable) {
const get_store_value = component.helper('get_store_value');
return `${name} = ${get_store_value}(${store.name})`;
}
return name;
})
.join(', ')};`,
user_code,
parent_bindings.join('\n'),
css.code && `$$result.css.add(#css);`,

@ -0,0 +1,5 @@
export default {
html: `
<p>42</p>
`
};

@ -0,0 +1,3 @@
import { writable } from '../../../../store.js';
export default writable(42);

@ -0,0 +1,6 @@
<script>
import foo from './foo.js';
const answer = $foo;
</script>
<p>{answer}</p>
Loading…
Cancel
Save