fix(vite): improve commonjs handling (#3336)
Fixes https://github.com/denoland/fresh/issues/3323
This commit is contained in:
@@ -6,20 +6,22 @@ import {
|
||||
FIXTURE_DIR,
|
||||
launchDevServer,
|
||||
prepareDevServer,
|
||||
spawnDevServer,
|
||||
updateFile,
|
||||
withDevServer,
|
||||
} from "./test_utils.ts";
|
||||
|
||||
const tmp = await prepareDevServer(DEMO_DIR);
|
||||
const demoServer = await spawnDevServer(tmp.dir, {
|
||||
FRESH_PUBLIC_FOO: "foobar",
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite dev - launches",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
const res = await fetch(`${address}/tests/it_works`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("it works");
|
||||
});
|
||||
const res = await fetch(`${demoServer.address()}/tests/it_works`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("it works");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
@@ -28,11 +30,9 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - serves static files",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
const res = await fetch(`${address}/test_static/foo.txt`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("it works");
|
||||
});
|
||||
const res = await fetch(`${demoServer.address()}/test_static/foo.txt`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("it works");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
@@ -41,16 +41,14 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - loads islands",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${address}/tests/island_hooks`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
await waitForText(page, "button", "count: 0");
|
||||
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "button", "count: 1");
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${demoServer.address()}/tests/island_hooks`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
await waitForText(page, "button", "count: 0");
|
||||
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "button", "count: 1");
|
||||
});
|
||||
},
|
||||
sanitizeResources: false,
|
||||
@@ -103,30 +101,28 @@ Deno.test({
|
||||
name: "vite dev - can apply HMR to islands (hooks)",
|
||||
ignore: true, // Test is very flaky
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address, dir) => {
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${address}/tests/island_hooks`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
await waitForText(page, "button", "count: 0");
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "button", "count: 1");
|
||||
|
||||
const island = path.join(
|
||||
dir,
|
||||
"islands",
|
||||
"tests",
|
||||
"CounterHooks.tsx",
|
||||
);
|
||||
await using _ = await updateFile(
|
||||
island,
|
||||
(text) => text.replace("count:", "hmr:"),
|
||||
);
|
||||
|
||||
await waitForText(page, "button", "hmr: 1");
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "button", "hmr: 2");
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${demoServer.address()}/tests/island_hooks`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
await waitForText(page, "button", "count: 0");
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "button", "count: 1");
|
||||
|
||||
const island = path.join(
|
||||
demoServer.dir,
|
||||
"islands",
|
||||
"tests",
|
||||
"CounterHooks.tsx",
|
||||
);
|
||||
await using _ = await updateFile(
|
||||
island,
|
||||
(text) => text.replace("count:", "hmr:"),
|
||||
);
|
||||
|
||||
await waitForText(page, "button", "hmr: 1");
|
||||
await page.locator("button").click();
|
||||
await waitForText(page, "button", "hmr: 2");
|
||||
});
|
||||
},
|
||||
sanitizeResources: false,
|
||||
@@ -136,13 +132,11 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - can import json in npm package",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${address}/tests/mime`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
await page.locator(".ready").wait();
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${demoServer.address()}/tests/mime`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
await page.locator(".ready").wait();
|
||||
});
|
||||
},
|
||||
sanitizeResources: false,
|
||||
@@ -152,21 +146,19 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - inline env vars",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${address}/tests/env`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
const res = await page.locator("pre").evaluate((el) =>
|
||||
// deno-lint-ignore no-explicit-any
|
||||
(el as any).textContent ?? ""
|
||||
);
|
||||
|
||||
expect(JSON.parse(res)).toEqual({ deno: "foobar", nodeEnv: "foobar" });
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${demoServer.address()}/tests/env`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
}, { FRESH_PUBLIC_FOO: "foobar" });
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
const res = await page.locator("pre").evaluate((el) =>
|
||||
// deno-lint-ignore no-explicit-any
|
||||
(el as any).textContent ?? ""
|
||||
);
|
||||
|
||||
expect(JSON.parse(res)).toEqual({ deno: "foobar", nodeEnv: "foobar" });
|
||||
});
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
@@ -175,19 +167,17 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - serves imported assets",
|
||||
fn: async () => {
|
||||
await launchDevServer(DEMO_DIR, async (address) => {
|
||||
// Vite has an internal allowlist that is refreshed when
|
||||
// pathnames are requested. It will discover valid static
|
||||
// files imported in JS once it encounters them. Therefore
|
||||
// we must request the URL that ultimately imports the
|
||||
// asset first for it to work.
|
||||
let res = await fetch(`${address}/tests/assets`);
|
||||
await res.body?.cancel();
|
||||
// Vite has an internal allowlist that is refreshed when
|
||||
// pathnames are requested. It will discover valid static
|
||||
// files imported in JS once it encounters them. Therefore
|
||||
// we must request the URL that ultimately imports the
|
||||
// asset first for it to work.
|
||||
let res = await fetch(`${demoServer.address()}/tests/assets`);
|
||||
await res.body?.cancel();
|
||||
|
||||
res = await fetch(`${address}/assets/deno-logo.png`);
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.headers.get("Content-Type")).toEqual("image/png");
|
||||
});
|
||||
res = await fetch(`${demoServer.address()}/assets/deno-logo.png`);
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.headers.get("Content-Type")).toEqual("image/png");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
@@ -232,19 +222,17 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - partial island",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${address}/tests/partial`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await page.locator(".ready").wait();
|
||||
await page.locator("a").click();
|
||||
await page.locator(".counter-hooks").wait();
|
||||
|
||||
await page.locator(".counter-hooks button").click();
|
||||
await waitForText(page, ".counter-hooks button", "count: 1");
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${demoServer.address()}/tests/partial`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await page.locator(".ready").wait();
|
||||
await page.locator("a").click();
|
||||
await page.locator(".counter-hooks").wait();
|
||||
|
||||
await page.locator(".counter-hooks button").click();
|
||||
await waitForText(page, ".counter-hooks button", "count: 1");
|
||||
});
|
||||
},
|
||||
sanitizeResources: false,
|
||||
@@ -254,11 +242,9 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - json from jsr dependency",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
const res = await fetch(`${address}/tests/dep_json`);
|
||||
const json = await res.json();
|
||||
expect(json.name).toEqual("@marvinh-test/import-json");
|
||||
});
|
||||
const res = await fetch(`${demoServer.address()}/tests/dep_json`);
|
||||
const json = await res.json();
|
||||
expect(json.name).toEqual("@marvinh-test/import-json");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
@@ -267,11 +253,9 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - import node:*",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
const res = await fetch(`${address}/tests/feed`);
|
||||
await res.body?.cancel();
|
||||
expect(res.status).toEqual(200);
|
||||
});
|
||||
const res = await fetch(`${demoServer.address()}/tests/feed`);
|
||||
await res.body?.cancel();
|
||||
expect(res.status).toEqual(200);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
@@ -280,18 +264,16 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - island css modules",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${address}/tests/css_modules`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
const color = await page
|
||||
.locator("h1")
|
||||
// deno-lint-ignore no-explicit-any
|
||||
.evaluate((el) => window.getComputedStyle(el as any).color);
|
||||
expect(color).toEqual("rgb(255, 0, 0)");
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${demoServer.address()}/tests/css_modules`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
const color = await page
|
||||
.locator("h1")
|
||||
// deno-lint-ignore no-explicit-any
|
||||
.evaluate((el) => window.getComputedStyle(el as any).color);
|
||||
expect(color).toEqual("rgb(255, 0, 0)");
|
||||
});
|
||||
},
|
||||
sanitizeResources: false,
|
||||
@@ -341,11 +323,23 @@ Deno.test({
|
||||
Deno.test({
|
||||
name: "vite dev - allow routes looking like static paths",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
const res = await fetch(`${address}/tests/api/@marvinh@infosec.exchange`);
|
||||
const text = await res.text();
|
||||
expect(text).toEqual("ok");
|
||||
});
|
||||
const res = await fetch(
|
||||
`${demoServer.address()}/tests/api/@marvinh@infosec.exchange`,
|
||||
);
|
||||
const text = await res.text();
|
||||
expect(text).toEqual("ok");
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
// issue: https://github.com/denoland/fresh/issues/3323
|
||||
Deno.test({
|
||||
name: "vite dev - npm:pg",
|
||||
fn: async () => {
|
||||
const res = await fetch(`${demoServer.address()}/tests/pg`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("<h1>pg</h1>");
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
|
||||
Reference in New Issue
Block a user