Skip to content

Commit

Permalink
fix: cjs imports from marko files in dev (#50)
Browse files Browse the repository at this point in the history
* fix: cjs imports from marko files in dev

* fix: re-export named exports

* fix: windows

* fix: update snapshot

* chore: add changeset
  • Loading branch information
rturnq authored Apr 13, 2023
1 parent 597770a commit e014eb9
Show file tree
Hide file tree
Showing 24 changed files with 529 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-panthers-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

fix: handle cjs dependencies of .marko templates
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div
id="implicit"
>
<button
class="btn btn--secondary"
data-ebayui=""
id="clickable"
type="button"
>
Mounted: false Clicks: 0
</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div
id="implicit"
>
<button
class="btn btn--secondary"
data-ebayui=""
id="clickable"
type="button"
>
Mounted: true Clicks: 0
</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div
id="implicit"
>
<button
class="btn btn--secondary"
data-ebayui=""
id="clickable"
type="button"
>
Mounted: true Clicks: 1
</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div
id="implicit"
>
<button
class="btn btn--secondary"
data-ebayui=""
id="clickable"
type="button"
>
Mounted: false Clicks: 0
</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div
id="implicit"
>
<button
class="btn btn--secondary"
data-ebayui=""
id="clickable"
type="button"
>
Mounted: true Clicks: 0
</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div
id="implicit"
>
<button
class="btn btn--secondary"
data-ebayui=""
id="clickable"
type="button"
>
Mounted: true Clicks: 1
</button>
</div>
34 changes: 34 additions & 0 deletions src/__tests__/fixtures/isomorphic-commonjs/dev-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// In dev we'll start a Vite dev server in middleware mode,
// and forward requests to our http request handler.

const { createServer } = require("vite");
const { join } = require("path");
const markoPlugin = require("../../..").default;

module.exports = (async () => {
const devServer = await createServer({
root: __dirname,
appType: "custom",
logLevel: "silent",
plugins: [markoPlugin()],
optimizeDeps: { force: true },
server: {
middlewareMode: true,
watch: {
ignored: ["**/node_modules/**", "**/dist/**", "**/__snapshots__/**"],
},
},
});
return devServer.middlewares.use(async (req, res, next) => {
try {
const { handler } = await devServer.ssrLoadModule(
join(__dirname, "./src/index.js")
);
await handler(req, res, next);
} catch (err) {
console.log(err);
devServer.ssrFixStacktrace(err);
return next(err);
}
});
})();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/__tests__/fixtures/isomorphic-commonjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "isomorphic-commonjs",
"dependencies": {
"test-package": "0.0.0"
}
}
11 changes: 11 additions & 0 deletions src/__tests__/fixtures/isomorphic-commonjs/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// In production, simply start up the http server.
const path = require("path");
const { createServer } = require("http");
const serve = require("serve-handler");
const { handler } = require("./dist/index.mjs");
const serveOpts = { public: path.resolve(__dirname, "dist") };
module.exports = createServer(async (req, res) => {
await handler(req, res);
if (res.headersSent) return;
await serve(req, res, serveOpts);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class {
onCreate() {
this.state = {
clickCount: 0,
mounted: false
};
}
onMount() {
this.state.mounted = true;
}
handleClick() {
this.state.clickCount++;
}
}

<test-button#clickable on-click("handleClick")>
Mounted: ${state.mounted}
Clicks: ${state.clickCount}
</test-button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
static {
if (typeof window === "object") {
document.body.firstElementChild.append("Loaded Implicit Component");
}
}

<div#implicit>
<class-component/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
static {
if (typeof window === "object") {
document.body.firstElementChild.append("Loaded Layout Component");
}
}

<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World</title>
</head>
<body>
<${input.renderBody}/>
</body>
</html>
Loading

0 comments on commit e014eb9

Please sign in to comment.