mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
exclude irrelevant stuff from SSR output
This commit is contained in:
parent
7f39b5be16
commit
6436886500
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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) {
|
||||
|
5
test/js/samples/ssr-no-oncreate-etc/_config.js
Normal file
5
test/js/samples/ssr-no-oncreate-etc/_config.js
Normal file
@ -0,0 +1,5 @@
|
||||
export default {
|
||||
options: {
|
||||
generate: 'ssr'
|
||||
}
|
||||
};
|
23
test/js/samples/ssr-no-oncreate-etc/expected-bundle.js
Normal file
23
test/js/samples/ssr-no-oncreate-etc/expected-bundle.js
Normal file
@ -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;
|
37
test/js/samples/ssr-no-oncreate-etc/expected.js
Normal file
37
test/js/samples/ssr-no-oncreate-etc/expected.js
Normal file
@ -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;
|
23
test/js/samples/ssr-no-oncreate-etc/input.html
Normal file
23
test/js/samples/ssr-no-oncreate-etc/input.html
Normal file
@ -0,0 +1,23 @@
|
||||
<script>
|
||||
export default {
|
||||
oncreate() {
|
||||
console.log('oncreate');
|
||||
},
|
||||
|
||||
ondestroy() {
|
||||
console.log('ondestroy');
|
||||
},
|
||||
|
||||
methods: {
|
||||
foo() {
|
||||
console.log('foo');
|
||||
}
|
||||
},
|
||||
|
||||
events: {
|
||||
swipe(node, callback) {
|
||||
// TODO implement
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user