Skip to content

Commit 33dbc18

Browse files
committed
merge master -> shared-init
2 parents 0b71a93 + 9d8f2c4 commit 33dbc18

File tree

47 files changed

+135
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+135
-5
lines changed

src/generators/Generator.ts

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export default class Generator {
5656
expectedProperties: Set<string>;
5757
usesRefs: boolean;
5858

59+
locate: (c: number) => { line: number, column: number };
60+
5961
stylesheet: Stylesheet;
6062

6163
importedNames: Set<string>;
@@ -86,6 +88,8 @@ export default class Generator {
8688
this.bindingGroups = [];
8789
this.indirectDependencies = new Map();
8890

91+
this.locate = getLocator(this.source);
92+
8993
// track which properties are needed, so we can provide useful info
9094
// in dev mode
9195
this.expectedProperties = new Set();

src/generators/dom/Block.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import CodeBuilder from '../../utils/CodeBuilder';
22
import deindent from '../../utils/deindent';
3+
import { escape } from '../../utils/stringify';
34
import { DomGenerator } from './index';
45
import { Node } from '../../interfaces';
56
import shared from './shared';
@@ -9,6 +10,7 @@ export interface BlockOptions {
910
generator?: DomGenerator;
1011
expression?: Node;
1112
context?: string;
13+
comment?: string;
1214
key?: string;
1315
contexts?: Map<string, string>;
1416
indexes?: Map<string, string>;
@@ -27,6 +29,7 @@ export default class Block {
2729
name: string;
2830
expression: Node;
2931
context: string;
32+
comment?: string;
3033

3134
key: string;
3235
first: string;
@@ -72,6 +75,7 @@ export default class Block {
7275
this.name = options.name;
7376
this.expression = options.expression;
7477
this.context = options.context;
78+
this.comment = options.comment;
7579

7680
// for keyed each blocks
7781
this.key = options.key;
@@ -340,6 +344,7 @@ export default class Block {
340344
}
341345

342346
return deindent`
347+
${this.comment && `// ${escape(this.comment)}`}
343348
function ${this.name}(${this.params.join(', ')}, #component${this.key ? `, ${localKey}` : ''}) {
344349
${this.variables.size > 0 &&
345350
`var ${Array.from(this.variables.keys())

src/generators/dom/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Generator from '../Generator';
1212
import Stylesheet from '../../css/Stylesheet';
1313
import preprocess from './preprocess';
1414
import Block from './Block';
15+
import { version } from '../../../package.json';
1516
import { Parsed, CompileOptions, Node } from '../../interfaces';
1617

1718
export class DomGenerator extends Generator {
@@ -396,6 +397,8 @@ export default function dom(
396397
});
397398
}
398399

400+
result = `/* ${options.filename ? `${options.filename} ` : ``}generated by Svelte v${version} */\n\n${result}`;
401+
399402
return generator.generate(result, options, {
400403
name,
401404
format,

src/generators/dom/preprocess.ts

+23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ function getChildState(parent: State, child = {}) {
2222
);
2323
}
2424

25+
function createDebuggingComment(node: Node, generator: DomGenerator) {
26+
const { locate, source } = generator;
27+
28+
let c = node.start;
29+
if (node.type === 'ElseBlock') {
30+
while (source[c] !== '{') c -= 1;
31+
c -= 1;
32+
}
33+
34+
let d = node.expression ? node.expression.end : c;
35+
while (source[d] !== '}') d += 1;
36+
d += 2;
37+
38+
const start = locate(c);
39+
const loc = `(${start.line + 1}:${start.column})`;
40+
41+
return `${loc} ${source.slice(c, d)}`.replace(/\n/g, ' ');
42+
}
43+
2544
// Whitespace inside one of these elements will not result in
2645
// a whitespace node being created in any circumstances. (This
2746
// list is almost certainly very incomplete)
@@ -107,6 +126,7 @@ const preprocessors = {
107126
block.addDependencies(dependencies);
108127

109128
node._block = block.child({
129+
comment: createDebuggingComment(node, generator),
110130
name: generator.getUniqueName(`create_if_block`),
111131
});
112132

@@ -127,6 +147,7 @@ const preprocessors = {
127147
attachBlocks(node.else.children[0]);
128148
} else if (node.else) {
129149
node.else._block = block.child({
150+
comment: createDebuggingComment(node.else, generator),
130151
name: generator.getUniqueName(`create_if_block`),
131152
});
132153

@@ -202,6 +223,7 @@ const preprocessors = {
202223
contextDependencies.set(node.context, dependencies);
203224

204225
node._block = block.child({
226+
comment: createDebuggingComment(node, generator),
205227
name: generator.getUniqueName('create_each_block'),
206228
expression: node.expression,
207229
context: node.context,
@@ -231,6 +253,7 @@ const preprocessors = {
231253

232254
if (node.else) {
233255
node.else._block = block.child({
256+
comment: createDebuggingComment(node.else, generator),
234257
name: generator.getUniqueName(`${node._block.name}_else`),
235258
});
236259

src/generators/server-side-rendering/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default function ssr(
103103
104104
var ${name} = {};
105105
106-
${name}.filename = ${stringify(options.filename)};
106+
${options.filename && `${name}.filename = ${stringify(options.filename)}`};
107107
108108
${name}.data = function() {
109109
return ${templateProperties.data ? `@template.data()` : `{}`};

src/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ function normalizeOptions(options: CompileOptions): CompileOptions {
1212
{
1313
generate: 'dom',
1414

15-
// a filename is necessary for sourcemap generation
16-
filename: 'SvelteComponent.html',
17-
1815
onwarn: (warning: Warning) => {
1916
if (warning.loc) {
2017
console.warn(

test/js/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe("js", () => {
7171
expectedBundle.trim().replace(/^\s+$/gm, "")
7272
);
7373
}).catch(err => {
74-
console.error(err.loc);
74+
if (err.loc) console.error(err.loc);
7575
throw err;
7676
});
7777
});

test/js/samples/collapses-text-around-comments/expected-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ var proto = {
189189
_unmount: _unmount
190190
};
191191

192+
/* generated by Svelte v1.39.3 */
193+
192194
var template = (function() {
193195
return {
194196
data: function () {

test/js/samples/collapses-text-around-comments/expected.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js";
24

35
var template = (function() {

test/js/samples/computed-collapsed-if/expected-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ var proto = {
165165
_unmount: _unmount
166166
};
167167

168+
/* generated by Svelte v1.39.3 */
169+
168170
var template = (function() {
169171
return {
170172
computed: {

test/js/samples/computed-collapsed-if/expected.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { assign, differs, init, noop, proto } from "svelte/shared.js";
24

35
var template = (function() {

test/js/samples/css-media-query/expected-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ var proto = {
185185
_unmount: _unmount
186186
};
187187

188+
/* generated by Svelte v1.39.3 */
189+
188190
function encapsulateStyles(node) {
189191
setAttribute(node, "svelte-2363328337", "");
190192
}

test/js/samples/css-media-query/expected.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { appendNode, assign, createElement, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js";
24

35
function encapsulateStyles(node) {

test/js/samples/css-shadow-dom-keyframes/expected-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ var proto = {
185185
_unmount: _unmount
186186
};
187187

188+
/* generated by Svelte v1.39.3 */
189+
188190
function create_main_fragment(state, component) {
189191
var div, text;
190192

test/js/samples/css-shadow-dom-keyframes/expected.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js";
24

35
function create_main_fragment(state, component) {

test/js/samples/each-block-changed-check/expected-bundle.js

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ var proto = {
198198
_unmount: _unmount
199199
};
200200

201+
/* generated by Svelte v1.39.3 */
202+
201203
function create_main_fragment(state, component) {
202204
var text, p, text_1;
203205

@@ -271,6 +273,7 @@ function create_main_fragment(state, component) {
271273
};
272274
}
273275

276+
// (1:0) {{#each comments as comment, i}}
274277
function create_each_block(state, each_block_value, comment, i, component) {
275278
var div, strong, text, text_1, span, text_2_value = comment.author, text_2, text_3, text_4_value = state.elapsed(comment.time, state.time), text_4, text_5, text_6, raw_value = comment.html, raw_before;
276279

test/js/samples/each-block-changed-check/expected.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { appendNode, assign, createElement, createText, destroyEach, detachAfter, detachNode, init, insertNode, noop, proto } from "svelte/shared.js";
24

35
function create_main_fragment(state, component) {
@@ -73,6 +75,7 @@ function create_main_fragment(state, component) {
7375
};
7476
}
7577

78+
// (1:0) {{#each comments as comment, i}}
7679
function create_each_block(state, each_block_value, comment, i, component) {
7780
var div, strong, text, text_1, span, text_2_value = comment.author, text_2, text_3, text_4_value = state.elapsed(comment.time, state.time), text_4, text_5, text_6, raw_value = comment.html, raw_before;
7881

test/js/samples/event-handlers-custom/expected-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ var proto = {
185185
_unmount: _unmount
186186
};
187187

188+
/* generated by Svelte v1.39.3 */
189+
188190
var template = (function() {
189191
return {
190192
methods: {

test/js/samples/event-handlers-custom/expected.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js";
24

35
var template = (function() {

test/js/samples/if-block-no-update/expected-bundle.js

+4
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ var proto = {
189189
_unmount: _unmount
190190
};
191191

192+
/* generated by Svelte v1.39.3 */
193+
192194
function create_main_fragment(state, component) {
193195
var if_block_anchor;
194196

@@ -227,6 +229,7 @@ function create_main_fragment(state, component) {
227229
};
228230
}
229231

232+
// (1:0) {{#if foo}}
230233
function create_if_block(state, component) {
231234
var p, text;
232235

@@ -249,6 +252,7 @@ function create_if_block(state, component) {
249252
};
250253
}
251254

255+
// (3:0) {{else}}
252256
function create_if_block_1(state, component) {
253257
var p, text;
254258

test/js/samples/if-block-no-update/expected.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { appendNode, assign, createComment, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js";
24

35
function create_main_fragment(state, component) {
@@ -38,6 +40,7 @@ function create_main_fragment(state, component) {
3840
};
3941
}
4042

43+
// (1:0) {{#if foo}}
4144
function create_if_block(state, component) {
4245
var p, text;
4346

@@ -60,6 +63,7 @@ function create_if_block(state, component) {
6063
};
6164
}
6265

66+
// (3:0) {{else}}
6367
function create_if_block_1(state, component) {
6468
var p, text;
6569

test/js/samples/if-block-simple/expected-bundle.js

+3
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ var proto = {
189189
_unmount: _unmount
190190
};
191191

192+
/* generated by Svelte v1.39.3 */
193+
192194
function create_main_fragment(state, component) {
193195
var if_block_anchor;
194196

@@ -230,6 +232,7 @@ function create_main_fragment(state, component) {
230232
};
231233
}
232234

235+
// (1:0) {{#if foo}}
233236
function create_if_block(state, component) {
234237
var p, text;
235238

test/js/samples/if-block-simple/expected.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { appendNode, assign, createComment, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js";
24

35
function create_main_fragment(state, component) {
@@ -41,6 +43,7 @@ function create_main_fragment(state, component) {
4143
};
4244
}
4345

46+
// (1:0) {{#if foo}}
4447
function create_if_block(state, component) {
4548
var p, text;
4649

test/js/samples/inline-style-optimized-multiple/expected-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ var proto = {
181181
_unmount: _unmount
182182
};
183183

184+
/* generated by Svelte v1.39.3 */
185+
184186
function create_main_fragment(state, component) {
185187
var div;
186188

test/js/samples/inline-style-optimized-multiple/expected.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js";
24

35
function create_main_fragment(state, component) {

test/js/samples/inline-style-optimized-url/expected-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ var proto = {
181181
_unmount: _unmount
182182
};
183183

184+
/* generated by Svelte v1.39.3 */
185+
184186
function create_main_fragment(state, component) {
185187
var div;
186188

test/js/samples/inline-style-optimized-url/expected.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js";
24

35
function create_main_fragment(state, component) {

test/js/samples/inline-style-optimized/expected-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ var proto = {
181181
_unmount: _unmount
182182
};
183183

184+
/* generated by Svelte v1.39.3 */
185+
184186
function create_main_fragment(state, component) {
185187
var div;
186188

test/js/samples/inline-style-optimized/expected.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* generated by Svelte v1.39.3 */
2+
13
import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js";
24

35
function create_main_fragment(state, component) {

test/js/samples/inline-style-unoptimized/expected-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ var proto = {
181181
_unmount: _unmount
182182
};
183183

184+
/* generated by Svelte v1.39.3 */
185+
184186
function create_main_fragment(state, component) {
185187
var div, text, div_1, div_1_style_value;
186188

0 commit comments

Comments
 (0)