Skip to content

Commit

Permalink
feat: all scripts are ES modules now
Browse files Browse the repository at this point in the history
BREAKING CHANGE: no need to write type for component scripts
  • Loading branch information
infodusha committed Jul 28, 2024
1 parent b99d785 commit 519c8d7
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 73 deletions.
2 changes: 0 additions & 2 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,3 @@ Inside script tag you can use `this` at the root level to reference the componen
console.log(this.querySelector("h1").innerText);
</script>
```

You can set `type="module"` attribute to use ES modules.
2 changes: 1 addition & 1 deletion example/components/app-root.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
</style>

<script type="module">
<script>
import { log } from "../scripts/utils.js";

log(this);
Expand Down
1 change: 0 additions & 1 deletion example/components/card.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ <h3 data-attr="header">No header</h3>
</script>

<script src="../scripts/card.js"></script>
<script src="../scripts/card.js" type="module"></script>
2 changes: 0 additions & 2 deletions src/css-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { cloneNode } from "./helpers.js";

const hostRe = /:host(\((.+)\))?/g;

export function getEncapsulatedCss(
Expand Down
27 changes: 0 additions & 27 deletions src/error-stack-modifier.ts

This file was deleted.

44 changes: 4 additions & 40 deletions src/execute-script.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ErrorStackModifier } from "./error-stack-modifier.js";
import { returnIfDefined } from "./helpers.js";

// From https://github.com/antonkc/MOR/blob/main/matchJsImports.md
Expand Down Expand Up @@ -64,11 +63,12 @@ function setContextForModuleScript(
)}\n}).call(${component});`;
}

async function executeModule(
code: string,
export async function executeScript(
element: HTMLScriptElement,
relativeTo: string,
context: Element
): Promise<CleanupFn> {
): Promise<CleanupFn | undefined> {
const code = await getCode(element, relativeTo);
const uuid = crypto.randomUUID();
window[scriptContextSymbol].set(uuid, context);
const jsCode = setContextForModuleScript(code, uuid, relativeTo);
Expand All @@ -80,39 +80,3 @@ async function executeModule(
URL.revokeObjectURL(url);
return cleanup;
}

function execute(code: string, context: Element): CleanupFn | undefined {
const fn = Function(code);
try {
const result = fn.call(context);
return typeof result === "function" ? result : undefined;
} catch (e) {
if (e instanceof Error) {
const stack = ErrorStackModifier.fromError(e);
const currentPlaceStackLength = ErrorStackModifier.current().items.length;
const cutSize = currentPlaceStackLength - 2;
const newStack = new ErrorStackModifier(stack.items.slice(0, -cutSize));
const selector = context?.tagName.toLowerCase();
if (selector) {
newStack.applyToRow((r) => r.replace("Component.eval", selector));
}
console.error(newStack.toString());
} else {
console.error(e);
}
}
return undefined;
}

export async function executeScript(
element: HTMLScriptElement,
relativeTo: string,
context: Element
): Promise<CleanupFn | undefined> {
const code = await getCode(element, relativeTo);
const isModule = element.getAttribute("type") === "module";
if (isModule) {
return await executeModule(code, relativeTo, context);
}
return execute(code, context);
}

0 comments on commit 519c8d7

Please sign in to comment.