Skip to content

Commit

Permalink
Merge pull request #51 from break-stuff/create-lazy-loader
Browse files Browse the repository at this point in the history
add custom element lazy-loader
  • Loading branch information
break-stuff authored Jan 6, 2024
2 parents 074cdeb + 15c0081 commit e5a45bf
Show file tree
Hide file tree
Showing 16 changed files with 1,536 additions and 173 deletions.
4 changes: 4 additions & 0 deletions demo/lit-app/custom-elements-manifest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { customElementJsxPlugin } from "custom-element-jsx-integration";
import { getTsProgram, expandTypesPlugin } from "cem-plugin-expanded-types";
import { customElementReactWrapperPlugin } from "custom-element-react-wrappers";
import { customElementVuejsPlugin } from "custom-element-vuejs-integration";
import { customElementLazyLoaderPlugin } from "custom-element-lazy-loader";

export default {
/** Globs to analyze */
Expand Down Expand Up @@ -68,5 +69,8 @@ export default {
customElementVuejsPlugin({
componentTypePath: (name, tag) => `./dist/${tag}/${name}.d.ts`,
}),
customElementLazyLoaderPlugin({
importPathTemplate: (name, tag) => `./dist/${tag}/${name}.js`,
})
],
};
88 changes: 88 additions & 0 deletions demo/lit-app/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
let observer;
let components = {
"radio-group": {
importPath: "./dist/radio-group/RadioGroup.js",
dependencies: [],
},
"radio-button": {
importPath: "./dist/radio-button/RadioButton.js",
dependencies: [],
},
};

export function updateConfig(config) {
if (config.components) {
components = { ...components, ...config.components };
}

if (config.rootElement) {
observer.disconnect();
start(config.rootElement);
}
}

async function load(root) {
const rootTagName = root instanceof Element ? root.tagName.toLowerCase() : "";
const tags = [...root.querySelectorAll(":not(:defined)")].map((el) =>
el.tagName.toLowerCase()
);
if (rootTagName.includes("-") && !customElements.get(rootTagName)) {
tags.push(rootTagName);
}
const tagsToRegister = [...new Set(tags)];
await Promise.allSettled(tagsToRegister.map((tagName) => register(tagName)));
}

function register(tagName) {
if (customElements.get(tagName)) {
cleanUp(component, tagName);
return Promise.resolve();
}

const component = components[tagName];
if (!component) {
return Promise.resolve();
}

return new Promise((resolve, reject) => {
import(component.importPath)
.then(() => {
cleanUp(component, tagName);
resolve();
})
.catch(() => {
console.error(
`Unable to load <${tagName}> from ${component.importPath}`
);
reject();
});
});
}

function cleanUp(component, tagName) {
delete components[tagName];
component.dependencies?.forEach((dependency) => {
delete components[dependency];
});

if (!Object.keys(component).length) {
observer.disconnect();
}
}

function start(root = document.body) {
observer = new MutationObserver((mutations) => {
for (const { addedNodes } of mutations) {
for (const node of addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
load(node);
}
}
}
});

load(root);
observer.observe(root, { subtree: true, childList: true });
}

start();
1 change: 1 addition & 0 deletions demo/lit-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"cem-plugin-expanded-types": "*",
"custom-element-jet-brains-integration": "*",
"custom-element-jsx-integration": "*",
"custom-element-lazy-loader": "*",
"custom-element-react-wrappers": "*",
"custom-element-solidjs-integration": "*",
"custom-element-vs-code-integration": "*",
Expand Down
Loading

0 comments on commit e5a45bf

Please sign in to comment.