diff --git a/.changeset/chilly-socks-smell.md b/.changeset/chilly-socks-smell.md
new file mode 100644
index 0000000..588f12c
--- /dev/null
+++ b/.changeset/chilly-socks-smell.md
@@ -0,0 +1,5 @@
+---
+"@marko/vite": patch
+---
+
+Allow importing assets from marko file in cjs libs
diff --git a/package-lock.json b/package-lock.json
index e564f92..684af1f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -19,6 +19,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",
@@ -851,6 +852,30 @@
"url": "https://github.com/prettier/prettier?sponsor=1"
}
},
+ "node_modules/@chialab/cjs-to-esm": {
+ "version": "0.18.0",
+ "resolved": "https://registry.npmjs.org/@chialab/cjs-to-esm/-/cjs-to-esm-0.18.0.tgz",
+ "integrity": "sha512-fm8X9NhPO5pyUB7gxOZgwxb8lVq1UD4syDJCpqh6x4zGME6RTck7BguWZ4Zgv3GML4fQ4KZtyRwP5eoDgNGrmA==",
+ "dev": true,
+ "dependencies": {
+ "@chialab/estransform": "^0.18.0"
+ },
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@chialab/estransform": {
+ "version": "0.18.1",
+ "resolved": "https://registry.npmjs.org/@chialab/estransform/-/estransform-0.18.1.tgz",
+ "integrity": "sha512-W/WmjpQL2hndD0/XfR0FcPBAUj+aLNeoAVehOjV/Q9bSnioz0GVSAXXhzp59S33ZynxJBBfn8DNiMTVNJmk4Aw==",
+ "dev": true,
+ "dependencies": {
+ "@parcel/source-map": "^2.0.0"
+ },
+ "engines": {
+ "node": ">=18"
+ }
+ },
"node_modules/@esbuild/aix-ppc64": {
"version": "0.20.1",
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.20.1.tgz",
@@ -1721,6 +1746,18 @@
"node": ">= 8"
}
},
+ "node_modules/@parcel/source-map": {
+ "version": "2.1.1",
+ "resolved": "https://registry.npmjs.org/@parcel/source-map/-/source-map-2.1.1.tgz",
+ "integrity": "sha512-Ejx1P/mj+kMjQb8/y5XxDUn4reGdr+WyKYloBljpppUy8gs42T+BNoEOuRYqDVdgPc6NxduzIDoJS9pOFfV5Ew==",
+ "dev": true,
+ "dependencies": {
+ "detect-libc": "^1.0.3"
+ },
+ "engines": {
+ "node": "^12.18.3 || >=14"
+ }
+ },
"node_modules/@rollup/rollup-android-arm-eabi": {
"version": "4.12.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.12.0.tgz",
@@ -3470,6 +3507,18 @@
"node": ">=8"
}
},
+ "node_modules/detect-libc": {
+ "version": "1.0.3",
+ "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz",
+ "integrity": "sha512-pGjwhsmsp4kL2RTz08wcOlGN83otlqHeD/Z5T8GXZB+/YcpQ/dgo+lbU8ZsGxV0HIvqqxo9l7mqYwyYMD9bKDg==",
+ "dev": true,
+ "bin": {
+ "detect-libc": "bin/detect-libc.js"
+ },
+ "engines": {
+ "node": ">=0.10"
+ }
+ },
"node_modules/detect-newline": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz",
diff --git a/package.json b/package.json
index 899426c..f42f2a7 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/__snapshots__/build.expected.loading.0.html b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/__snapshots__/build.expected.loading.0.html
new file mode 100644
index 0000000..1910281
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/__snapshots__/build.expected.loading.0.html
@@ -0,0 +1 @@
+foo
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/__snapshots__/dev.expected.loading.0.html b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/__snapshots__/dev.expected.loading.0.html
new file mode 100644
index 0000000..1910281
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/__snapshots__/dev.expected.loading.0.html
@@ -0,0 +1 @@
+foo
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/dev-server.mjs b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/dev-server.mjs
new file mode 100644
index 0000000..64b889d
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/dev-server.mjs
@@ -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);
+ }
+});
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/node_modules/test-package/package.json b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/node_modules/test-package/package.json
new file mode 100644
index 0000000..942bd64
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/node_modules/test-package/package.json
@@ -0,0 +1,4 @@
+{
+ "name": "test-package",
+ "version": "1.0.0"
+}
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/node_modules/test-package/sub/index.js b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/node_modules/test-package/sub/index.js
new file mode 100644
index 0000000..d00e0a2
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/node_modules/test-package/sub/index.js
@@ -0,0 +1 @@
+module.exports = 'foo';
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/node_modules/test-package/sub/package.json b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/node_modules/test-package/sub/package.json
new file mode 100644
index 0000000..675122b
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/node_modules/test-package/sub/package.json
@@ -0,0 +1,3 @@
+{
+ "sideEffects:": false
+}
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/package.json b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/package.json
new file mode 100644
index 0000000..c9890e8
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/package.json
@@ -0,0 +1,7 @@
+{
+ "name": "isomorphic-commonjs-dir-import",
+ "type": "commonjs",
+ "dependencies": {
+ "test-package": "0.0.0"
+ }
+}
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/server.mjs b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/server.mjs
new file mode 100644
index 0000000..410eee8
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/server.mjs
@@ -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);
+});
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/src/components/layout-component.marko b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/src/components/layout-component.marko
new file mode 100644
index 0000000..8d60022
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/src/components/layout-component.marko
@@ -0,0 +1,15 @@
+static {
+ if (typeof window === "object") {
+ document.body.firstElementChild.append("Loaded Layout Component");
+ }
+}
+
+
+
+
+ Hello World
+
+
+ <${input.renderBody}/>
+
+
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/src/index.js b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/src/index.js
new file mode 100644
index 0000000..d3f5422
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/src/index.js
@@ -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);
+ }
+}
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/src/template.marko b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/src/template.marko
new file mode 100644
index 0000000..e610ac9
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/src/template.marko
@@ -0,0 +1,14 @@
+// import addMonths from 'date-fns/addMonths';
+
+import sub from "test-package/sub";
+style {
+ div {
+ color: green;
+ }
+}
+
+
+
+ ${sub}
+
+
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-dir-import/test.config.ts b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/test.config.ts
new file mode 100644
index 0000000..77ab0a0
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-dir-import/test.config.ts
@@ -0,0 +1 @@
+export const ssr = true;
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/__snapshots__/build.expected.loading.0.html b/src/__tests__/fixtures/isomorphic-commonjs-svg/__snapshots__/build.expected.loading.0.html
new file mode 100644
index 0000000..4fd93c0
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/__snapshots__/build.expected.loading.0.html
@@ -0,0 +1,6 @@
+
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/__snapshots__/dev.expected.loading.0.html b/src/__tests__/fixtures/isomorphic-commonjs-svg/__snapshots__/dev.expected.loading.0.html
new file mode 100644
index 0000000..546958f
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/__snapshots__/dev.expected.loading.0.html
@@ -0,0 +1,6 @@
+
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/dev-server.mjs b/src/__tests__/fixtures/isomorphic-commonjs-svg/dev-server.mjs
new file mode 100644
index 0000000..2a1e900
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/dev-server.mjs
@@ -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);
+ }
+});
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/components/test-button/index.marko b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/components/test-button/index.marko
new file mode 100644
index 0000000..ca90f6c
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/components/test-button/index.marko
@@ -0,0 +1,3 @@
+
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/components/test-icon/icon.svg b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/components/test-icon/icon.svg
new file mode 100644
index 0000000..7189a16
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/components/test-icon/icon.svg
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/components/test-icon/index.marko b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/components/test-icon/index.marko
new file mode 100644
index 0000000..d851051
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/components/test-icon/index.marko
@@ -0,0 +1,3 @@
+import icon from "./icon.svg";
+
+
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/marko.json b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/marko.json
new file mode 100644
index 0000000..89cb29b
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/marko.json
@@ -0,0 +1,3 @@
+{
+ "tags-dir": "./components"
+}
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/package.json b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/package.json
new file mode 100644
index 0000000..6364484
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/node_modules/test-package/package.json
@@ -0,0 +1,4 @@
+{
+ "name": "test-package",
+ "type": "commonjs"
+}
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/package.json b/src/__tests__/fixtures/isomorphic-commonjs-svg/package.json
new file mode 100644
index 0000000..34738dd
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/package.json
@@ -0,0 +1,6 @@
+{
+ "name": "isomorphic-commonjs",
+ "dependencies": {
+ "test-package": "0.0.0"
+ }
+}
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/server.mjs b/src/__tests__/fixtures/isomorphic-commonjs-svg/server.mjs
new file mode 100644
index 0000000..410eee8
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/server.mjs
@@ -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);
+});
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/src/components/class-component.marko b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/components/class-component.marko
new file mode 100644
index 0000000..5dcb4ac
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/components/class-component.marko
@@ -0,0 +1,20 @@
+class {
+ onCreate() {
+ this.state = {
+ clickCount: 0,
+ mounted: false
+ };
+ }
+ onMount() {
+ this.state.mounted = true;
+ }
+
+ handleClick() {
+ this.state.clickCount++;
+ }
+}
+
+
+ Mounted: ${state.mounted}
+ Clicks: ${state.clickCount}
+
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/src/components/implicit-component.marko b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/components/implicit-component.marko
new file mode 100644
index 0000000..7d8310b
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/components/implicit-component.marko
@@ -0,0 +1,9 @@
+static {
+ if (typeof window === "object") {
+ document.body.firstElementChild.append("Loaded Implicit Component");
+ }
+}
+
+
+
+
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/src/components/layout-component.marko b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/components/layout-component.marko
new file mode 100644
index 0000000..8d60022
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/components/layout-component.marko
@@ -0,0 +1,15 @@
+static {
+ if (typeof window === "object") {
+ document.body.firstElementChild.append("Loaded Layout Component");
+ }
+}
+
+
+
+
+ Hello World
+
+
+ <${input.renderBody}/>
+
+
\ No newline at end of file
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/src/index.js b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/index.js
new file mode 100644
index 0000000..9eada66
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/index.js
@@ -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);
+ },
+ },
+ );
+ }
+}
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/src/template.marko b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/template.marko
new file mode 100644
index 0000000..3a1cdfa
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/src/template.marko
@@ -0,0 +1,3 @@
+
+
+
diff --git a/src/__tests__/fixtures/isomorphic-commonjs-svg/test.config.ts b/src/__tests__/fixtures/isomorphic-commonjs-svg/test.config.ts
new file mode 100644
index 0000000..0718846
--- /dev/null
+++ b/src/__tests__/fixtures/isomorphic-commonjs-svg/test.config.ts
@@ -0,0 +1,2 @@
+export const ssr = true;
+export async function steps() {}
diff --git a/src/__tests__/main.test.ts b/src/__tests__/main.test.ts
index abeb77a..a12a9f9 100644
--- a/src/__tests__/main.test.ts
+++ b/src/__tests__/main.test.ts
@@ -227,6 +227,18 @@ async function testPage(
(server.address() as net.AddressInfo).port
}`;
await waitForPendingRequests(page, () => page.goto(href));
+
+ const title = await page.title();
+ if (title === "Error") {
+ const error = new Error("Error in response");
+ const pre = await page.waitForSelector("pre");
+ const html = await pre.innerHTML();
+ if (html) {
+ error.stack = JSDOM.fragment(html.replace(/
/g, "\n")).textContent!;
+ }
+ throw error;
+ }
+
await page.waitForSelector("#app");
await forEachChange((html, i) =>
snap(html, { ext: `.loading.${i}.html`, dir }),
diff --git a/src/babel-plugin-cjs-interop.ts b/src/babel-plugin-cjs-interop.ts
index e5d6623..d3058f9 100644
--- a/src/babel-plugin-cjs-interop.ts
+++ b/src/babel-plugin-cjs-interop.ts
@@ -31,15 +31,17 @@ import { isCJSModule, resolve } from "./resolve";
export default function plugin(options: {
extensions: string[];
conditions: string[];
+ filter?: (path: string) => boolean;
}): PluginObj {
return {
name: "marko-import-interop",
visitor: {
ImportDeclaration(path) {
- // Skip side-effect only imports and marko imports
+ // Skip side-effect only, relative, and marko imports
if (
!path.node.specifiers.length ||
- /\.(?:mjs|marko)$|\?/.test(path.node.source.value)
+ /\.(?:mjs|marko)$|\?/.test(path.node.source.value) ||
+ options.filter?.(path.node.source.value) === false
) {
return;
}
diff --git a/src/index.ts b/src/index.ts
index 8844afd..8bc9de2 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -6,6 +6,7 @@ import path from "path";
import crypto from "crypto";
import anyMatch from "anymatch";
import { pathToFileURL } from "url";
+import { transform as cjsToEsm } from "@chialab/cjs-to-esm";
import getServerEntryTemplate from "./server-entry-template";
import {
@@ -109,8 +110,7 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
let basePathVar: string | undefined;
let baseConfig: Compiler.Config;
let ssrConfig: Compiler.Config;
- let ssrCjsBuildConfig: Compiler.Config;
- let ssrCjsServeConfig: Compiler.Config;
+ let ssrCjsConfig: Compiler.Config;
let domConfig: Compiler.Config;
let hydrateConfig: Compiler.Config;
@@ -213,13 +213,6 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
(ssrConfig as any).markoViteLinked = linked;
}
- ssrCjsServeConfig = {
- ...ssrConfig,
- ast: true,
- code: false,
- sourceMaps: false,
- };
-
domConfig = {
...baseConfig,
resolveVirtualDependency,
@@ -352,9 +345,8 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
configResolved(config) {
basePath = config.base;
- ssrCjsBuildConfig = {
+ ssrCjsConfig = {
...ssrConfig,
- //modules: 'cjs'
babelConfig: {
...ssrConfig.babelConfig,
plugins: (
@@ -366,6 +358,7 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
interopBabelPlugin({
extensions: config.resolve.extensions,
conditions: config.resolve.conditions,
+ filter: isBuild ? undefined : (path) => !/^\./.test(path),
}),
),
},
@@ -622,6 +615,13 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
}
if (!isMarkoFile(id)) {
+ if (!isBuild) {
+ const ext = path.extname(id);
+ if (ext === ".cjs" || (ext === ".js" && isCJSModule(id))) {
+ return cjsToEsm(source);
+ }
+ }
+
return null;
}
@@ -635,7 +635,7 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
const { code, map, meta } = await compiler.compile(
source,
id,
- getConfigForFileSystem(info, ssrCjsBuildConfig),
+ getConfigForFileSystem(info, ssrCjsConfig),
);
return {
@@ -643,68 +643,6 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
map,
meta: { arcSourceCode: source, arcScanIds: meta.analyzedTags },
};
- } else {
- // For Marko files in CJS packages we create a facade
- // that loads the module as commonjs.
- const { ast } = await compiler.compile(
- source,
- id,
- ssrCjsServeConfig,
- );
- let namedExports = "";
- let code = `import { createRequire } from "module";\n`;
- code += `import "@marko/compiler/register.js";\n`;
- code += `const mod = createRequire(import.meta.url)(${JSON.stringify(
- id,
- )});\n`;
-
- for (const child of ast.program.body) {
- switch (child.type) {
- case "ExportAllDeclaration":
- code += `export * from ${JSON.stringify(
- child.source.value,
- )};\n`;
- break;
- case "ExportNamedDeclaration":
- if (child.specifiers) {
- for (const specifier of child.specifiers) {
- if (specifier.exported.type === "Identifier") {
- namedExports += `${specifier.exported.name},`;
- } else {
- namedExports += `mod[${JSON.stringify(
- specifier.exported.value,
- )}] as ${specifier.exported.value},`;
- }
- }
- }
-
- if (child.declaration) {
- if ("id" in child.declaration && child.declaration.id) {
- if (child.declaration.id.type === "Identifier") {
- namedExports += `${child.declaration.id.name},`;
- } else {
- namedExports += `mod[${JSON.stringify(
- child.declaration.id.value,
- )}] as ${child.declaration.id.value},`;
- }
- }
-
- if ("declarations" in child.declaration) {
- for (const declaration of child.declaration
- .declarations) {
- if (declaration.id.type === "Identifier") {
- namedExports += `${declaration.id.name},`;
- }
- }
- }
- }
- break;
- }
- }
-
- code += `export const { ${namedExports} } = mod;\n`;
- code += `export default mod.default;\n`;
- return code;
}
}
}
@@ -715,7 +653,9 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
getConfigForFileSystem(
info,
isSSR
- ? ssrConfig
+ ? isCJSModule(id)
+ ? ssrCjsConfig
+ : ssrConfig
: query === browserEntryQuery
? hydrateConfig
: domConfig,