From 0b913aa33871b34668cf8d29e8e3154fe4883820 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Wed, 5 Jun 2019 19:21:16 -0400 Subject: [PATCH] import globals from helpers (#2612) (#2947) --- src/compiler/compile/render-dom/index.ts | 12 ++++++------ .../compile/render-dom/wrappers/AwaitBlock.ts | 4 ++-- .../compile/render-dom/wrappers/EachBlock.ts | 4 ++-- .../compile/render-ssr/handlers/InlineComponent.ts | 2 +- src/runtime/internal/globals.ts | 5 +++++ src/runtime/internal/index.ts | 1 + src/runtime/internal/utils.ts | 2 +- test/js/samples/debug-empty/expected.js | 3 +++ test/js/samples/debug-foo-bar-baz-things/expected.js | 3 +++ test/js/samples/debug-foo/expected.js | 3 +++ test/js/samples/debug-hoisted/expected.js | 1 + test/js/samples/deconflict-builtins/expected.js | 1 + .../dev-warning-missing-data-computed/expected.js | 3 +++ test/js/samples/each-block-array-literal/expected.js | 1 + test/js/samples/each-block-changed-check/expected.js | 1 + .../js/samples/each-block-keyed-animated/expected.js | 2 ++ test/js/samples/each-block-keyed/expected.js | 2 ++ test/runtime/index.js | 1 + 18 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 src/runtime/internal/globals.ts diff --git a/src/compiler/compile/render-dom/index.ts b/src/compiler/compile/render-dom/index.ts index 46aa705bfb..a9941b838f 100644 --- a/src/compiler/compile/render-dom/index.ts +++ b/src/compiler/compile/render-dom/index.ts @@ -57,7 +57,7 @@ export default function dom( if (options.dev && !options.hydratable) { block.builders.claim.add_line( - 'throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");' + 'throw new @Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");' ); } @@ -106,7 +106,7 @@ export default function dom( } else if (component.compile_options.dev) { body.push(deindent` get ${x.export_name}() { - throw new Error("<${component.tag}>: Props cannot be read directly from the component instance unless compiling with 'accessors: true' or ''"); + throw new @Error("<${component.tag}>: Props cannot be read directly from the component instance unless compiling with 'accessors: true' or ''"); } `); } @@ -122,14 +122,14 @@ export default function dom( } else if (component.compile_options.dev) { body.push(deindent` set ${x.export_name}(value) { - throw new Error("<${component.tag}>: Cannot set read-only property '${x.export_name}'"); + throw new @Error("<${component.tag}>: Cannot set read-only property '${x.export_name}'"); } `); } } else if (component.compile_options.dev) { body.push(deindent` set ${x.export_name}(value) { - throw new Error("<${component.tag}>: Props cannot be set directly on the component instance unless compiling with 'accessors: true' or ''"); + throw new @Error("<${component.tag}>: Props cannot be set directly on the component instance unless compiling with 'accessors: true' or ''"); } `); } @@ -145,7 +145,7 @@ export default function dom( const props = ${options.customElement ? `this.attributes` : `options.props || {}`}; ${expected.map(prop => deindent` if (ctx.${prop.name} === undefined && !('${prop.export_name}' in props)) { - console.warn("<${component.tag}> was created without expected prop '${prop.export_name}'"); + @console.warn("<${component.tag}> was created without expected prop '${prop.export_name}'"); }`)} `; } @@ -402,7 +402,7 @@ export default function dom( if (component.compile_options.dev && !component.var_lookup.has('$$props') && writable_props.length) { unknown_props_check = deindent` const writable_props = [${writable_props.map(prop => `'${prop.export_name}'`).join(', ')}]; - Object.keys($$props).forEach(key => { + @Object.keys($$props).forEach(key => { if (!writable_props.includes(key) && !key.startsWith('$$')) console.warn(\`<${component.tag}> was created with unknown prop '\${key}'\`); }); `; diff --git a/src/compiler/compile/render-dom/wrappers/AwaitBlock.ts b/src/compiler/compile/render-dom/wrappers/AwaitBlock.ts index c7af29a073..73ca3fe4f5 100644 --- a/src/compiler/compile/render-dom/wrappers/AwaitBlock.ts +++ b/src/compiler/compile/render-dom/wrappers/AwaitBlock.ts @@ -142,7 +142,7 @@ export default class AwaitBlockWrapper extends Wrapper { this.catch.block.name && `catch: ${this.catch.block.name}`, this.then.block.name && `value: '${this.node.value}'`, this.catch.block.name && `error: '${this.node.error}'`, - this.pending.block.has_outro_method && `blocks: Array(3)` + this.pending.block.has_outro_method && `blocks: [,,,]` ].filter(Boolean); block.builders.init.add_block(deindent` @@ -230,4 +230,4 @@ export default class AwaitBlockWrapper extends Wrapper { branch.fragment.render(branch.block, null, 'nodes'); }); } -} \ No newline at end of file +} diff --git a/src/compiler/compile/render-dom/wrappers/EachBlock.ts b/src/compiler/compile/render-dom/wrappers/EachBlock.ts index a1a875a12f..dddf415557 100644 --- a/src/compiler/compile/render-dom/wrappers/EachBlock.ts +++ b/src/compiler/compile/render-dom/wrappers/EachBlock.ts @@ -190,7 +190,7 @@ export default class EachBlockWrapper extends Wrapper { renderer.blocks.push(deindent` function ${this.vars.get_each_context}(ctx, list, i) { - const child_ctx = Object.create(ctx); + const child_ctx = @Object.create(ctx); ${this.context_props} return child_ctx; } @@ -296,7 +296,7 @@ export default class EachBlockWrapper extends Wrapper { const lookup = block.get_unique_name(`${this.var}_lookup`); block.add_variable(iterations, '[]'); - block.add_variable(lookup, `new Map()`); + block.add_variable(lookup, `new @Map()`); if (this.fragment.nodes[0].is_dom_node()) { this.block.first = this.fragment.nodes[0].var; diff --git a/src/compiler/compile/render-ssr/handlers/InlineComponent.ts b/src/compiler/compile/render-ssr/handlers/InlineComponent.ts index 94fdcfd434..2f407df523 100644 --- a/src/compiler/compile/render-ssr/handlers/InlineComponent.ts +++ b/src/compiler/compile/render-ssr/handlers/InlineComponent.ts @@ -52,7 +52,7 @@ export default function(node: InlineComponent, renderer: Renderer, options: Rend let props; if (uses_spread) { - props = `Object.assign(${ + props = `@Object.assign(${ node.attributes .map(attribute => { if (attribute.is_spread) { diff --git a/src/runtime/internal/globals.ts b/src/runtime/internal/globals.ts new file mode 100644 index 0000000000..91a8e4a16d --- /dev/null +++ b/src/runtime/internal/globals.ts @@ -0,0 +1,5 @@ +import { is_client } from './utils'; + +const { console, Error, Map, Object } = (is_client ? window : global) as { console, Error, Map, Object }; + +export { console, Error, Map, Object }; diff --git a/src/runtime/internal/index.ts b/src/runtime/internal/index.ts index 6487f04525..b5ef3e398e 100644 --- a/src/runtime/internal/index.ts +++ b/src/runtime/internal/index.ts @@ -1,6 +1,7 @@ export * from './animations'; export * from './await-block'; export * from './dom'; +export * from './globals'; export * from './keyed-each'; export * from './lifecycle'; export * from './loop'; diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index 152c0e79b0..5766ac26a5 100644 --- a/src/runtime/internal/utils.ts +++ b/src/runtime/internal/utils.ts @@ -90,7 +90,7 @@ export function once(fn) { } } -const is_client = typeof window !== 'undefined'; +export const is_client = typeof window !== 'undefined'; export let now: () => number = is_client ? () => window.performance.now() diff --git a/test/js/samples/debug-empty/expected.js b/test/js/samples/debug-empty/expected.js index 6f07993590..2e4f9feb13 100644 --- a/test/js/samples/debug-empty/expected.js +++ b/test/js/samples/debug-empty/expected.js @@ -1,8 +1,11 @@ /* generated by Svelte vX.Y.Z */ import { + Error, + Object, SvelteComponentDev, add_location, append, + console, detach, element, init, diff --git a/test/js/samples/debug-foo-bar-baz-things/expected.js b/test/js/samples/debug-foo-bar-baz-things/expected.js index eea35d5ba7..c083ea29c3 100644 --- a/test/js/samples/debug-foo-bar-baz-things/expected.js +++ b/test/js/samples/debug-foo-bar-baz-things/expected.js @@ -1,8 +1,11 @@ /* generated by Svelte vX.Y.Z */ import { + Error, + Object, SvelteComponentDev, add_location, append, + console, destroy_each, detach, element, diff --git a/test/js/samples/debug-foo/expected.js b/test/js/samples/debug-foo/expected.js index 5b931d9464..46096466c1 100644 --- a/test/js/samples/debug-foo/expected.js +++ b/test/js/samples/debug-foo/expected.js @@ -1,8 +1,11 @@ /* generated by Svelte vX.Y.Z */ import { + Error, + Object, SvelteComponentDev, add_location, append, + console, destroy_each, detach, element, diff --git a/test/js/samples/debug-hoisted/expected.js b/test/js/samples/debug-hoisted/expected.js index 51d8bf63a3..7f1b7535f3 100644 --- a/test/js/samples/debug-hoisted/expected.js +++ b/test/js/samples/debug-hoisted/expected.js @@ -1,5 +1,6 @@ /* generated by Svelte vX.Y.Z */ import { + Error, SvelteComponentDev, init, noop, diff --git a/test/js/samples/deconflict-builtins/expected.js b/test/js/samples/deconflict-builtins/expected.js index 3294778aa3..85f3c31597 100644 --- a/test/js/samples/deconflict-builtins/expected.js +++ b/test/js/samples/deconflict-builtins/expected.js @@ -1,5 +1,6 @@ /* generated by Svelte vX.Y.Z */ import { + Object, SvelteComponent, append, destroy_each, diff --git a/test/js/samples/dev-warning-missing-data-computed/expected.js b/test/js/samples/dev-warning-missing-data-computed/expected.js index 5c4b2ece1b..d61f8f3de3 100644 --- a/test/js/samples/dev-warning-missing-data-computed/expected.js +++ b/test/js/samples/dev-warning-missing-data-computed/expected.js @@ -1,8 +1,11 @@ /* generated by Svelte vX.Y.Z */ import { + Error, + Object, SvelteComponentDev, add_location, append, + console, detach, element, init, diff --git a/test/js/samples/each-block-array-literal/expected.js b/test/js/samples/each-block-array-literal/expected.js index 455cb2f25f..934c5cfe92 100644 --- a/test/js/samples/each-block-array-literal/expected.js +++ b/test/js/samples/each-block-array-literal/expected.js @@ -1,5 +1,6 @@ /* generated by Svelte vX.Y.Z */ import { + Object, SvelteComponent, append, destroy_each, diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index 951565bae4..4ed8756b9a 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -1,5 +1,6 @@ /* generated by Svelte vX.Y.Z */ import { + Object, SvelteComponent, append, destroy_each, diff --git a/test/js/samples/each-block-keyed-animated/expected.js b/test/js/samples/each-block-keyed-animated/expected.js index ec28e60d5f..9e777f25ee 100644 --- a/test/js/samples/each-block-keyed-animated/expected.js +++ b/test/js/samples/each-block-keyed-animated/expected.js @@ -1,5 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { + Map, + Object, SvelteComponent, append, create_animation, diff --git a/test/js/samples/each-block-keyed/expected.js b/test/js/samples/each-block-keyed/expected.js index efb58ebf6a..7ac66a09a7 100644 --- a/test/js/samples/each-block-keyed/expected.js +++ b/test/js/samples/each-block-keyed/expected.js @@ -1,5 +1,7 @@ /* generated by Svelte vX.Y.Z */ import { + Map, + Object, SvelteComponent, append, destroy_block, diff --git a/test/runtime/index.js b/test/runtime/index.js index 77879edc1f..900f3087fc 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -125,6 +125,7 @@ describe("runtime", () => { // Put things we need on window for testing window.SvelteComponent = SvelteComponent; + window.Error = global.Error; const target = window.document.querySelector("main");