Skip to content

Commit

Permalink
shuffle some stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 28, 2018
1 parent 8759ccd commit 8fc59d7
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 47 deletions.
36 changes: 29 additions & 7 deletions src/generators/Generator.ts
Expand Up @@ -17,7 +17,7 @@ import getName from '../utils/getName';
import Stylesheet from '../css/Stylesheet';
import { test } from '../config';
import Fragment from './nodes/Fragment';
import shared from './dom/shared'; // TODO move this file
import shared from './shared';
import { Node, GenerateOptions, ShorthandImport, Ast, CompileOptions, CustomElementOptions } from '../interfaces';

interface Computation {
Expand Down Expand Up @@ -193,6 +193,8 @@ export default class Generator {
this.fragment = new Fragment(this, ast.html);
// this.walkTemplate();
if (!this.customElement) this.stylesheet.reify();

stylesheet.warnOnUnusedSelectors(options.onwarn);
}

addSourcemapLocations(node: Node) {
Expand All @@ -212,9 +214,29 @@ export default class Generator {
return this.aliases.get(name);
}

generate(result: string, options: CompileOptions, { banner = '', helpers, name, format }: GenerateOptions ) {
generate(result: string, options: CompileOptions, { banner = '', name, format }: GenerateOptions ) {
const pattern = /\[✂(\d+)-(\d+)$/;

const helpers = new Set();

// TODO use same regex for both
result = result.replace(options.generate === 'ssr' ? /(@+|#+|%+)(\w*(?:-\w*)?)/g : /(%+|@+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => {
if (sigil === '@') {
if (name in shared) {
if (options.dev && `${name}Dev` in shared) name = `${name}Dev`;
helpers.add(name);
}

return this.alias(name);
}

if (sigil === '%') {
return this.templateVars.get(name);
}

return sigil.slice(1) + name;
});

let importedHelpers;

if (options.shared) {
Expand All @@ -233,8 +255,8 @@ export default class Generator {

importedHelpers = [];

helpers.forEach(key => {
const str = shared[key];
helpers.forEach(name => {
const str = shared[name];
const code = new MagicString(str);
const expression = parseExpressionAt(str, 0);

Expand Down Expand Up @@ -267,14 +289,14 @@ export default class Generator {
},
});

if (key === 'transitionManager') {
if (name === 'transitionManager') {
// special case
const global = `_svelteTransitionManager`;

inlineHelpers += `\n\nvar ${this.alias('transitionManager')} = window.${global} || (window.${global} = ${code});\n\n`;
} else if (key === 'escaped' || key === 'missingComponent') {
} else if (name === 'escaped' || name === 'missingComponent') {
// vars are an awkward special case... would be nice to avoid this
const alias = this.alias(key);
const alias = this.alias(name);
inlineHelpers += `\n\nconst ${alias} = ${code};`
} else {
const alias = this.alias(expression.id.name);
Expand Down
1 change: 0 additions & 1 deletion src/generators/dom/Block.ts
Expand Up @@ -3,7 +3,6 @@ import deindent from '../../utils/deindent';
import { escape } from '../../utils/stringify';
import { DomGenerator } from './index';
import { Node } from '../../interfaces';
import shared from './shared';

export interface BlockOptions {
name: string;
Expand Down
24 changes: 1 addition & 23 deletions src/generators/dom/index.ts
Expand Up @@ -76,8 +76,6 @@ export default function dom(
// prevent fragment being created twice (#1063)
if (options.customElement) block.builders.create.addLine(`this.c = @noop;`);

generator.stylesheet.warnOnUnusedSelectors(options.onwarn);

const builder = new CodeBuilder();
const computationBuilder = new CodeBuilder();
const computationDeps = new Set();
Expand Down Expand Up @@ -361,26 +359,7 @@ export default function dom(
${immutable && `${name}.prototype._differs = @_differsImmutable;`}
`);

const helpers = new Set();

let result = builder
.toString()
.replace(/(%+|@+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => {
if (sigil === '@') {
if (name in shared) {
if (options.dev && `${name}Dev` in shared) name = `${name}Dev`;
helpers.add(name);
}

return generator.alias(name);
}

if (sigil === '%') {
return generator.templateVars.get(name);
}

return sigil.slice(1) + name;
});
let result = builder.toString();

const filename = options.filename && (
typeof process !== 'undefined' ? options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : options.filename
Expand All @@ -389,7 +368,6 @@ export default function dom(
return generator.generate(result, options, {
banner: `/* ${filename ? `${filename} ` : ``}generated by Svelte v${"__VERSION__"} */`,
sharedPath,
helpers,
name,
format,
});
Expand Down
2 changes: 1 addition & 1 deletion src/generators/nodes/EachBlock.ts
Expand Up @@ -5,7 +5,7 @@ import Block from '../dom/Block';
import createDebuggingComment from '../../utils/createDebuggingComment';
import Expression from './shared/Expression';
import mapChildren from './shared/mapChildren';
import TemplateScope from '../dom/TemplateScope';
import TemplateScope from './shared/TemplateScope';

export default class EachBlock extends Node {
type: 'EachBlock';
Expand Down
2 changes: 1 addition & 1 deletion src/generators/nodes/Fragment.ts
Expand Up @@ -3,7 +3,7 @@ import { DomGenerator } from '../dom/index';
import Generator from '../Generator';
import mapChildren from './shared/mapChildren';
import Block from '../dom/Block';
import TemplateScope from '../dom/TemplateScope';
import TemplateScope from './shared/TemplateScope';

export default class Fragment extends Node {
block: Block;
Expand Down
File renamed without changes.
14 changes: 2 additions & 12 deletions src/generators/server-side-rendering/index.ts
Expand Up @@ -27,8 +27,6 @@ export class SsrGenerator extends Generator {
this.bindings = [];
this.renderCode = '';
this.appendTargets = [];

this.stylesheet.warnOnUnusedSelectors(options.onwarn);
}

append(code: string) {
Expand Down Expand Up @@ -157,17 +155,9 @@ export default function ssr(
var warned = false;
${templateProperties.preload && `${name}.preload = %preload;`}
`.replace(/(@+|#+|%+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => {
if (sigil === '@') {
helpers.add(name);
return generator.alias(name);
}

if (sigil === '%') return generator.templateVars.get(name);
return sigil.slice(1) + name;
});
`;

return generator.generate(result, options, { name, format, helpers });
return generator.generate(result, options, { name, format });
}

function trim(nodes) {
Expand Down
79 changes: 79 additions & 0 deletions src/generators/shared.ts

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/interfaces.ts
Expand Up @@ -71,7 +71,6 @@ export interface GenerateOptions {
format: ModuleFormat;
banner?: string;
sharedPath?: string;
helpers: Set<string>;
}

export interface ShorthandImport {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/_build.js
Expand Up @@ -32,7 +32,7 @@ fs.readdirSync(__dirname).forEach(file => {
});

fs.writeFileSync(
'src/generators/dom/shared.ts',
'src/generators/shared.ts',
`// this file is auto-generated, do not edit it
const shared: Record<string, string> = ${JSON.stringify(declarations, null, '\t')};
Expand Down

0 comments on commit 8fc59d7

Please sign in to comment.