From d61aad670e2dcab76cfde23de98f3881246b343d Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 18 Mar 2023 13:43:53 -0700 Subject: [PATCH 1/9] feat: debug build --- chompfile.toml | 4 +--- rollup.config.js | 11 ++++++----- src/es-module-shims.js | 20 +++++++++++++++----- test/test-shim-map-overrides.html | 2 -- test/test-shim.html | 2 -- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/chompfile.toml b/chompfile.toml index 70e642c0..6d4ad309 100644 --- a/chompfile.toml +++ b/chompfile.toml @@ -2,8 +2,6 @@ version = 0.1 extensions = ['chomp@0.1:footprint', 'chomp@0.1:npm'] -default-task = 'build' - [server] port = 8080 root = "." @@ -18,7 +16,7 @@ run = 'rm -rf bench/results' [[task]] name = 'build' -targets = ['dist/es-module-shims.js', 'dist/es-module-shims.wasm.js'] +targets = ['dist/es-module-shims.js', 'dist/es-module-shims.wasm.js', 'dist/es-module-shims.debug.js'] deps = ['src/*.js', 'npm:install', 'README.md'] run = 'rollup -c' diff --git a/rollup.config.js b/rollup.config.js index 5229831f..8670ffcd 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -5,17 +5,18 @@ import path from 'path'; const version = JSON.parse(fs.readFileSync('package.json')).version; export default [ - config(true), - config(false), + config(true, false), + config(false, false), + config(false, true), ]; -function config (isWasm) { +function config (isWasm, isDebug) { const name = 'es-module-shims' return { input: `src/${name}.js`, output: { - file: `dist/${name}${isWasm ? '.wasm' : ''}.js`, + file: `dist/${name}${isWasm ? '.wasm' : ''}${isDebug ? '.debug' : ''}.js`, format: 'iife', strict: false, sourcemap: false, @@ -29,7 +30,7 @@ function config (isWasm) { } }, replace({ - 'self.ESMS_DEBUG': 'false', + 'self.ESMS_DEBUG': isDebug.toString(), preventAssignment: true }), ] diff --git a/src/es-module-shims.js b/src/es-module-shims.js index a2b54ccc..c649dd90 100755 --- a/src/es-module-shims.js +++ b/src/es-module-shims.js @@ -120,7 +120,8 @@ let importMap = { imports: {}, scopes: {} }; let baselinePassthrough; const initPromise = featureDetectionPromise.then(() => { - baselinePassthrough = esmsInitOptions.polyfillEnable !== true && supportsDynamicImport && supportsImportMeta && supportsImportMaps && (!jsonModulesEnabled || supportsJsonAssertions) && (!cssModulesEnabled || supportsCssAssertions) && !importMapSrcOrLazy && !self.ESMS_DEBUG; + baselinePassthrough = esmsInitOptions.polyfillEnable !== true && supportsDynamicImport && supportsImportMeta && supportsImportMaps && (!jsonModulesEnabled || supportsJsonAssertions) && (!cssModulesEnabled || supportsCssAssertions) && !importMapSrcOrLazy; + if (self.ESMS_DEBUG) console.info(`es-module-shims: init ${shimMode ? 'shim mode' : 'polyfill mode'}, ${baselinePassthrough ? 'baseline passthrough' : 'polyfill engaged'}`); if (hasDocument) { if (!supportsImportMaps) { const supports = HTMLScriptElement.supports || (type => type === 'classic' || type === 'module'); @@ -174,6 +175,7 @@ async function topLevelLoad (url, fetchOpts, source, nativelyLoaded, lastStaticL if (importHook) await importHook(url, typeof fetchOpts !== 'string' ? fetchOpts : {}, ''); // early analysis opt-out - no need to even fetch if we have feature support if (!shimMode && baselinePassthrough) { + if (self.ESMS_DEBUG) console.info(`es-module-shims: load skipping polyfill due to baseline passthrough applying: ${url}`); // for polyfill case, only dynamic import needs a return value here, and dynamic import will never pass nativelyLoaded if (nativelyLoaded) return null; @@ -186,7 +188,7 @@ async function topLevelLoad (url, fetchOpts, source, nativelyLoaded, lastStaticL lastLoad = undefined; resolveDeps(load, seen); await lastStaticLoadPromise; - if (source && !shimMode && !load.n && !self.ESMS_DEBUG) { + if (source && !shimMode && !load.n) { if (nativelyLoaded) return; if (revokeBlobURLs) revokeObjectURLs(Object.keys(seen)); return await dynamicImport(createBlob(source), { errUrl: source }); @@ -463,6 +465,7 @@ function getOrCreateLoad (url, fetchOpts, parent, source) { } function processScriptsAndPreloads (mapsOnly = false) { + if (self.ESMS_DEBUG) console.info(`es-module-shims: processing scripts`); if (!mapsOnly) for (const link of document.querySelectorAll(shimMode ? 'link[rel=modulepreload-shim]' : 'link[rel=modulepreload]')) processPreload(link); @@ -492,8 +495,10 @@ let lastStaticLoadPromise = Promise.resolve(); let domContentLoadedCnt = 1; function domContentLoadedCheck () { - if (--domContentLoadedCnt === 0 && !noLoadEventRetriggers && (shimMode || !baselinePassthrough)) + if (--domContentLoadedCnt === 0 && !noLoadEventRetriggers && (shimMode || !baselinePassthrough)) { + if (self.ESMS_DEBUG) console.info(`es-module-shims: DOMContentLoaded refire`); document.dispatchEvent(new Event('DOMContentLoaded')); + } } // this should always trigger because we assume es-module-shims is itself a domcontentloaded requirement if (hasDocument) { @@ -505,8 +510,10 @@ if (hasDocument) { let readyStateCompleteCnt = 1; function readyStateCompleteCheck () { - if (--readyStateCompleteCnt === 0 && !noLoadEventRetriggers && (shimMode || !baselinePassthrough)) + if (--readyStateCompleteCnt === 0 && !noLoadEventRetriggers && (shimMode || !baselinePassthrough)) { + if (self.ESMS_DEBUG) console.info(`es-module-shims: readystatechange complete refire`); document.dispatchEvent(new Event('readystatechange')); + } } const hasNext = script => script.nextSibling || script.parentNode && hasNext(script.parentNode); @@ -544,12 +551,15 @@ function processScript (script, ready = readyStateCompleteCnt > 0) { const isDomContentLoadedScript = domContentLoadedCnt > 0; if (isBlockingReadyScript) readyStateCompleteCnt++; if (isDomContentLoadedScript) domContentLoadedCnt++; + if (self.ESMS_DEBUG) console.info(`es-module-shims: processing ${script.src || ''}`); const loadPromise = topLevelLoad(script.src || pageBaseUrl, getFetchOpts(script), !script.src && script.innerHTML, !shimMode, isBlockingReadyScript && lastStaticLoadPromise) .then(() => { // if the type of the script tag "module-shim", browser does not dispatch a "load" event // see https://github.com/guybedford/es-module-shims/issues/346 - if (shimMode) + if (shimMode) { + if (self.ESMS_DEBUG) console.info(`es-module-shims: load even refire ${script.src || ''}`); script.dispatchEvent(new Event('load')); + } }) .catch(throwError); if (isBlockingReadyScript) diff --git a/test/test-shim-map-overrides.html b/test/test-shim-map-overrides.html index b0b7e56e..83408bd1 100644 --- a/test/test-shim-map-overrides.html +++ b/test/test-shim-map-overrides.html @@ -25,8 +25,6 @@ shimMode: true, mapOverrides: true, }; - - window.ESMS_DEBUG = true;