diff --git a/src/compile/Component.ts b/src/compile/Component.ts index 905f81d60f..24e2098901 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -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); }); diff --git a/src/compile/render-ssr/index.ts b/src/compile/render-ssr/index.ts index 68e07465d7..c39ca6febe 100644 --- a/src/compile/render-ssr/index.ts +++ b/src/compile/render-ssr/index.ts @@ -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);`, diff --git a/test/runtime/samples/store-imported/_config.js b/test/runtime/samples/store-imported/_config.js new file mode 100644 index 0000000000..c2d471a329 --- /dev/null +++ b/test/runtime/samples/store-imported/_config.js @@ -0,0 +1,5 @@ +export default { + html: ` +
42
+ ` +}; \ No newline at end of file diff --git a/test/runtime/samples/store-imported/foo.js b/test/runtime/samples/store-imported/foo.js new file mode 100644 index 0000000000..340c3e6bd8 --- /dev/null +++ b/test/runtime/samples/store-imported/foo.js @@ -0,0 +1,3 @@ +import { writable } from '../../../../store.js'; + +export default writable(42); \ No newline at end of file diff --git a/test/runtime/samples/store-imported/main.svelte b/test/runtime/samples/store-imported/main.svelte new file mode 100644 index 0000000000..dc1199a83d --- /dev/null +++ b/test/runtime/samples/store-imported/main.svelte @@ -0,0 +1,6 @@ + + +{answer}
\ No newline at end of file