Skip to content

Commit

Permalink
Merge branch 'master' into gh-1175
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 25, 2018
2 parents a5cc451 + 0648998 commit e95a0b6
Show file tree
Hide file tree
Showing 95 changed files with 902 additions and 402 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Svelte changelog

## 1.56.0

* Internal refactor ([#1122](https://github.com/sveltejs/svelte/issues/1122))
* Use correct context for component events ([#1184](https://github.com/sveltejs/svelte/issues/1184))
* Allow observing `$foo` in dev mode ([#1181](https://github.com/sveltejs/svelte/issues/1181))
* Handle dynamic data in default slot ([#1144](https://github.com/sveltejs/svelte/issues/1144))

## 1.55.1

* Fix cancellation of store `onchange` handlers ([#1177](https://github.com/sveltejs/svelte/issues/1177))
* Write `["default"]` instead of `.default` in legacy mode ([#1166](https://github.com/sveltejs/svelte/issues/1166))
* Upgrade Acorn ([#1182](https://github.com/sveltejs/svelte/pull/1182))
* Don't warn about capitalisation if `options.name` begins with non-alphabetical character ([#1179](https://github.com/sveltejs/svelte/pull/1179))

## 1.55.0

* Add `immutable` compiler option for Svelte and runtime option for `Store` ([#1146](https://github.com/sveltejs/svelte/issues/1146))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This is the Svelte compiler, which is primarily intended for authors of tooling
* [svelte-hot-loader](https://github.com/ekhaled/svelte-hot-loader) – Webpack loader addon to support HMR
* [meteor-svelte](https://github.com/klaussner/meteor-svelte) – Meteor build plugin
* [sveltejs-brunch](https://github.com/StarpTech/sveltejs-brunch) – Brunch build plugin
* [svelte-dev-store](https://github.com/GarethOates/svelte-dev-store) - Use Redux tools to visualise Svelte store
* More to come!


Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte",
"version": "1.55.0",
"version": "1.56.0",
"description": "The magical disappearing UI framework",
"main": "compiler/svelte.js",
"files": [
Expand Down Expand Up @@ -45,7 +45,7 @@
"devDependencies": {
"@types/mocha": "^2.2.41",
"@types/node": "^8.0.17",
"acorn": "^5.1.1",
"acorn": "^5.4.1",
"acorn-dynamic-import": "^2.0.2",
"chalk": "^2.0.1",
"codecov": "^2.2.0",
Expand Down
17 changes: 0 additions & 17 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,11 @@ import typescript from 'rollup-plugin-typescript';
import buble from 'rollup-plugin-buble';
import pkg from './package.json';

const src = path.resolve('src');

export default [
/* compiler/svelte.js */
{
input: 'src/index.ts',
plugins: [
{
resolveId(importee, importer) {
// bit of a hack — TypeScript only really works if it can resolve imports,
// but they misguidedly chose to reject imports with file extensions. This
// means we need to resolve them here
if (
importer &&
importer.startsWith(src) &&
importee[0] === '.' &&
path.extname(importee) === ''
) {
return path.resolve(path.dirname(importer), `${importee}.ts`);
}
}
},
replace({
__VERSION__: pkg.version
}),
Expand Down
4 changes: 2 additions & 2 deletions src/generators/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ export default class Generator {
return alias;
}

getUniqueNameMaker(params: string[]) {
const localUsedNames = new Set(params);
getUniqueNameMaker() {
const localUsedNames = new Set();

function add(name: string) {
localUsedNames.add(name);
Expand Down
38 changes: 31 additions & 7 deletions src/generators/dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface BlockOptions {
contextTypes?: Map<string, string>;
indexes?: Map<string, string>;
changeableIndexes?: Map<string, boolean>;
params?: string[];
indexNames?: Map<string, string>;
listNames?: Map<string, string>;
indexName?: string;
Expand All @@ -41,7 +40,6 @@ export default class Block {
indexes: Map<string, string>;
changeableIndexes: Map<string, boolean>;
dependencies: Set<string>;
params: string[];
indexNames: Map<string, string>;
listNames: Map<string, string>;
indexName: string;
Expand Down Expand Up @@ -90,10 +88,10 @@ export default class Block {
this.changeableIndexes = options.changeableIndexes;
this.dependencies = new Set();

this.params = options.params;
this.indexNames = options.indexNames;
this.listNames = options.listNames;

this.indexName = options.indexName;
this.listName = options.listName;

this.builders = {
Expand All @@ -116,7 +114,7 @@ export default class Block {

this.aliases = new Map();
this.variables = new Map();
this.getUniqueName = this.generator.getUniqueNameMaker(options.params);
this.getUniqueName = this.generator.getUniqueNameMaker();

this.hasUpdateMethod = false; // determined later
}
Expand Down Expand Up @@ -191,6 +189,29 @@ export default class Block {
this.builders.mount.addLine(`${this.autofocus}.focus();`);
}

// TODO `this.contexts` is possibly redundant post-#1122
const initializers = [];
const updaters = [];
this.contexts.forEach((alias, name) => {
// TODO only the ones that are actually used in this block...
const assignment = `${alias} = state.${name}`;

initializers.push(assignment);
updaters.push(`${assignment};`);

this.hasUpdateMethod = true;
});

this.indexNames.forEach((alias, name) => {
// TODO only the ones that are actually used in this block...
const assignment = `${alias} = state.${alias}`; // TODO this is wrong!!!

initializers.push(assignment);
updaters.push(`${assignment};`);

this.hasUpdateMethod = true;
});

// minor hack – we need to ensure that any {{{triples}}} are detached first
this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString());

Expand Down Expand Up @@ -250,11 +271,12 @@ export default class Block {
}

if (this.hasUpdateMethod) {
if (this.builders.update.isEmpty()) {
if (this.builders.update.isEmpty() && updaters.length === 0) {
properties.addBlock(`p: @noop,`);
} else {
properties.addBlock(deindent`
p: function update(changed, ${this.params.join(', ')}) {
p: function update(changed, state) {
${updaters}
${this.builders.update}
},
`);
Expand Down Expand Up @@ -328,7 +350,9 @@ export default class Block {

return deindent`
${this.comment && `// ${escape(this.comment)}`}
function ${this.name}(${this.params.join(', ')}, #component${this.key ? `, ${localKey}` : ''}) {
function ${this.name}(#component${this.key ? `, ${localKey}` : ''}, state) {
${initializers.length > 0 &&
`var ${initializers.join(', ')};`}
${this.variables.size > 0 &&
`var ${Array.from(this.variables.keys())
.map(key => {
Expand Down
6 changes: 3 additions & 3 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class DomGenerator extends Generator {
this.metaBindings = [];
}

getUniqueNameMaker(params: string[]) {
const localUsedNames = new Set(params);
getUniqueNameMaker() {
const localUsedNames = new Set();

function add(name: string) {
localUsedNames.add(name);
Expand Down Expand Up @@ -257,7 +257,7 @@ export default function dom(
${generator.slots.size && `this.slots = {};`}
this._fragment = @create_main_fragment(this._state, this);
this._fragment = @create_main_fragment(this, this._state);
${(templateProperties.oncreate) && deindent`
this.root._oncreate.push(_oncreate);
Expand Down
57 changes: 31 additions & 26 deletions src/generators/nodes/AwaitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,19 @@ export default class AwaitBlock extends Node {
].forEach(([status, arg]) => {
const child = this[status];

const context = block.getUniqueName(arg || '_'); // TODO can we remove the extra param from pending blocks?
const contexts = new Map(block.contexts);
contexts.set(arg, context);

const contextTypes = new Map(block.contextTypes);
contextTypes.set(arg, status);

child.block = block.child({
comment: createDebuggingComment(child, this.generator),
name: this.generator.getUniqueName(`create_${status}_block`),
params: block.params.concat(context),
context,
contexts,
contextTypes
contexts: new Map(block.contexts),
contextTypes: new Map(block.contextTypes)
});

if (arg) {
child.block.context = arg;
child.block.contexts.set(arg, arg); // TODO should be using getUniqueName
child.block.contextTypes.set(arg, status);
}

child.initChildren(child.block, stripWhitespace, nextSibling);
this.generator.blocks.push(child.block);

Expand All @@ -75,8 +72,6 @@ export default class AwaitBlock extends Node {
const anchor = this.getOrCreateAnchor(block, parentNode, parentNodes);
const updateMountNode = this.getUpdateMountNode(anchor);

const params = block.params.join(', ');

block.contextualise(this.expression);
const { snippet } = this.metadata;

Expand Down Expand Up @@ -106,11 +101,11 @@ export default class AwaitBlock extends Node {
// but it's probably not worth it

block.builders.init.addBlock(deindent`
function ${replace_await_block}(${token}, type, ${value}, ${params}) {
function ${replace_await_block}(${token}, type, state) {
if (${token} !== ${await_token}) return;
var ${old_block} = ${await_block};
${await_block} = (${await_block_type} = type)(${params}, ${resolved} = ${value}, #component);
${await_block} = type && (${await_block_type} = type)(#component, state);
if (${old_block}) {
${old_block}.u();
Expand All @@ -122,33 +117,43 @@ export default class AwaitBlock extends Node {
}
}
function ${handle_promise}(${promise}, ${params}) {
function ${handle_promise}(${promise}, state) {
var ${token} = ${await_token} = {};
if (@isPromise(${promise})) {
${promise}.then(function(${value}) {
var state = #component.get();
${replace_await_block}(${token}, ${create_then_block}, ${value}, ${params});
${this.then.block.context ? deindent`
var state = #component.get();
${resolved} = { ${this.then.block.context}: ${value} };
${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved}));
` : deindent`
${replace_await_block}(${token}, null, null);
`}
}, function (${error}) {
var state = #component.get();
${replace_await_block}(${token}, ${create_catch_block}, ${error}, ${params});
${this.catch.block.context ? deindent`
var state = #component.get();
${resolved} = { ${this.catch.block.context}: ${error} };
${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, ${resolved}));
` : deindent`
${replace_await_block}(${token}, null, null);
`}
});
// if we previously had a then/catch block, destroy it
if (${await_block_type} !== ${create_pending_block}) {
${replace_await_block}(${token}, ${create_pending_block}, null, ${params});
${replace_await_block}(${token}, ${create_pending_block}, state);
return true;
}
} else {
${resolved} = ${promise};
${resolved} = { ${this.then.block.context}: ${promise} };
if (${await_block_type} !== ${create_then_block}) {
${replace_await_block}(${token}, ${create_then_block}, ${resolved}, ${params});
${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved}));
return true;
}
}
}
${handle_promise}(${promise} = ${snippet}, ${params});
${handle_promise}(${promise} = ${snippet}, state);
`);

block.builders.create.addBlock(deindent`
Expand Down Expand Up @@ -177,15 +182,15 @@ export default class AwaitBlock extends Node {

conditions.push(
`${promise} !== (${promise} = ${snippet})`,
`${handle_promise}(${promise}, ${params})`
`${handle_promise}(${promise}, state)`
);

if (this.pending.block.hasUpdateMethod) {
block.builders.update.addBlock(deindent`
if (${conditions.join(' && ')}) {
// nothing
} else {
${await_block}.p(changed, ${params}, ${resolved});
${await_block}.p(changed, @assign({}, state, ${resolved}));
}
`);
} else {
Expand Down
Loading

0 comments on commit e95a0b6

Please sign in to comment.