Skip to content

Commit

Permalink
Merge pull request #674 from sveltejs/gh-655
Browse files Browse the repository at this point in the history
deconflict name with imports
  • Loading branch information
Rich-Harris authored Jun 25, 2017
2 parents e582691 + 6751a83 commit b1d1cea
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 50 deletions.
48 changes: 10 additions & 38 deletions src/generators/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export default class Generator {

this.parsed = parsed;
this.source = source;
this.name = name;
this.options = options;

this.imports = [];
Expand All @@ -81,7 +80,10 @@ export default class Generator {
// Svelte's builtin `import { get, ... } from 'svelte/shared.ts'`;
this.importedNames = new Set();
this.aliases = new Map();
this.usedNames = new Set([name]);
this.usedNames = new Set();

this.parseJs();
this.name = this.alias(name);
}

addSourcemapLocations(node: Node) {
Expand Down Expand Up @@ -389,7 +391,7 @@ export default class Generator {
};
}

parseJs(ssr: boolean = false) {
parseJs() {
const { source } = this;
const { js } = this.parsed;

Expand Down Expand Up @@ -417,7 +419,7 @@ export default class Generator {
}
}

const defaultExport = body.find(
const defaultExport = this.defaultExport = body.find(
(node: Node) => node.type === 'ExportDefaultDeclaration'
);

Expand Down Expand Up @@ -525,34 +527,6 @@ export default class Generator {
templateProperties.ondestroy = templateProperties.onteardown;
}

// in an SSR context, we don't need to include events, methods, oncreate or ondestroy
if (ssr) {
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
);
}

// now that we've analysed the default export, we can determine whether or not we need to keep it
let hasDefaultExport = !!defaultExport;
if (defaultExport && defaultExport.declaration.properties.length === 0) {
Expand Down Expand Up @@ -611,11 +585,9 @@ export default class Generator {
}
}

return {
computations,
hasJs,
namespace,
templateProperties,
};
this.computations = computations;
this.hasJs = hasJs;
this.namespace = namespace;
this.templateProperties = templateProperties;
}
}
6 changes: 3 additions & 3 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ export default function dom(
options: CompileOptions
) {
const format = options.format || 'es';
const name = options.name || 'SvelteComponent';

const generator = new DomGenerator(parsed, source, name, options);
const generator = new DomGenerator(parsed, source, options.name || 'SvelteComponent', options);

const {
computations,
hasJs,
name,
templateProperties,
namespace,
} = generator.parseJs();
} = generator;

const { block, state } = preprocess(generator, namespace, parsed.html);

Expand Down
34 changes: 31 additions & 3 deletions src/generators/server-side-rendering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import deindent from '../../utils/deindent';
import Generator from '../Generator';
import Block from './Block';
import visit from './visit';
import { removeNode, removeObjectKey } from '../../utils/removeNode';
import { Parsed, Node, CompileOptions } from '../../interfaces';

export class SsrGenerator extends Generator {
Expand All @@ -19,6 +20,34 @@ export class SsrGenerator extends Generator {
this.bindings = [];
this.renderCode = '';
this.elementDepth = 0;

// in an SSR context, we don't need to include events, methods, oncreate or ondestroy
const { templateProperties, defaultExport } = this;

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) {
Expand All @@ -32,11 +61,10 @@ export default function ssr(
options: CompileOptions
) {
const format = options.format || 'cjs';
const name = options.name || 'SvelteComponent';

const generator = new SsrGenerator(parsed, source, name, options);
const generator = new SsrGenerator(parsed, source, options.name || 'SvelteComponent', options);

const { computations, hasJs, templateProperties } = generator.parseJs(true);
const { computations, name, hasJs, templateProperties } = generator;

// create main render() function
const mainBlock = new Block({
Expand Down
2 changes: 1 addition & 1 deletion src/validate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default function validate(

try {
if (name && !/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name)) {
const error = new Error(`options.name must be a valid identifier`);
const error = new Error(`options.name must be a valid identifier (got '${name}')`);
throw error;
}

Expand Down
5 changes: 3 additions & 2 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function addLineNumbers(code) {
.join('\n');
}

function capitalize(str) {
function capitalise(str) {
return str[0].toUpperCase() + str.slice(1);
}

Expand All @@ -173,7 +173,8 @@ export function showOutput(cwd, options = {}) {
const { code } = svelte.compile(
fs.readFileSync(`${cwd}/${file}`, 'utf-8'),
Object.assign(options, {
name: capitalize(file.slice(0, -path.extname(file).length))
filename: file,
name: capitalise(path.basename(file).replace(/\.html$/, ''))
})
);

Expand Down
3 changes: 3 additions & 0 deletions test/runtime/samples/deconflict-self/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: `<p>nested component</p>`
};
9 changes: 9 additions & 0 deletions test/runtime/samples/deconflict-self/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Main/>

<script>
import Main from './nested/main.html';

export default {
components: { Main }
};
</script>
1 change: 1 addition & 0 deletions test/runtime/samples/deconflict-self/nested/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>nested component</p>
2 changes: 0 additions & 2 deletions test/runtime/samples/self-reference/_config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export default {
// solo: true,

data: {
depth: 5
},
Expand Down
3 changes: 2 additions & 1 deletion test/server-side-rendering/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "assert";
import * as fs from "fs";
import * as path from "path";
import glob from 'glob';

import {
showOutput,
Expand Down Expand Up @@ -88,7 +89,7 @@ describe("ssr", () => {
(config.skip ? it.skip : config.solo ? it.only : it)(dir, () => {
const cwd = path.resolve("test/runtime/samples", dir);

fs.readdirSync(`test/runtime/samples/${dir}`).forEach(file => {
glob.sync('**/*.html', { cwd: `test/runtime/samples/${dir}` }).forEach(file => {
const resolved = require.resolve(`../runtime/samples/${dir}/${file}`);
delete require.cache[resolved];
});
Expand Down

0 comments on commit b1d1cea

Please sign in to comment.