diff --git a/README.md b/README.md index 95b352e16a87..6a9c5455f590 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 ad10d20aa542..84d5c0fa7f2c 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 de187da71d1b..36c0ff28635f 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 000000000000..9e62c8d1922f --- /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 000000000000..19461df02059 --- /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 000000000000..eff9344154fc --- /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 000000000000..7f7e291caa34 --- /dev/null +++ b/test/js/samples/ssr-preserve-comments/input.html @@ -0,0 +1,3 @@ +
content
+ +
more content