Skip to content

Commit 3c51f87

Browse files
authored
injectEvent (#27)
1 parent 33782e9 commit 3c51f87

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ declare interface CssModulesOptions {
4242

4343
declare interface PluginOptions {
4444
inject?: boolean | string | ((css: string, digest: string) => string);
45+
injectEvent?: string;
4546
localsConvention?: CssModulesOptions['localsConvention'];
4647
generateScopedName?: CssModulesOptions['generateScopedName'];
4748
cssModulesOption?: CssModulesOptions;

lib/plugin.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ const buildCssModulesJs = async ({ fullPath, outDir, options, digest, build }) =
5858
let builtCssFullPath = null;
5959

6060
if (inject) {
61+
const injectOptions = { urlFullPathMap, injectEvent: options.injectEvent };
6162
const typeofInject = typeof inject;
6263
if (typeofInject === 'boolean') {
63-
injectCode = buildInjectCode('head', finalCssContent, digest, { urlFullPathMap });
64+
injectCode = buildInjectCode('head', finalCssContent, digest, injectOptions);
6465
} else if (typeofInject === 'string') {
65-
injectCode = buildInjectCode(inject, finalCssContent, digest, { urlFullPathMap });
66+
injectCode = buildInjectCode(inject, finalCssContent, digest, injectOptions);
6667
} else if (typeofInject === 'function') {
67-
injectCode = inject(finalCssContent, digest, { urlFullPathMap });
68+
injectCode = inject(finalCssContent, digest, injectOptions);
6869
} else {
6970
throw new Error('type of `inject` must be boolean or string or function');
7071
}

lib/utils.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const getAbsoluteUrl = (resolveDir, url) => {
3939
* @param {string} digest
4040
* @returns {string}
4141
*/
42-
const buildInjectCode = (injectToSelector = 'head', css, digest, { urlFullPathMap }) => {
42+
const buildInjectCode = (injectToSelector = 'head', css, digest, { urlFullPathMap, injectEvent }) => {
4343
const patchedPlaceholders = [];
4444
const imports = Object.keys(urlFullPathMap)
4545
.map((placeholder) => {
@@ -50,6 +50,7 @@ const buildInjectCode = (injectToSelector = 'head', css, digest, { urlFullPathMa
5050
.join('\n');
5151
return `${imports}
5252
(function(){
53+
const injectEvent = ${injectEvent ? `"${injectEvent}"` : 'undefined'}
5354
let css = \`${css}\`;
5455
${
5556
patchedPlaceholders.length
@@ -65,22 +66,31 @@ const buildInjectCode = (injectToSelector = 'head', css, digest, { urlFullPathMa
6566
}
6667
6768
const __inject = function() {
68-
let root = document.querySelector('${injectToSelector}');
69-
if (root && root.shadowRoot) {
70-
root = root.shadowRoot;
69+
const doInject = () => {
70+
let root = document.querySelector('${injectToSelector}');
71+
if (root && root.shadowRoot) {
72+
root = root.shadowRoot;
73+
}
74+
if (!root) {
75+
root = document.head;
76+
console.warn('[esbuild-css-modules-plugin]', 'can not find element \`${injectToSelector}\`, append style to', root);
77+
}
78+
if (!root.querySelector('#_${digest}')) {
79+
const el = document.createElement('style');
80+
el.id = '_${digest}';
81+
el.textContent = css;
82+
root.appendChild(el);
83+
}
7184
}
72-
if (!root) {
73-
root = document.head;
74-
console.warn('[esbuild-css-modules-plugin]', 'can not find element \`${injectToSelector}\`, append style to', root);
75-
}
76-
if (!root.querySelector('#_${digest}')) {
77-
const el = document.createElement('style');
78-
el.id = '_${digest}';
79-
el.textContent = css;
80-
root.appendChild(el);
85+
if (injectEvent) {
86+
window.addEventListener(injectEvent, function() {
87+
doInject();
88+
});
89+
} else {
90+
doInject();
8191
}
8292
}
83-
if (document.readyState !== 'interactive' && document.readyState !== 'complete') {
93+
if (!injectEvent && document.readyState !== 'interactive' && document.readyState !== 'complete') {
8494
window.addEventListener('DOMContentLoaded', function() {
8595
__inject();
8696
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esbuild-css-modules-plugin",
3-
"version": "2.2.3",
3+
"version": "2.2.4",
44
"description": "A esbuild plugin to bundle css modules into js(x)/ts(x).",
55
"main": "index.js",
66
"keywords": [

0 commit comments

Comments
 (0)