Created
January 15, 2018 23:27
-
-
Save jamesbirtles/74299ff2166eb5f7c8b7c68e58e4bc6d to your computer and use it in GitHub Desktop.
SSR Nested Store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello {{$name}} | |
<script> | |
import { Store } from 'svelte/store.js'; | |
const store = new Store({ name: 'James' }); | |
export default { | |
store: () => store, | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var __import0 = require("svelte/store.js"); | |
var Store = __import0.Store; | |
const store = new Store({ name: 'James' }); | |
function store_1() { | |
return store; | |
} | |
var Nested = {}; | |
Nested.filename = "Nested.html"; | |
Nested.data = function() { | |
return {}; | |
}; | |
Nested.render = function(state, options = {}) { | |
var components = new Set(); | |
function addComponent(component) { | |
components.add(component); | |
} | |
var result = { head: '', addComponent }; | |
options.store = store_1(); | |
var html = Nested._render(result, state, options); | |
var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\n'); | |
return { | |
html, | |
head: result.head, | |
css: { code: cssCode, map: null }, | |
toString() { | |
return html; | |
} | |
}; | |
} | |
Nested._render = function(__result, state, options) { | |
__result.addComponent(Nested); | |
state = Object.assign(options.store._init(["name"]), state); | |
return `Hello ${__escape(state.$name)}`; | |
}; | |
Nested.css = { | |
code: '', | |
map: null | |
}; | |
var warned = false; | |
Nested.renderCss = function() { | |
if (!warned) { | |
console.error('Component.renderCss(...) is deprecated and will be removed in v2 — use Component.render(...).css instead'); | |
warned = true; | |
} | |
var components = []; | |
return { | |
css: components.map(x => x.css).join('\n'), | |
map: null, | |
components | |
}; | |
}; | |
var escaped = { | |
'"': '"', | |
"'": ''', | |
'&': '&', | |
'<': '<', | |
'>': '>' | |
}; | |
function __escape(html) { | |
return String(html).replace(/["'&<>]/g, match => escaped[match]); | |
} | |
module.exports = Nested; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Nested /> | |
<script> | |
import { Nested } from './Nested'; | |
export default { | |
components: { | |
Nested, | |
}, | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var __import0 = require("./Nested"); | |
var Nested = __import0.Nested; | |
var Parent = {}; | |
Parent.filename = "Parent.html"; | |
Parent.data = function() { | |
return {}; | |
}; | |
Parent.render = function(state, options = {}) { | |
var components = new Set(); | |
function addComponent(component) { | |
components.add(component); | |
} | |
var result = { head: '', addComponent }; | |
var html = Parent._render(result, state, options); | |
var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\n'); | |
return { | |
html, | |
head: result.head, | |
css: { code: cssCode, map: null }, | |
toString() { | |
return html; | |
} | |
}; | |
} | |
Parent._render = function(__result, state, options) { | |
__result.addComponent(Parent); | |
state = Object.assign({}, state); | |
return `${Nested._render(__result, {}, { store: options.store })}`; | |
}; | |
Parent.css = { | |
code: '', | |
map: null | |
}; | |
var warned = false; | |
Parent.renderCss = function() { | |
if (!warned) { | |
console.error('Component.renderCss(...) is deprecated and will be removed in v2 — use Component.render(...).css instead'); | |
warned = true; | |
} | |
var components = []; | |
var seen = {}; | |
function addComponent(component) { | |
var result = component.renderCss(); | |
result.components.forEach(x => { | |
if (seen[x.filename]) return; | |
seen[x.filename] = true; | |
components.push(x); | |
}); | |
} | |
addComponent(Nested); | |
return { | |
css: components.map(x => x.css).join('\n'), | |
map: null, | |
components | |
}; | |
}; | |
module.exports = Parent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment