Skip to content

Commit

Permalink
fix: allow modulepreloads to have asset base (#56)
Browse files Browse the repository at this point in the history
* fix: allow modulepreloads to have asset base

* chore: update mocha-snap

* fix: ensure runtime ids match

* chore: add test

* chore: add changeset
  • Loading branch information
rturnq authored May 4, 2023
1 parent c8e7cac commit b8c6e7e
Show file tree
Hide file tree
Showing 26 changed files with 332 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-games-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

fix: preload base path
11 changes: 7 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"lint-staged": "^13.1.0",
"marko": "^5.22.0",
"mocha": "^10.2.0",
"mocha-snap": "^4.3.0",
"mocha-snap": "^5.0.0",
"nyc": "^15.1.0",
"playwright": "^1.29.1",
"prettier": "^2.8.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div
id="implicit"
>
<div
id="clickable"
>
Mounted: false Clicks: 0 LOGO_PATH: /my-prefix/assets/logo-[hash].svg ENV: /
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div
id="implicit"
>
<div
id="clickable"
>
Mounted: true Clicks: 0 LOGO_PATH: /my-prefix/assets/logo-[hash].svg ENV: /
</div>
</div>
<div>
PRELOAD: /my-prefix/assets/read-head-[hash].js
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div
id="implicit"
>
<div
id="clickable"
>
Mounted: true Clicks: 1 LOGO_PATH: /my-prefix/assets/logo-[hash].svg ENV: /
</div>
</div>
<div>
PRELOAD: /my-prefix/assets/read-head-[hash].js
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div
id="implicit"
>
<div
id="clickable"
>
Mounted: false Clicks: 0 LOGO_PATH: /src/components/logo.svg ENV: /
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div
id="implicit"
>
<div
id="clickable"
>
Mounted: true Clicks: 0 LOGO_PATH: /src/components/logo.svg ENV: /
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div
id="implicit"
>
<div
id="clickable"
>
Mounted: true Clicks: 1 LOGO_PATH: /src/components/logo.svg ENV: /
</div>
</div>
33 changes: 33 additions & 0 deletions src/__tests__/fixtures/isomorphic-modulepreload/dev-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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) {
devServer.ssrFixStacktrace(err);
return next(err);
}
});
})();
17 changes: 17 additions & 0 deletions src/__tests__/fixtures/isomorphic-modulepreload/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// In production, simply start up the http server.
globalThis.assetsPath = "/my-prefix/";

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;
if (req.url.startsWith("/my-prefix/")) {
req.url = req.url.replace("/my-prefix", "");
}
await serve(req, res, serveOpts);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logo from "./logo.svg";

class {
onCreate() {
this.state = {
clickCount: 0,
mounted: false
};
}
onMount() {
this.state.mounted = true;
}
handleClick() {
this.state.clickCount++;
}
}

<div#clickable onClick("handleClick")>
Mounted: ${state.mounted}
Clicks: ${state.clickCount}

LOGO_PATH: ${logo.replace(/-[a-z0-9]+(\.\w+)$/i, '-[hash]$1')}
ENV: ${import.meta.env.BASE_URL}
</div>
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class {
onCreate() {
this.state = {
modulePreloads: []
};
}
onMount() {
this.state.modulePreloads = document.querySelectorAll('link[rel="modulepreload"]');
}
}

<for|link| of=state.modulePreloads>
<div>PRELOAD: ${new URL(link.href).pathname.replace(/-[a-z0-9]+(\.\w+)$/i, '-[hash]$1')}</div>
</for>
10 changes: 10 additions & 0 deletions src/__tests__/fixtures/isomorphic-modulepreload/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import template from "./template.marko";
import "./components/read-head.marko";

export function handler(req, res) {
if (req.url === "/") {
res.statusCode = 200;
res.setHeader("Content-Type", "text/html; charset=utf-8");
template.render({}, res);
}
}
13 changes: 13 additions & 0 deletions src/__tests__/fixtures/isomorphic-modulepreload/src/template.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
style {
div {
color: green;
}
}

<layout-component>
<div id="app">
<implicit-component/>
<read-head />
<!-- <${input.component}/> -->
</div>
</layout-component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Options } from "../../..";

export const ssr = true;
export async function steps() {
await page.click("#clickable");
}
export const options: Options = {
basePathVar: "assetsPath",
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div
id="implicit"
>
<div
id="clickable"
>
Mounted: true Clicks: 0 LOGO_PATH: /my-prefix/assets/logo-[hash].svg ENV: /
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div
id="implicit"
>
<div
id="clickable"
>
Mounted: true Clicks: 1 LOGO_PATH: /my-prefix/assets/logo-[hash].svg ENV: /
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ const serveOpts = { public: path.resolve(__dirname, "dist") };
module.exports = createServer(async (req, res) => {
await handler(req, res);
if (res.headersSent) return;

if (req.url.startsWith("/my-prefix/")) {
req.url = req.url.replace("/my-prefix", "");
res.statusCode = 200;
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end("Hello World");
await serve(req, res, serveOpts);
} else {
res.statusCode = 404;
res.end("Not Found");
}
await serve(req, res, serveOpts);
});
Loading

0 comments on commit b8c6e7e

Please sign in to comment.