From 6c26f3a95b74b1b76d17fd59df1a1a25d4080e31 Mon Sep 17 00:00:00 2001 From: indooorsman Date: Wed, 11 Jan 2023 18:09:44 +0800 Subject: [PATCH] 2.7.0 --- changelog.md | 4 ++++ lib/utils.js | 24 +++++++++++------------- package.json | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/changelog.md b/changelog.md index 56f7972..7001b8a 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +## V2.7.0 +- fix inject logic to avoid styles missing in some micro-frontend framework +- ***Important***: there's a breaking change of inject logic, before this version the inject method would be deleted once it's called, from this verson it will ***not*** be deleted anymore, for people using custom inject method please pay attention. + ## V2.6.3 - upgrade dependencies - fix #45 #46, thanks to @markdalgleish diff --git a/lib/utils.js b/lib/utils.js index b8a781b..bafb234 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -56,41 +56,39 @@ const getLogger = (build) => { const buildInjectCode = (injectToSelector = 'head', css, digest, options) => { if (typeof options.inject === 'function') { return ` -(function(){ +(function(window){ const doInject = () => { ${options.inject(css, digest)} - delete window.__inject_${digest}__; }; window.__inject_${digest}__ = doInject; -})(); +})(globalThis); `; } return ` -(function(){ +(function(win){ const css = \`${css}\`; const doInject = () => { - if (typeof document === 'undefined') { + if (typeof win.document === 'undefined') { return; } - let root = document.querySelector('${injectToSelector}'); + let root = win.document.querySelector('${injectToSelector}'); if (root && root.shadowRoot) { root = root.shadowRoot; } if (!root) { - root = document.head; + root = win.document.head; } let container = root.querySelector('#_${digest}'); if (!container) { - container = document.createElement('style'); + container = win.document.createElement('style'); container.id = '_${digest}'; + const text = win.document.createTextNode(css); + container.appendChild(text); root.appendChild(container); } - const text = document.createTextNode(css); - container.appendChild(text); - delete window.__inject_${digest}__; } - window.__inject_${digest}__ = doInject; -})(); + win.__inject_${digest}__ = doInject; +})(globalThis); `; }; diff --git a/package.json b/package.json index cf23d28..1c96f7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "esbuild-css-modules-plugin", - "version": "2.6.3", + "version": "2.7.0", "description": "A esbuild plugin to bundle css modules into js(x)/ts(x).", "main": "./index.js", "types": "./index.d.ts",