0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

exclude irrelevant stuff from SSR output

This commit is contained in:
Rich Harris 2017-09-20 11:51:01 -04:00
parent 7f39b5be16
commit 6436886500
7 changed files with 98 additions and 39 deletions

View File

@ -116,7 +116,8 @@ export default class Generator {
source: string,
name: string,
stylesheet: Stylesheet,
options: CompileOptions
options: CompileOptions,
dom: boolean
) {
this.ast = clone(parsed);
@ -154,7 +155,7 @@ export default class Generator {
this.aliases = new Map();
this.usedNames = new Set();
this.parseJs();
this.parseJs(dom);
this.name = this.alias(name);
if (options.customElement === true) {
@ -452,7 +453,7 @@ export default class Generator {
};
}
parseJs() {
parseJs(dom: boolean) {
const { code, source } = this;
const { js } = this.parsed;
@ -613,7 +614,7 @@ export default class Generator {
addDeclaration('data', templateProperties.data.value);
}
if (templateProperties.events) {
if (templateProperties.events && dom) {
templateProperties.events.value.properties.forEach((property: Node) => {
addDeclaration(property.key.name, property.value, 'events');
});
@ -625,7 +626,7 @@ export default class Generator {
});
}
if (templateProperties.methods) {
if (templateProperties.methods && dom) {
addDeclaration('methods', templateProperties.methods.value);
}
@ -635,12 +636,12 @@ export default class Generator {
}
if (templateProperties.onrender) templateProperties.oncreate = templateProperties.onrender; // remove after v2
if (templateProperties.oncreate) {
if (templateProperties.oncreate && dom) {
addDeclaration('oncreate', templateProperties.oncreate.value);
}
if (templateProperties.onteardown) templateProperties.ondestroy = templateProperties.onteardown; // remove after v2
if (templateProperties.ondestroy) {
if (templateProperties.ondestroy && dom) {
addDeclaration('ondestroy', templateProperties.ondestroy.value);
}

View File

@ -39,7 +39,7 @@ export class DomGenerator extends Generator {
stylesheet: Stylesheet,
options: CompileOptions
) {
super(parsed, source, name, stylesheet, options);
super(parsed, source, name, stylesheet, options, true);
this.blocks = [];
this.readonly = new Set();

View File

@ -21,44 +21,14 @@ export class SsrGenerator extends Generator {
stylesheet: Stylesheet,
options: CompileOptions
) {
super(parsed, source, name, stylesheet, options);
super(parsed, source, name, stylesheet, options, false);
this.bindings = [];
this.renderCode = '';
this.appendTargets = [];
// in an SSR context, we don't need to include events, methods, oncreate or ondestroy
const { templateProperties, defaultExport } = this;
preprocess(this, parsed.html);
this.stylesheet.warnOnUnusedSelectors(options.onwarn);
// TODO how to exclude non-SSR-able stuff?
// if (templateProperties.oncreate)
// removeNode(
// this.code,
// defaultExport.declaration,
// templateProperties.oncreate
// );
// if (templateProperties.ondestroy)
// removeNode(
// this.code,
// defaultExport.declaration,
// templateProperties.ondestroy
// );
// if (templateProperties.methods)
// removeNode(
// this.code,
// defaultExport.declaration,
// templateProperties.methods
// );
// if (templateProperties.events)
// removeNode(
// this.code,
// defaultExport.declaration,
// templateProperties.events
// );
}
append(code: string) {

View File

@ -0,0 +1,5 @@
export default {
options: {
generate: 'ssr'
}
};

View File

@ -0,0 +1,23 @@
var SvelteComponent = {};
SvelteComponent.data = function() {
return {};
};
SvelteComponent.render = function(state, options) {
state = state || {};
return ``.trim();
};
SvelteComponent.renderCss = function() {
var components = [];
return {
css: components.map(x => x.css).join('\n'),
map: null,
components
};
};
module.exports = SvelteComponent;

View File

@ -0,0 +1,37 @@
"use strict";
var SvelteComponent = {};;
SvelteComponent.data = function() {
return {};
};
SvelteComponent.render = function(state, options) {
state = state || {};
return ``.trim();
};
SvelteComponent.renderCss = function() {
var components = [];
return {
css: components.map(x => x.css).join('\n'),
map: null,
components
};
};
var escaped = {
'"': '"',
"'": ''',
'&': '&',
'<': '&lt;',
'>': '&gt;'
};
function __escape(html) {
return String(html).replace(/["'&<>]/g, match => escaped[match]);
}
module.exports = SvelteComponent;

View File

@ -0,0 +1,23 @@
<script>
export default {
oncreate() {
console.log('oncreate');
},
ondestroy() {
console.log('ondestroy');
},
methods: {
foo() {
console.log('foo');
}
},
events: {
swipe(node, callback) {
// TODO implement
}
}
};
</script>