From 3c61655e93ac12455ffd162c3d14169e7eadd5fd Mon Sep 17 00:00:00 2001 From: Alan Palazzolo Date: Tue, 20 Mar 2018 13:45:47 -0500 Subject: [PATCH] Add option preserve comments in SSR rendering. --- README.md | 1 + .../server-side-rendering/visitors/Comment.ts | 11 +++- src/interfaces.ts | 4 ++ .../samples/ssr-preserve-comments/_config.js | 6 ++ .../ssr-preserve-comments/expected-bundle.js | 59 ++++++++++++++++++ .../samples/ssr-preserve-comments/expected.js | 62 +++++++++++++++++++ .../samples/ssr-preserve-comments/input.html | 3 + 7 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 test/js/samples/ssr-preserve-comments/_config.js create mode 100644 test/js/samples/ssr-preserve-comments/expected-bundle.js create mode 100644 test/js/samples/ssr-preserve-comments/expected.js create mode 100644 test/js/samples/ssr-preserve-comments/input.html diff --git a/README.md b/README.md index 95b352e16a..6a9c5455f5 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ The Svelte compiler optionally takes a second argument, an object of configurati | `filename` | `string` | The filename to use in sourcemaps and compiler error and warning messages. | `'SvelteComponent.html'` | | `amd`.`id` | `string` | The AMD module ID to use for the `'amd'` and `'umd'` output formats. | `undefined` | | `globals` | `object`, `function` | When outputting to the `'umd'`, `'iife'` or `'eval'` formats, an object or function mapping the names of imported dependencies to the names of global variables. | `{}` | +| `preserveComments` | `boolean` | Include comments in rendering. Currently, only applies to SSR rendering | `false` | | | | | | `onerror` | `function` | Specify a callback for when Svelte encounters an error while compiling the component. Passed two arguments: the error object, and another function that is Svelte's default onerror handling. | (exception is thrown) | | `onwarn` | `function` | Specify a callback for when Svelte encounters a non-fatal warning while compiling the component. Passed two arguments: the warning object, and another function that is Svelte's default onwarn handling. | (warning is logged to console) | diff --git a/src/generators/server-side-rendering/visitors/Comment.ts b/src/generators/server-side-rendering/visitors/Comment.ts index ad10d20aa5..84d5c0fa7f 100644 --- a/src/generators/server-side-rendering/visitors/Comment.ts +++ b/src/generators/server-side-rendering/visitors/Comment.ts @@ -1,3 +1,10 @@ -export default function visitComment() { - // do nothing +export default function visitComment( + generator: SsrGenerator, + block: Block, + node: Node +) { + // Allow option to preserve comments, otherwise ignore + if (generator && generator.options && generator.options.preserveComments) { + generator.append(``); + } } diff --git a/src/interfaces.ts b/src/interfaces.ts index de187da71d..36c0ff2863 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -62,6 +62,10 @@ export interface CompileOptions { css?: boolean; store?: boolean; + ssr?: { + preserveComments?: boolean | false; + }; + onerror?: (error: Error) => void; onwarn?: (warning: Warning) => void; } diff --git a/test/js/samples/ssr-preserve-comments/_config.js b/test/js/samples/ssr-preserve-comments/_config.js new file mode 100644 index 0000000000..9e62c8d192 --- /dev/null +++ b/test/js/samples/ssr-preserve-comments/_config.js @@ -0,0 +1,6 @@ +export default { + options: { + generate: 'ssr', + preserveComments: true + } +}; diff --git a/test/js/samples/ssr-preserve-comments/expected-bundle.js b/test/js/samples/ssr-preserve-comments/expected-bundle.js new file mode 100644 index 0000000000..19461df020 --- /dev/null +++ b/test/js/samples/ssr-preserve-comments/expected-bundle.js @@ -0,0 +1,59 @@ +var SvelteComponent = {}; +SvelteComponent.data = function() { + return {}; +}; + +SvelteComponent.render = function(state, options = {}) { + var components = new Set(); + + function addComponent(component) { + components.add(component); + } + + var result = { head: '', addComponent }; + var html = SvelteComponent._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; + } + }; +}; + +SvelteComponent._render = function(__result, state, options) { + __result.addComponent(SvelteComponent); + + state = Object.assign({}, state); + + return `
content
+ +
more content
`; +}; + +SvelteComponent.css = { + code: '', + map: null +}; + +var warned = false; +SvelteComponent.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 + }; +}; + +module.exports = SvelteComponent; diff --git a/test/js/samples/ssr-preserve-comments/expected.js b/test/js/samples/ssr-preserve-comments/expected.js new file mode 100644 index 0000000000..eff9344154 --- /dev/null +++ b/test/js/samples/ssr-preserve-comments/expected.js @@ -0,0 +1,62 @@ +"use strict"; + +var SvelteComponent = {};; + +SvelteComponent.data = function() { + return {}; +}; + +SvelteComponent.render = function(state, options = {}) { + var components = new Set(); + + function addComponent(component) { + components.add(component); + } + + var result = { head: '', addComponent }; + var html = SvelteComponent._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; + } + }; +} + +SvelteComponent._render = function(__result, state, options) { + __result.addComponent(SvelteComponent); + + state = Object.assign({}, state); + + return `
content
+ +
more content
`; +}; + +SvelteComponent.css = { + code: '', + map: null +}; + +var warned = false; +SvelteComponent.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 + }; +}; + +module.exports = SvelteComponent; diff --git a/test/js/samples/ssr-preserve-comments/input.html b/test/js/samples/ssr-preserve-comments/input.html new file mode 100644 index 0000000000..7f7e291caa --- /dev/null +++ b/test/js/samples/ssr-preserve-comments/input.html @@ -0,0 +1,3 @@ +
content
+ +
more content