diff --git a/src/runtime/internal/Component.ts b/src/runtime/internal/Component.ts index 7d2a92fa1b..7cc3098ec3 100644 --- a/src/runtime/internal/Component.ts +++ b/src/runtime/internal/Component.ts @@ -1,6 +1,7 @@ import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler'; import { current_component, set_current_component } from './lifecycle'; -import { blank_object, is_function, run, run_all, noop } from './utils'; +import { blank_object, is_function, run, run_all } from './utils'; +import { noop } from './environment'; import { children, detach } from './dom'; import { transition_in } from './transitions'; diff --git a/src/runtime/internal/animations.ts b/src/runtime/internal/animations.ts index 6dc6a446f6..d7ec335208 100644 --- a/src/runtime/internal/animations.ts +++ b/src/runtime/internal/animations.ts @@ -1,5 +1,5 @@ -import { identity as linear, noop } from './utils'; -import { now } from './environment'; +import { identity as linear } from './utils'; +import { now, noop } from './environment'; import { loop } from './loop'; import { create_rule, delete_rule } from './style_manager'; import { AnimationConfig } from '../animate'; diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index f67fd13b2d..7fdc26808b 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -1,4 +1,5 @@ import { has_prop } from "./utils"; +import { is_cors } from "./environment"; export function append(target: Node, node: Node) { target.appendChild(node); @@ -234,26 +235,6 @@ export function select_multiple_value(select) { return [].map.call(select.querySelectorAll(':checked'), option => option.__value); } -// unfortunately this can't be a constant as that wouldn't be tree-shakeable -// so we cache the result instead -let crossorigin: boolean; - -export function is_crossorigin() { - if (crossorigin === undefined) { - crossorigin = false; - - try { - if (typeof window !== 'undefined' && window.parent) { - void window.parent.document; - } - } catch (error) { - crossorigin = true; - } - } - - return crossorigin; -} - export function add_resize_listener(node: HTMLElement, fn: () => void) { const computed_style = getComputedStyle(node); const z_index = (parseInt(computed_style.zIndex) || 0) - 1; @@ -270,11 +251,9 @@ export function add_resize_listener(node: HTMLElement, fn: () => void) { iframe.setAttribute('aria-hidden', 'true'); iframe.tabIndex = -1; - const crossorigin = is_crossorigin(); - let unsubscribe: () => void; - if (crossorigin) { + if (is_cors) { iframe.src = `data:text/html,`; unsubscribe = listen(window, 'message', (event: MessageEvent) => { if (event.source === iframe.contentWindow) fn(); @@ -289,7 +268,7 @@ export function add_resize_listener(node: HTMLElement, fn: () => void) { append(node, iframe); return () => { - if (crossorigin) { + if (is_cors) { unsubscribe(); } else if (unsubscribe && iframe.contentWindow) { unsubscribe(); diff --git a/src/runtime/internal/environment.ts b/src/runtime/internal/environment.ts index 7123399180..b69a8a1e02 100644 --- a/src/runtime/internal/environment.ts +++ b/src/runtime/internal/environment.ts @@ -1,12 +1,23 @@ -import { noop } from './utils'; - -export const is_client = typeof window !== 'undefined'; - -export let now: () => number = is_client - ? () => window.performance.now() - : () => Date.now(); - -export let raf = is_client ? cb => requestAnimationFrame(cb) : noop; +export function noop() {} +export const is_browser = typeof window !== 'undefined'; +export const is_iframe = is_browser && window.self !== window.top; +export const is_cors = + is_iframe && + /*#__PURE__*/ (() => { + try { + if (window.parent) void window.parent.document; + return false; + } catch (error) { + return true; + } + })(); +export const has_Symbol = typeof Symbol === 'function'; +/* eslint-disable no-var */ +declare var global: any; +export const globals = is_browser ? window : typeof globalThis !== 'undefined' ? globalThis : global; +export const resolved_promise = Promise.resolve(); +export let now = is_browser ? window.performance.now.bind(window.performance) : Date.now.bind(Date); +export let raf = is_browser ? requestAnimationFrame : noop; // used internally for testing export function set_now(fn) { diff --git a/src/runtime/internal/globals.ts b/src/runtime/internal/globals.ts deleted file mode 100644 index b97f81ab9f..0000000000 --- a/src/runtime/internal/globals.ts +++ /dev/null @@ -1,7 +0,0 @@ -declare const global: any; - -export const globals = (typeof window !== 'undefined' - ? window - : typeof globalThis !== 'undefined' - ? globalThis - : global) as unknown as typeof globalThis; diff --git a/src/runtime/internal/index.ts b/src/runtime/internal/index.ts index e1dd2a1fcf..daeb9b1f0a 100644 --- a/src/runtime/internal/index.ts +++ b/src/runtime/internal/index.ts @@ -2,7 +2,6 @@ export * from './animations'; export * from './await_block'; export * from './dom'; export * from './environment'; -export * from './globals'; export * from './keyed_each'; export * from './lifecycle'; export * from './loop'; diff --git a/src/runtime/internal/scheduler.ts b/src/runtime/internal/scheduler.ts index b0db71035a..9d1d1c7551 100644 --- a/src/runtime/internal/scheduler.ts +++ b/src/runtime/internal/scheduler.ts @@ -1,5 +1,6 @@ import { run_all } from './utils'; import { set_current_component } from './lifecycle'; +import { resolved_promise } from './environment'; export const dirty_components = []; export const intros = { enabled: false }; @@ -8,7 +9,6 @@ export const binding_callbacks = []; const render_callbacks = []; const flush_callbacks = []; -const resolved_promise = Promise.resolve(); let update_scheduled = false; export function schedule_update() { diff --git a/src/runtime/internal/transitions.ts b/src/runtime/internal/transitions.ts index ed23d3c1dd..eaac5e21a1 100644 --- a/src/runtime/internal/transitions.ts +++ b/src/runtime/internal/transitions.ts @@ -1,5 +1,5 @@ -import { identity as linear, is_function, noop, run_all } from './utils'; -import { now } from "./environment"; +import { identity as linear, is_function, run_all } from './utils'; +import { now, noop } from "./environment"; import { loop } from './loop'; import { create_rule, delete_rule } from './style_manager'; import { custom_event } from './dom'; diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index d752c9de9d..976f5ee669 100644 --- a/src/runtime/internal/utils.ts +++ b/src/runtime/internal/utils.ts @@ -1,5 +1,4 @@ -export function noop() {} - +import { noop } from './environment'; export const identity = x => x; export function assign(tar: T, src: S): T & S {