-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow modulepreloads to have asset base (#56)
* 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
Showing
26 changed files
with
332 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@marko/vite": patch | ||
--- | ||
|
||
fix: preload base path |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/__tests__/fixtures/isomorphic-modulepreload/__snapshots__/build.expected.loading.0.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
12 changes: 12 additions & 0 deletions
12
src/__tests__/fixtures/isomorphic-modulepreload/__snapshots__/build.expected.loading.1.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
12 changes: 12 additions & 0 deletions
12
src/__tests__/fixtures/isomorphic-modulepreload/__snapshots__/build.expected.step-0.0.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
src/__tests__/fixtures/isomorphic-modulepreload/__snapshots__/dev.expected.loading.0.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
src/__tests__/fixtures/isomorphic-modulepreload/__snapshots__/dev.expected.loading.1.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
src/__tests__/fixtures/isomorphic-modulepreload/__snapshots__/dev.expected.step-0.0.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/__tests__/fixtures/isomorphic-modulepreload/dev-server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
25 changes: 25 additions & 0 deletions
25
src/__tests__/fixtures/isomorphic-modulepreload/src/components/class-component.marko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
src/__tests__/fixtures/isomorphic-modulepreload/src/components/implicit-component.marko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
15 changes: 15 additions & 0 deletions
15
src/__tests__/fixtures/isomorphic-modulepreload/src/components/layout-component.marko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
72 changes: 72 additions & 0 deletions
72
src/__tests__/fixtures/isomorphic-modulepreload/src/components/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions
14
src/__tests__/fixtures/isomorphic-modulepreload/src/components/read-head.marko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/__tests__/fixtures/isomorphic-modulepreload/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/__tests__/fixtures/isomorphic-modulepreload/src/template.marko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
src/__tests__/fixtures/isomorphic-modulepreload/test.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}; |
9 changes: 9 additions & 0 deletions
9
...tests__/fixtures/isomorphic-runtime-base-path/__snapshots__/build.expected.loading.1.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
..._tests__/fixtures/isomorphic-runtime-base-path/__snapshots__/build.expected.step-0.0.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.