Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle asset imports from cjs lib marko files #120

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-socks-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

Allow importing assets from marko file in cjs libs
49 changes: 49 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@chialab/cjs-to-esm": "^0.18.0",
"@marko/compiler": "^5.34.7",
"@marko/fixture-snapshots": "^2.2.1",
"@marko/testing-library": "^6.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// In dev we'll start a Vite dev server in middleware mode,
// and forward requests to our http request handler.

import { createServer } from "vite";
import path from "path";
import url from "url";
import { createRequire } from "module";

// change to import once marko-vite is updated to ESM
const markoPlugin = createRequire(import.meta.url)("../../..").default;

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

const devServer = await createServer({
root: __dirname,
appType: "custom",
logLevel: "silent",
plugins: [markoPlugin()],
optimizeDeps: { force: true },
server: {
middlewareMode: true,
watch: {
ignored: ["**/node_modules/**", "**/dist/**", "**/__snapshots__/**"],
},
},
});

export default devServer.middlewares.use(async (req, res, next) => {
try {
const { handler } = await devServer.ssrLoadModule(
path.join(__dirname, "./src/index.js")
);
await handler(req, res, next);
} catch (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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "isomorphic-commonjs-dir-import",
"type": "commonjs",
"dependencies": {
"test-package": "0.0.0"
}
}
15 changes: 15 additions & 0 deletions src/__tests__/fixtures/isomorphic-commonjs-dir-import/server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// In production, simply start up the http server.
import path from 'path'
import url from 'url';
import { createServer } from "http";
import serve from "serve-handler";
import { handler } from "./dist/index.mjs";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const serveOpts = { public: path.resolve(__dirname, "dist") };

export default 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,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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import template from "./template.marko";

export function handler(req, res) {
if (req.url === "/") {
res.statusCode = 200;
res.setHeader("Content-Type", "text/html; charset=utf-8");
template.render({}, res);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import addMonths from 'date-fns/addMonths';

import sub from "test-package/sub";
style {
div {
color: green;
}
}

<layout-component>
<div id="app">
${sub}
</div>
</layout-component>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ssr = true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<button>
<img
alt="icon"
src="/assets/icon-[hash].svg"
/>
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<button>
<img
alt="icon"
src="/node_modules/test-package/components/test-icon/icon.svg"
/>
</button>
40 changes: 40 additions & 0 deletions src/__tests__/fixtures/isomorphic-commonjs-svg/dev-server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// In dev we'll start a Vite dev server in middleware mode,
// and forward requests to our http request handler.

import { createServer } from "vite";
import path from "path";
import url from "url";
import { createRequire } from "module";

// change to import once marko-vite is updated to ESM
const markoPlugin = createRequire(import.meta.url)("../../..").default;

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

const devServer = await createServer({
root: __dirname,
appType: "custom",
logLevel: "silent",
plugins: [markoPlugin()],
optimizeDeps: { force: true },
server: {
middlewareMode: true,
watch: {
ignored: ["**/node_modules/**", "**/dist/**", "**/__snapshots__/**"],
},
},
});

export default devServer.middlewares.use(async (req, res, next) => {
try {
const { handler } = await devServer.ssrLoadModule(
path.join(__dirname, "./src/index.js"),
);
await handler(req, res, next);
} catch (err) {
try {
devServer.ssrFixStacktrace(err);
} catch (_) {}
return next(err);
}
});

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

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-svg/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"
}
}
15 changes: 15 additions & 0 deletions src/__tests__/fixtures/isomorphic-commonjs-svg/server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// In production, simply start up the http server.
import path from 'path'
import url from 'url';
import { createServer } from "http";
import serve from "serve-handler";
import { handler } from "./dist/index.mjs";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const serveOpts = { public: path.resolve(__dirname, "dist") };

export default 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>
19 changes: 19 additions & 0 deletions src/__tests__/fixtures/isomorphic-commonjs-svg/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import template from "./template.marko";

export function handler(req, res) {
if (req.url === "/") {
res.statusCode = 200;
res.setHeader("Content-Type", "text/html; charset=utf-8");
template.render(
{},
{
write(chunk) {
res.write(chunk);
},
end(chunk) {
res.end(chunk);
},
},
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div id="app">
<test-button/>
</div>
2 changes: 2 additions & 0 deletions src/__tests__/fixtures/isomorphic-commonjs-svg/test.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ssr = true;
export async function steps() {}
Loading
Loading