Skip to content

Commit

Permalink
fix: improve cjs interop and fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Nov 19, 2024
1 parent 25fea17 commit 0ef59c7
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 145 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-nails-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

Improve\* cjs interop
2 changes: 1 addition & 1 deletion src/__tests__/fixtures/isomorphic-base-url/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const baseUrl = process.env.BASE_URL;
export default createServer(async (req, res) => {
// remove the base url from the request url
if (req.url.startsWith(baseUrl, 1)) {
req.url = req.url.slice(baseUrl.length - 1);
req.url = req.url.slice(baseUrl.length);
}

await handler(req, res);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
document.documentElement.style.visibility='hidden'
</script>
<div
id="clickable"
class="clickable"
>
Mounted: false Clicks: 0
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
document.documentElement.style.visibility='hidden'
</script>
<div
id="clickable"
class="clickable"
>
Mounted: false Clicks: 0
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
document.documentElement.style.visibility='hidden'
</script>
<div
id="clickable"
class="clickable"
>
Mounted: false Clicks: 0
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
document.documentElement.style.visibility='hidden'
</script>
<div
id="clickable"
class="clickable"
>
Mounted: false Clicks: 0
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
document.documentElement.style.visibility='hidden'
</script>
<div
id="clickable"
class="clickable"
>
Mounted: false Clicks: 0
</div>
Expand Down Expand Up @@ -40,7 +40,7 @@
document.documentElement.style.visibility='hidden'
</script>
<div
id="clickable"
class="clickable"
>
Mounted: false Clicks: 0
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div
id="clickable"
class="clickable"
>
Mounted: false Clicks: 0
</div>
Expand All @@ -14,7 +14,7 @@
type="module"
/>
<div
id="clickable"
class="clickable"
>
Mounted: false Clicks: 0
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div
id="clickable"
class="clickable"
>
Mounted: true Clicks: 0
</div>
Expand All @@ -14,7 +14,7 @@
type="module"
/>
<div
id="clickable"
class="clickable"
>
Mounted: true Clicks: 0
</div>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div
id="clickable"
class="clickable"
>
Mounted: true Clicks: 1
</div>
Expand All @@ -14,7 +14,7 @@
type="module"
/>
<div
id="clickable"
class="clickable"
>
Mounted: true Clicks: 0
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div
class="clickable"
>
Mounted: true Clicks: 1
</div>
<script
async=""
src="/src/template.marko?marko-browser-entry"
type="module"
/>
<script
async=""
src="/src/components/class-[hash].marko?marko-browser-entry"
type="module"
/>
<div
class="clickable"
>
Mounted: true Clicks: 1
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class {
}
}

<div#clickable onClick("handleClick")>
<div.clickable onClick("handleClick")>
Mounted: ${state.mounted}
Clicks: ${state.clickCount}
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const ssr = true;
export async function steps() {
await page.click("#clickable");
for (const el of await page.$$(".clickable")) {
await el.click();
}
}

This file was deleted.

This file was deleted.

47 changes: 39 additions & 8 deletions src/babel-plugin-cjs-interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,47 @@ export default function plugin(options: {
}
}

namespaceId ||= path.scope.generateUidIdentifier(
defaultImportId?.name || path.node.source.value,
const rawImport = path.scope.generateUidIdentifier(
namespaceId?.name || defaultImportId?.name || path.node.source.value,
);
path.node.specifiers = [t.importDefaultSpecifier(namespaceId)];
path.node.specifiers = [t.importNamespaceSpecifier(rawImport)];

if (defaultImportId) {
path.insertAfter(
t.variableDeclaration("const", [
t.variableDeclarator(
defaultImportId,
t.objectPattern([
t.objectProperty(t.identifier("default"), defaultImportId),
]),
t.conditionalExpression(
t.optionalMemberExpression(
namespaceId,
t.memberExpression(rawImport, t.identifier("default")),
t.identifier("__esModule"),
false,
true,
),
t.memberExpression(namespaceId, t.identifier("default")),
namespaceId,
t.memberExpression(rawImport, t.identifier("default")),
rawImport,
),
),
]),
);
}

if (namespaceId) {
path.insertAfter(
t.variableDeclaration("const", [
t.variableDeclarator(
namespaceId,
t.conditionalExpression(
t.optionalMemberExpression(
rawImport,
t.identifier("__esModule"),
false,
true,
),
rawImport,
t.memberExpression(rawImport, t.identifier("default")),
),
),
]),
Expand All @@ -118,7 +140,16 @@ export default function plugin(options: {
),
),
),
namespaceId,
t.conditionalExpression(
t.optionalMemberExpression(
rawImport,
t.identifier("__esModule"),
false,
true,
),
rawImport,
t.memberExpression(rawImport, t.identifier("default")),
),
),
]),
);
Expand Down

0 comments on commit 0ef59c7

Please sign in to comment.