Skip to content

Commit

Permalink
Merge branch 'preserve-comments-option' of https://github.com/zzolo/s…
Browse files Browse the repository at this point in the history
…velte into zzolo-preserve-comments-option
  • Loading branch information
Rich-Harris committed Mar 24, 2018
2 parents 006a45c + 3c61655 commit 61d3ab0
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
11 changes: 9 additions & 2 deletions src/generators/server-side-rendering/visitors/Comment.ts
Original file line number Diff line number Diff line change
@@ -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}-->`);
}
}
4 changes: 4 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export interface CompileOptions {
css?: boolean;
store?: boolean;

ssr?: {
preserveComments?: boolean | false;
};

onerror?: (error: Error) => void;
onwarn?: (warning: Warning) => void;
}
Expand Down
6 changes: 6 additions & 0 deletions test/js/samples/ssr-preserve-comments/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
options: {
generate: 'ssr',
preserveComments: true
}
};
59 changes: 59 additions & 0 deletions test/js/samples/ssr-preserve-comments/expected-bundle.js
Original file line number Diff line number Diff line change
@@ -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;
62 changes: 62 additions & 0 deletions test/js/samples/ssr-preserve-comments/expected.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions test/js/samples/ssr-preserve-comments/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>content</div>
<!-- comment -->
<div>more content</div>

0 comments on commit 61d3ab0

Please sign in to comment.