From 6cf3f1121fcafd7445c33c3e2911ebb47f888f6c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 28 Apr 2018 18:24:59 -0400 Subject: [PATCH] replace all ssr __helpers --- src/generators/Generator.ts | 4 ++ src/generators/nodes/Attribute.ts | 2 +- src/generators/nodes/AwaitBlock.ts | 2 +- src/generators/nodes/Component.ts | 4 +- src/generators/nodes/EachBlock.ts | 2 +- src/generators/nodes/MustacheTag.ts | 2 +- src/generators/server-side-rendering/index.ts | 45 ------------------- src/shared/index.js | 1 + src/shared/ssr.js | 26 ++++++++++- 9 files changed, 36 insertions(+), 52 deletions(-) diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index f8b0446a68..fbee4037c4 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -272,6 +272,10 @@ export default class Generator { const global = `_svelteTransitionManager`; inlineHelpers += `\n\nvar ${this.alias('transitionManager')} = window.${global} || (window.${global} = ${code});\n\n`; + } else if (key === 'escaped' || key === 'missingComponent') { + // vars are an awkward special case... would be nice to avoid this + const alias = this.alias(key); + inlineHelpers += `\n\nconst ${alias} = ${code};` } else { const alias = this.alias(expression.id.name); if (alias !== expression.id.name) { diff --git a/src/generators/nodes/Attribute.ts b/src/generators/nodes/Attribute.ts index daecfd17f6..8b543715c2 100644 --- a/src/generators/nodes/Attribute.ts +++ b/src/generators/nodes/Attribute.ts @@ -340,7 +340,7 @@ export default class Attribute extends Node { return escapeTemplate(escape(chunk.data).replace(/"/g, '"')); } - return '${__escape(' + chunk.snippet + ')}'; + return '${@escape(' + chunk.snippet + ')}'; }) .join(''); } diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index ebb05d45d7..634b97d659 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -225,7 +225,7 @@ export default class AwaitBlock extends Node { const childBlock = block.child({}); - compiler.append('${(function(__value) { if(__isPromise(__value)) return `'); + compiler.append('${(function(__value) { if(@isPromise(__value)) return `'); this.pending.children.forEach((child: Node) => { child.ssr(compiler, block); diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 2832efc735..2dc2a412b6 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -489,7 +489,7 @@ export default class Component extends Node { return escapeTemplate(escape(chunk.data)); } - return '${__escape( ' + chunk.snippet + ')}'; + return '${@escape( ' + chunk.snippet + ')}'; } const bindingProps = this.bindings.map(binding => { @@ -541,7 +541,7 @@ export default class Component extends Node { const expression = ( this.name === 'svelte:self' ? compiler.name : - isDynamicComponent ? `((${this.expression.snippet}) || __missingComponent)` : + isDynamicComponent ? `((${this.expression.snippet}) || @missingComponent)` : `%components-${this.name}` ); diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 81dcdce0ad..52f3869c5c 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -483,7 +483,7 @@ export default class EachBlock extends Node { ? `(item, i) => Object.assign({}, ctx, { ${props.join(', ')}, ${this.index}: i })` : `item => Object.assign({}, ctx, { ${props.join(', ')} })`; - const open = `\${ ${this.else ? `${snippet}.length ? ` : ''}__each(${snippet}, ${getContext}, ctx => \``; + const open = `\${ ${this.else ? `${snippet}.length ? ` : ''}@each(${snippet}, ${getContext}, ctx => \``; compiler.append(open); const childBlock = block.child({}); diff --git a/src/generators/nodes/MustacheTag.ts b/src/generators/nodes/MustacheTag.ts index cd66e4aa30..9547db2878 100644 --- a/src/generators/nodes/MustacheTag.ts +++ b/src/generators/nodes/MustacheTag.ts @@ -31,7 +31,7 @@ export default class MustacheTag extends Tag { this.parent.type === 'Element' && this.parent.name === 'style' ? '${' + this.expression.snippet + '}' - : '${__escape(' + this.expression.snippet + ')}' + : '${@escape(' + this.expression.snippet + ')}' ); } } \ No newline at end of file diff --git a/src/generators/server-side-rendering/index.ts b/src/generators/server-side-rendering/index.ts index dfbd68259b..e6ec4fc3d9 100644 --- a/src/generators/server-side-rendering/index.ts +++ b/src/generators/server-side-rendering/index.ts @@ -163,51 +163,6 @@ export default function ssr( var warned = false; ${templateProperties.preload && `${name}.preload = %preload;`} - - ${ - // TODO this is a bit hacky - /__escape/.test(generator.renderCode) && deindent` - var escaped = { - '"': '"', - "'": '&##39;', - '&': '&', - '<': '<', - '>': '>' - }; - - function __escape(html) { - return String(html).replace(/["'&<>]/g, match => escaped[match]); - } - ` - } - - ${ - /__each/.test(generator.renderCode) && deindent` - function __each(items, assign, fn) { - let str = ''; - for (let i = 0; i < items.length; i += 1) { - str += fn(assign(items[i], i)); - } - return str; - } - ` - } - - ${ - /__isPromise/.test(generator.renderCode) && deindent` - function __isPromise(value) { - return value && typeof value.then === 'function'; - } - ` - } - - ${ - /__missingComponent/.test(generator.renderCode) && deindent` - var __missingComponent = { - _render: () => '' - }; - ` - } `.replace(/(@+|#+|%+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => { if (sigil === '@') { helpers.add(name); diff --git a/src/shared/index.js b/src/shared/index.js index 1040e5eb6b..774849f027 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -3,6 +3,7 @@ import { noop } from './utils.js'; export * from './dom.js'; export * from './keyed-each.js'; export * from './spread.js'; +export * from './ssr.js'; export * from './transitions.js'; export * from './utils.js'; diff --git a/src/shared/ssr.js b/src/shared/ssr.js index c2253f6d23..8381118f3e 100644 --- a/src/shared/ssr.js +++ b/src/shared/ssr.js @@ -10,4 +10,28 @@ export function spread(args) { }); return str; -} \ No newline at end of file +} + +export const escaped = { + '"': '"', + "'": ''', + '&': '&', + '<': '<', + '>': '>' +}; + +export function escape(html) { + return String(html).replace(/["'&<>]/g, match => escaped[match]); +} + +export function each(items, assign, fn) { + let str = ''; + for (let i = 0; i < items.length; i += 1) { + str += fn(assign(items[i], i)); + } + return str; +} + +export const missingComponent = { + _render: () => '' +}; \ No newline at end of file