Add option preserve comments in SSR rendering.

pull/1266/head
Alan Palazzolo 7 years ago
parent bc416a538f
commit 3c61655e93

@ -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) |

@ -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(`<!--${node.data}-->`);
}
}

@ -62,6 +62,10 @@ export interface CompileOptions {
css?: boolean;
store?: boolean;
ssr?: {
preserveComments?: boolean | false;
};
onerror?: (error: Error) => void;
onwarn?: (warning: Warning) => void;
}

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

@ -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 `<div>content</div>
<!-- comment -->
<div>more content</div>`;
};
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;

@ -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 `<div>content</div>
<!-- comment -->
<div>more content</div>`;
};
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;

@ -0,0 +1,3 @@
<div>content</div>
<!-- comment -->
<div>more content</div>
Loading…
Cancel
Save