From f56eeb4092351d66c0d59af7f6079695fd4bf669 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Thu, 3 Oct 2019 18:14:42 +0200 Subject: [PATCH] fix(loader): better resourceUrl resolution --- .../component-lazy/generate-lazy-app.ts | 4 ++-- src/compiler/component-lazy/generate-system.ts | 17 +++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/compiler/component-lazy/generate-lazy-app.ts b/src/compiler/component-lazy/generate-lazy-app.ts index c10e44157eb..bfbfd62e6b8 100644 --- a/src/compiler/component-lazy/generate-lazy-app.ts +++ b/src/compiler/component-lazy/generate-lazy-app.ts @@ -137,14 +137,14 @@ function getLegacyLoader(config: d.Config) { var scriptElm = doc.createElement('script'); scriptElm.setAttribute('type', 'module'); scriptElm.src = url + '/${namespace}.esm.js'; - doc.head.appendChild(scriptElm); warn.push(scriptElm.outerHTML); + scriptElm.setAttribute('data-stencil-namespace', '${namespace}'); + doc.head.appendChild(scriptElm); scriptElm = doc.createElement('script'); scriptElm.setAttribute('nomodule', ''); scriptElm.src = url + '/${namespace}.js'; warn.push(scriptElm.outerHTML); - scriptElm.setAttribute('data-stencil-namespace', '${namespace}'); doc.head.appendChild(scriptElm); diff --git a/src/compiler/component-lazy/generate-system.ts b/src/compiler/component-lazy/generate-system.ts index 452d5179eb9..52d9dace474 100644 --- a/src/compiler/component-lazy/generate-system.ts +++ b/src/compiler/component-lazy/generate-system.ts @@ -48,20 +48,17 @@ async function getSystemLoader(config: d.Config, corePath: string, includePolyfi (function () { var doc = document; var currentScript = doc.currentScript; + + // Safari 10 support type="module" but still download and executes the nomodule script if (!currentScript || !currentScript.hasAttribute('nomodule') || !('onbeforeload' in currentScript)) { ${polyfills} - var scriptElm = doc.querySelector('script[data-stencil-namespace="${config.fsNamespace}"]'); - if (!scriptElm) { - var allScripts = doc.querySelectorAll('script'); - for (var x = allScripts.length - 1; x >= 0; x--) { - scriptElm = allScripts[x]; - if (scriptElm.src || scriptElm.hasAttribute('data-resources-url')) { - break; - } - } - } + // Figure out currentScript (for IE11, since it does not support currentScript) + var regex = /\\/${config.fsNamespace}(\\.esm)?\\.js($|\\?|#)/; + var scriptElm = currentScript || Array.from(doc.querySelectorAll('script')).find(function(s) { + return regex.test(s.src) || s.getAttribute('data-stencil-namespace') === "${config.fsNamespace}"; + }); var resourcesUrl = scriptElm ? scriptElm.getAttribute('data-resources-url') || scriptElm.src : ''; var start = function() {