feat: dynamically import WebComponent base only when used
This commit is contained in:
parent
0ccc321561
commit
1a04f6fd27
1 changed files with 14 additions and 6 deletions
|
@ -62,8 +62,6 @@ async function insertRegistry(
|
||||||
key.replace(`.${type}`, "")
|
key.replace(`.${type}`, "")
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(">>> availableComponents", availableComponents);
|
|
||||||
|
|
||||||
const usedCustomElements = [];
|
const usedCustomElements = [];
|
||||||
|
|
||||||
walkSync(ast, (node) => {
|
walkSync(ast, (node) => {
|
||||||
|
@ -89,19 +87,29 @@ async function insertRegistry(
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildRegistry(usedCustomElements: string[], type: "js" | "ts") {
|
async function buildRegistry(usedCustomElements: string[], type: "js" | "ts") {
|
||||||
let registryScript = `<script type='module'>
|
let registryScript = `<script type='module'>`;
|
||||||
import { WebComponent } from "https://unpkg.com/web-component-base@1.6.15/WebComponent.js";
|
|
||||||
`;
|
|
||||||
|
|
||||||
for (const name of usedCustomElements) {
|
for (const name of usedCustomElements) {
|
||||||
const content = await useStorage().getItem(
|
const content = await useStorage().getItem(
|
||||||
`assets:components:${name}.${type}`
|
`assets:components:${name}.${type}`
|
||||||
);
|
);
|
||||||
registryScript += content;
|
|
||||||
const evalStore = eval(
|
const evalStore = eval(
|
||||||
`class WebComponent {}; class HTMLElement {}; (${content.toString()})`
|
`class WebComponent {}; class HTMLElement {}; (${content.toString()})`
|
||||||
);
|
);
|
||||||
const className = new evalStore().constructor.name;
|
const className = new evalStore().constructor.name;
|
||||||
|
let baseClassImported = false;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!baseClassImported &&
|
||||||
|
content.toString().includes("extends WebComponent")
|
||||||
|
) {
|
||||||
|
const baseClassImport = `import { WebComponent } from "https://unpkg.com/web-component-base@1.6.15/WebComponent.js";`;
|
||||||
|
|
||||||
|
registryScript += baseClassImport;
|
||||||
|
baseClassImported = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
registryScript += content;
|
||||||
|
|
||||||
registryScript += `customElements.define("${name}", ${className});`;
|
registryScript += `customElements.define("${name}", ${className});`;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue