From 643688650035f84117740929a103de50e3e200d1 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 20 Sep 2017 11:51:01 -0400 Subject: [PATCH] exclude irrelevant stuff from SSR output --- src/generators/Generator.ts | 15 ++++---- src/generators/dom/index.ts | 2 +- src/generators/server-side-rendering/index.ts | 32 +--------------- .../js/samples/ssr-no-oncreate-etc/_config.js | 5 +++ .../ssr-no-oncreate-etc/expected-bundle.js | 23 ++++++++++++ .../samples/ssr-no-oncreate-etc/expected.js | 37 +++++++++++++++++++ .../js/samples/ssr-no-oncreate-etc/input.html | 23 ++++++++++++ 7 files changed, 98 insertions(+), 39 deletions(-) create mode 100644 test/js/samples/ssr-no-oncreate-etc/_config.js create mode 100644 test/js/samples/ssr-no-oncreate-etc/expected-bundle.js create mode 100644 test/js/samples/ssr-no-oncreate-etc/expected.js create mode 100644 test/js/samples/ssr-no-oncreate-etc/input.html diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 1255773897..5b7ec8f071 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -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); } diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index e9fe9805c7..cc7997455d 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -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(); diff --git a/src/generators/server-side-rendering/index.ts b/src/generators/server-side-rendering/index.ts index 8971110d57..5e6bd25d8d 100644 --- a/src/generators/server-side-rendering/index.ts +++ b/src/generators/server-side-rendering/index.ts @@ -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) { diff --git a/test/js/samples/ssr-no-oncreate-etc/_config.js b/test/js/samples/ssr-no-oncreate-etc/_config.js new file mode 100644 index 0000000000..803712adec --- /dev/null +++ b/test/js/samples/ssr-no-oncreate-etc/_config.js @@ -0,0 +1,5 @@ +export default { + options: { + generate: 'ssr' + } +}; \ No newline at end of file diff --git a/test/js/samples/ssr-no-oncreate-etc/expected-bundle.js b/test/js/samples/ssr-no-oncreate-etc/expected-bundle.js new file mode 100644 index 0000000000..6ea9ece9d4 --- /dev/null +++ b/test/js/samples/ssr-no-oncreate-etc/expected-bundle.js @@ -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; diff --git a/test/js/samples/ssr-no-oncreate-etc/expected.js b/test/js/samples/ssr-no-oncreate-etc/expected.js new file mode 100644 index 0000000000..6de85e548c --- /dev/null +++ b/test/js/samples/ssr-no-oncreate-etc/expected.js @@ -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 = { + '"': '"', + "'": ''', + '&': '&', + '<': '<', + '>': '>' +}; + +function __escape(html) { + return String(html).replace(/["'&<>]/g, match => escaped[match]); +} + +module.exports = SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/ssr-no-oncreate-etc/input.html b/test/js/samples/ssr-no-oncreate-etc/input.html new file mode 100644 index 0000000000..f8c26b5825 --- /dev/null +++ b/test/js/samples/ssr-no-oncreate-etc/input.html @@ -0,0 +1,23 @@ + \ No newline at end of file