Skip to content

Commit 324be53

Browse files
committed
merge master -> snake_case
2 parents 80ebdc1 + 64c805b commit 324be53

File tree

27 files changed

+198
-80
lines changed

27 files changed

+198
-80
lines changed

src/compiler/compile/Component.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ export default class Component {
101101
reactive_declaration_nodes: Set<Node> = new Set();
102102
has_reactive_assignments = false;
103103
injected_reactive_declaration_vars: Set<string> = new Set();
104-
helpers: Set<string> = new Set();
104+
helpers: Map<string, string> = new Map();
105+
globals: Map<string, string> = new Map();
105106

106107
indirect_dependencies: Map<string, Set<string>> = new Map();
107108

@@ -233,8 +234,15 @@ export default class Component {
233234
}
234235

235236
helper(name: string) {
236-
this.helpers.add(name);
237-
return this.alias(name);
237+
const alias = this.alias(name)
238+
this.helpers.set(name, alias);
239+
return alias;
240+
}
241+
242+
global(name: string) {
243+
const alias = this.alias(name);
244+
this.globals.set(name, alias);
245+
return alias;
238246
}
239247

240248
generate(result: string) {
@@ -251,23 +259,29 @@ export default class Component {
251259
.replace(/__svelte:self__/g, this.name)
252260
.replace(compile_options.generate === 'ssr' ? /(@+|#+)(\w*(?:-\w*)?)/g : /(@+)(\w*(?:-\w*)?)/g, (_match: string, sigil: string, name: string) => {
253261
if (sigil === '@') {
254-
if (internal_exports.has(name)) {
255-
if (compile_options.dev && internal_exports.has(`${name}Dev`)) name = `${name}Dev`;
256-
this.helpers.add(name);
262+
if (name[0] === '_') {
263+
return this.global(name.slice(1));
264+
}
265+
266+
if (!internal_exports.has(name)) {
267+
throw new Error(`compiler error: this shouldn't happen! generated code is trying to use inexistent internal '${name}'`);
268+
}
269+
270+
if (compile_options.dev && internal_exports.has(`${name}Dev`)) {
271+
name = `${name}Dev`;
257272
}
258273

259-
return this.alias(name);
274+
return this.helper(name);
260275
}
261276

262277
return sigil.slice(1) + name;
263278
});
264279

265-
const imported_helpers = Array.from(this.helpers)
266-
.sort()
267-
.map(name => {
268-
const alias = this.alias(name);
269-
return { name, alias };
270-
});
280+
const referenced_globals = Array.from(this.globals, ([name, alias]) => name !== alias && ({ name, alias })).filter(Boolean);
281+
if (referenced_globals.length) {
282+
this.helper('globals');
283+
}
284+
const imported_helpers = Array.from(this.helpers, ([name, alias]) => ({ name, alias }));
271285

272286
const module = create_module(
273287
result,
@@ -276,6 +290,7 @@ export default class Component {
276290
banner,
277291
compile_options.sveltePath,
278292
imported_helpers,
293+
referenced_globals,
279294
this.imports,
280295
this.vars.filter(variable => variable.module && variable.export_name).map(variable => ({
281296
name: variable.name,

src/compiler/compile/create_module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ export default function create_module(
1717
banner: string,
1818
sveltePath = 'svelte',
1919
helpers: Array<{ name: string; alias: string }>,
20+
globals: Array<{ name: string; alias: string }>,
2021
imports: Node[],
2122
module_exports: Export[],
2223
source: string
2324
): string {
2425
const internal_path = `${sveltePath}/internal`;
2526

2627
if (format === 'esm') {
27-
return esm(code, name, banner, sveltePath, internal_path, helpers, imports, module_exports, source);
28+
return esm(code, name, banner, sveltePath, internal_path, helpers, globals, imports, module_exports, source);
2829
}
2930

30-
if (format === 'cjs') return cjs(code, name, banner, sveltePath, internal_path, helpers, imports, module_exports);
31+
if (format === 'cjs') return cjs(code, name, banner, sveltePath, internal_path, helpers, globals, imports, module_exports);
3132

3233
throw new Error(`options.format is invalid (must be ${list(Object.keys(wrappers))})`);
3334
}
@@ -45,13 +46,17 @@ function esm(
4546
sveltePath: string,
4647
internal_path: string,
4748
helpers: Array<{ name: string; alias: string }>,
49+
globals: Array<{ name: string; alias: string }>,
4850
imports: Node[],
4951
module_exports: Export[],
5052
source: string
5153
) {
5254
const internal_imports = helpers.length > 0 && (
5355
`import ${stringify_props(helpers.map(h => h.name === h.alias ? h.name : `${h.name} as ${h.alias}`).sort())} from ${JSON.stringify(internal_path)};`
5456
);
57+
const internal_globals = globals.length > 0 && (
58+
`const ${stringify_props(globals.map(g => `${g.name}: ${g.alias}`).sort())} = ${helpers.find(({ name }) => name === 'globals').alias};`
59+
);
5560

5661
const user_imports = imports.length > 0 && (
5762
imports
@@ -70,6 +75,7 @@ function esm(
7075
return deindent`
7176
${banner}
7277
${internal_imports}
78+
${internal_globals}
7379
${user_imports}
7480
7581
${code}
@@ -85,6 +91,7 @@ function cjs(
8591
sveltePath: string,
8692
internal_path: string,
8793
helpers: Array<{ name: string; alias: string }>,
94+
globals: Array<{ name: string; alias: string }>,
8895
imports: Node[],
8996
module_exports: Export[]
9097
) {
@@ -93,6 +100,9 @@ function cjs(
93100
const internal_imports = helpers.length > 0 && (
94101
`const ${stringify_props(declarations)} = require(${JSON.stringify(internal_path)});\n`
95102
);
103+
const internal_globals = globals.length > 0 && (
104+
`const ${stringify_props(globals.map(g => `${g.name}: ${g.alias}`).sort())} = ${helpers.find(({ name }) => name === 'globals').alias};`
105+
);
96106

97107
const requires = imports.map(node => {
98108
let lhs;
@@ -127,6 +137,7 @@ function cjs(
127137
"use strict";
128138
129139
${internal_imports}
140+
${internal_globals}
130141
${requires}
131142
132143
${code}

src/compiler/compile/render_dom/Block.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export default class Block {
164164

165165
if (parent_node) {
166166
this.builders.mount.add_line(`@append(${parent_node}, ${name});`);
167-
if (parent_node === 'document.head' && !no_detach) this.builders.destroy.add_line(`@detach(${name});`);
167+
if (parent_node === '@_document.head' && !no_detach) this.builders.destroy.add_line(`@detach(${name});`);
168168
} else {
169169
this.builders.mount.add_line(`@insert(#target, ${name}, anchor);`);
170170
if (!no_detach) this.builders.destroy.add_conditional('detaching', `@detach(${name});`);

src/compiler/compile/render_dom/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ export default function dom(
3636
`${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */` :
3737
css.code, { only_escape_at_symbol: true });
3838

39+
const add_css = component.get_unique_name('add_css');
40+
3941
if (styles && component.compile_options.css !== false && !options.customElement) {
4042
builder.add_block(deindent`
41-
function @add_css() {
43+
function ${add_css}() {
4244
var style = @element("style");
4345
style.id = '${component.stylesheet.id}-style';
4446
style.textContent = ${styles};
45-
@append(document.head, style);
47+
@append(@_document.head, style);
4648
}
4749
`);
4850
}
@@ -57,7 +59,7 @@ export default function dom(
5759

5860
if (options.dev && !options.hydratable) {
5961
block.builders.claim.add_line(
60-
'throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");'
62+
'throw new @_Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");'
6163
);
6264
}
6365

@@ -106,7 +108,7 @@ export default function dom(
106108
} else if (component.compile_options.dev) {
107109
body.push(deindent`
108110
get ${x.export_name}() {
109-
throw new Error("<${component.tag}>: Props cannot be read directly from the component instance unless compiling with 'accessors: true' or '<svelte:options accessors/>'");
111+
throw new @_Error("<${component.tag}>: Props cannot be read directly from the component instance unless compiling with 'accessors: true' or '<svelte:options accessors/>'");
110112
}
111113
`);
112114
}
@@ -122,14 +124,14 @@ export default function dom(
122124
} else if (component.compile_options.dev) {
123125
body.push(deindent`
124126
set ${x.export_name}(value) {
125-
throw new Error("<${component.tag}>: Cannot set read-only property '${x.export_name}'");
127+
throw new @_Error("<${component.tag}>: Cannot set read-only property '${x.export_name}'");
126128
}
127129
`);
128130
}
129131
} else if (component.compile_options.dev) {
130132
body.push(deindent`
131133
set ${x.export_name}(value) {
132-
throw new Error("<${component.tag}>: Props cannot be set directly on the component instance unless compiling with 'accessors: true' or '<svelte:options accessors/>'");
134+
throw new @_Error("<${component.tag}>: Props cannot be set directly on the component instance unless compiling with 'accessors: true' or '<svelte:options accessors/>'");
133135
}
134136
`);
135137
}
@@ -145,7 +147,7 @@ export default function dom(
145147
const props = ${options.customElement ? `this.attributes` : `options.props || {}`};
146148
${expected.map(prop => deindent`
147149
if (ctx.${prop.name} === undefined && !('${prop.export_name}' in props)) {
148-
console.warn("<${component.tag}> was created without expected prop '${prop.export_name}'");
150+
@_console.warn("<${component.tag}> was created without expected prop '${prop.export_name}'");
149151
}`)}
150152
`;
151153
}
@@ -402,8 +404,8 @@ export default function dom(
402404
if (component.compile_options.dev && !component.var_lookup.has('$$props') && writable_props.length) {
403405
unknown_props_check = deindent`
404406
const writable_props = [${writable_props.map(prop => `'${prop.export_name}'`).join(', ')}];
405-
Object.keys($$props).forEach(key => {
406-
if (!writable_props.includes(key) && !key.startsWith('$$')) console.warn(\`<${component.tag}> was created with unknown prop '\${key}'\`);
407+
@_Object.keys($$props).forEach(key => {
408+
if (!writable_props.includes(key) && !key.startsWith('$$')) @_console.warn(\`<${component.tag}> was created with unknown prop '\${key}'\`);
407409
});
408410
`;
409411
}
@@ -481,7 +483,7 @@ export default function dom(
481483

482484
if (component.tag != null) {
483485
builder.add_block(deindent`
484-
customElements.define("${component.tag}", ${name});
486+
@_customElements.define("${component.tag}", ${name});
485487
`);
486488
}
487489
} else {
@@ -491,7 +493,7 @@ export default function dom(
491493
class ${name} extends @${superclass} {
492494
constructor(options) {
493495
super(${options.dev && `options`});
494-
${should_add_css && `if (!document.getElementById("${component.stylesheet.id}-style")) @add_css();`}
496+
${should_add_css && `if (!@_document.getElementById("${component.stylesheet.id}-style")) ${add_css}();`}
495497
@init(this, options, ${definition}, create_fragment, ${not_equal}, ${prop_names});
496498
497499
${dev_props_check}

src/compiler/compile/render_dom/wrappers/AwaitBlock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export default class AwaitBlockWrapper extends Wrapper {
142142
this.catch.block.name && `catch: ${this.catch.block.name}`,
143143
this.then.block.name && `value: '${this.node.value}'`,
144144
this.catch.block.name && `error: '${this.node.error}'`,
145-
this.pending.block.has_outro_method && `blocks: Array(3)`
145+
this.pending.block.has_outro_method && `blocks: [,,,]`
146146
].filter(Boolean);
147147

148148
block.builders.init.add_block(deindent`
@@ -230,4 +230,4 @@ export default class AwaitBlockWrapper extends Wrapper {
230230
branch.fragment.render(branch.block, null, 'nodes');
231231
});
232232
}
233-
}
233+
}

src/compiler/compile/render_dom/wrappers/Body.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export default class BodyWrapper extends Wrapper {
1111
const snippet = handler.render(block);
1212

1313
block.builders.init.add_block(deindent`
14-
document.body.addEventListener("${handler.name}", ${snippet});
14+
@_document.body.addEventListener("${handler.name}", ${snippet});
1515
`);
1616

1717
block.builders.destroy.add_block(deindent`
18-
document.body.removeEventListener("${handler.name}", ${snippet});
18+
@_document.body.removeEventListener("${handler.name}", ${snippet});
1919
`);
2020
});
2121
}

src/compiler/compile/render_dom/wrappers/DebugTag.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ export default class DebugTagWrapper extends Wrapper {
6262
block.builders.update.add_block(deindent`
6363
if (${condition}) {
6464
const { ${ctx_identifiers} } = ctx;
65-
console.${log}({ ${logged_identifiers} });
65+
@_console.${log}({ ${logged_identifiers} });
6666
debugger;
6767
}
6868
`);
6969

7070
block.builders.create.add_block(deindent`
7171
{
7272
const { ${ctx_identifiers} } = ctx;
73-
console.${log}({ ${logged_identifiers} });
73+
@_console.${log}({ ${logged_identifiers} });
7474
debugger;
7575
}
7676
`);

src/compiler/compile/render_dom/wrappers/EachBlock.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export default class EachBlockWrapper extends Wrapper {
190190

191191
renderer.blocks.push(deindent`
192192
function ${this.vars.get_each_context}(ctx, list, i) {
193-
const child_ctx = Object.create(ctx);
193+
const child_ctx = @_Object.create(ctx);
194194
${this.context_props}
195195
return child_ctx;
196196
}
@@ -296,7 +296,7 @@ export default class EachBlockWrapper extends Wrapper {
296296
const lookup = block.get_unique_name(`${this.var}_lookup`);
297297

298298
block.add_variable(iterations, '[]');
299-
block.add_variable(lookup, `new Map()`);
299+
block.add_variable(lookup, `new @_Map()`);
300300

301301
if (this.fragment.nodes[0].is_dom_node()) {
302302
this.block.first = this.fragment.nodes[0].var;
@@ -498,7 +498,7 @@ export default class EachBlockWrapper extends Wrapper {
498498

499499
if (this.block.has_outros) {
500500
block.builders.outro.add_block(deindent`
501-
${iterations} = ${iterations}.filter(Boolean);
501+
${iterations} = ${iterations}.filter(@_Boolean);
502502
for (let #i = 0; #i < ${view_length}; #i += 1) @transition_out(${iterations}[#i]);`
503503
);
504504
}

src/compiler/compile/render_dom/wrappers/Element/Binding.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export default class BindingWrapper {
144144
case 'currentTime':
145145
case 'playbackRate':
146146
case 'volume':
147-
update_conditions.push(`!isNaN(${this.snippet})`);
147+
update_conditions.push(`!@_isNaN(${this.snippet})`);
148148
break;
149149

150150
case 'paused':

src/compiler/compile/render_dom/wrappers/Element/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export default class ElementWrapper extends Wrapper {
270270
`@append(${parent_node}, ${node});`
271271
);
272272

273-
if (parent_node === 'document.head') {
273+
if (parent_node === '@_document.head') {
274274
block.builders.destroy.add_line(`@detach(${node});`);
275275
}
276276
} else {
@@ -381,7 +381,7 @@ export default class ElementWrapper extends Wrapper {
381381
}
382382

383383
if (namespace) {
384-
return `document.createElementNS("${namespace}", "${name}")`;
384+
return `@_document.createElementNS("${namespace}", "${name}")`;
385385
}
386386

387387
return `@element("${name}")`;
@@ -467,7 +467,7 @@ export default class ElementWrapper extends Wrapper {
467467
block.builders.init.add_block(deindent`
468468
function ${handler}() {
469469
${animation_frame && deindent`
470-
cancelAnimationFrame(${animation_frame});
470+
@_cancelAnimationFrame(${animation_frame});
471471
if (!${this.var}.paused) ${animation_frame} = @raf(${handler});`}
472472
${needs_lock && `${lock} = true;`}
473473
ctx.${handler}.call(${this.var}${contextual_dependencies.size > 0 ? ', ctx' : ''});

src/compiler/compile/render_dom/wrappers/Head.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export default class HeadWrapper extends Wrapper {
3030
}
3131

3232
render(block: Block, _parent_node: string, _parent_nodes: string) {
33-
this.fragment.render(block, 'document.head', 'nodes');
33+
this.fragment.render(block, '@_document.head', 'nodes');
3434
}
3535
}

src/compiler/compile/render_dom/wrappers/IfBlock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export default class IfBlockWrapper extends Wrapper {
154154

155155
const vars = { name, anchor, if_name, has_else, has_transitions };
156156

157-
const detaching = (parent_node && parent_node !== 'document.head') ? '' : 'detaching';
157+
const detaching = (parent_node && parent_node !== '@_document.head') ? '' : 'detaching';
158158

159159
if (this.node.else) {
160160
if (has_outros) {

src/compiler/compile/render_dom/wrappers/RawMustacheTag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class RawMustacheTagWrapper extends Tag {
2222
render(block: Block, parent_node: string, parent_nodes: string) {
2323
const name = this.var;
2424

25-
const in_head = parent_node === 'document.head';
25+
const in_head = parent_node === '@_document.head';
2626
const needs_anchors = !parent_node || in_head;
2727

2828
// if in head always needs anchors

0 commit comments

Comments
 (0)