Skip to content

Commit

Permalink
feat: support Vite 6 (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
Menci authored Dec 19, 2024
1 parent a79f392 commit 16c99fe
Show file tree
Hide file tree
Showing 19 changed files with 6,924 additions and 5,158 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: windows-latest
name: NPM Publish
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
- name: Install dependencies
run: yarn --frozen-lockfile
- name: Build
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: windows-latest
strategy:
matrix:
node: ["14", "16", "18", "20"]
node: ["14", "16", "18", "20", "22"]
name: Test on Node.js ${{ matrix.node }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- name: Set yarn to ignore engines version check
Expand Down
54 changes: 38 additions & 16 deletions e2e/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/// <reference types="jest-extended" />
/* istanbul ignore file */

import path from "path";
import url from "url";
import fs from "fs";
import type { AddressInfo } from "net";

import { jest } from "@jest/globals";
import { firefox } from "playwright";
import { firefox, chromium } from "playwright";

// import type { RollupOutput } from "rollup";
type RollupOutput = any;
import type { RollupOutput } from "rollup";
import vitePluginWasm from "../src/index.js";

import express from "express";
import waitPort from "wait-port";
import mime from "mime";
import path from "path";
import url from "url";
import type { AddressInfo } from "net";
import { temporaryDirectory } from "tempy";

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

Expand All @@ -37,9 +39,15 @@ type VitePackages =
vite: typeof import("./vite5/node_modules/vite");
vitePluginLegacy: (typeof import("./vite5/node_modules/@vitejs/plugin-legacy"))["default"];
vitePluginTopLevelAwait: (typeof import("./vite5/node_modules/vite-plugin-top-level-await"))["default"];
}
| {
vite: typeof import("./vite6/node_modules/vite");
vitePluginLegacy: (typeof import("./vite6/node_modules/@vitejs/plugin-legacy"))["default"];
vitePluginTopLevelAwait: (typeof import("./vite6/node_modules/vite-plugin-top-level-await"))["default"];
};

async function buildAndStartProdServer(
tempDir: string,
vitePackages: VitePackages,
transformTopLevelAwait: boolean,
modernOnly: boolean
Expand All @@ -49,8 +57,10 @@ async function buildAndStartProdServer(
const result = await vite.build({
root: __dirname,
build: {
target: "esnext"
target: "esnext",
outDir: path.resolve(tempDir, "dist")
},
cacheDir: path.resolve(tempDir, ".vite"),
plugins: [
...(modernOnly ? [] : [vitePluginLegacy()]),
vitePluginWasm(),
Expand Down Expand Up @@ -80,7 +90,7 @@ async function buildAndStartProdServer(
if (filePath in bundle) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "*");
const contentType = mime.getType(filePath);
const contentType = mime.getType(filePath) || "application/octet-stream";
const contentTypeWithEncoding = contentType + (contentType.includes("text/") ? "; charset=utf-8" : "");
res.contentType(contentTypeWithEncoding);
res.send(bundle[filePath]);
Expand All @@ -99,12 +109,13 @@ async function buildAndStartProdServer(
return `http://127.0.0.1:${port}/`;
}

async function startDevServer(vitePackages: VitePackages): Promise<string> {
async function startDevServer(tempDir: string, vitePackages: VitePackages): Promise<string> {
const { vite } = vitePackages;

const devServer = await vite.createServer({
root: __dirname,
plugins: [vitePluginWasm()],
cacheDir: path.resolve(tempDir, ".vite"),
logLevel: "error"
});

Expand All @@ -118,12 +129,15 @@ async function startDevServer(vitePackages: VitePackages): Promise<string> {
}

async function createBrowser(modernBrowser: boolean) {
return await firefox.launch({
firefoxUserPrefs: {
// Simulate a legacy browser with ES modules support disabled
"dom.moduleScripts.enabled": modernBrowser
}
});
return modernBrowser
? await chromium.launch()
: await firefox.launch({
headless: false,
firefoxUserPrefs: {
// Simulate a legacy browser with ES modules support disabled
"dom.moduleScripts.enabled": false
}
});
}

async function runTest(
Expand All @@ -132,7 +146,15 @@ async function runTest(
transformTopLevelAwait: boolean,
modernBrowser: boolean
) {
const tempDir = temporaryDirectory();
process.on("exit", () => {
try {
fs.rmdirSync(tempDir, { recursive: true });
} catch {}
});

const server = await (devServer ? startDevServer : buildAndStartProdServer)(
tempDir,
vitePackages,
transformTopLevelAwait,
modernBrowser
Expand Down Expand Up @@ -189,7 +211,7 @@ const runTestWithRetry = async (...args: Parameters<typeof runTest>) => {
};

export function runTests(viteVersion: number, importVitePackages: () => Promise<VitePackages>) {
jest.setTimeout(60000);
jest.setTimeout(600000);

describe(`E2E test for Vite ${viteVersion}`, () => {
const nodeVersion = Number(process.versions.node.split(".")[0]);
Expand Down
7 changes: 5 additions & 2 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"license": "MIT",
"private": true,
"scripts": {
"install": "yarn --cwd vite2 && yarn --cwd vite3 && yarn --cwd vite4 && yarn --cwd vite5"
"install": "yarn --cwd vite2 && yarn --cwd vite3 && yarn --cwd vite4 && yarn --cwd vite5 && yarn --cwd vite6"
},
"dependencies": {
"tempy": "^3.1.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
diff --git a/node_modules/vite/dist/node/chunks/dep-689425f3.js b/node_modules/vite/dist/node/chunks/dep-689425f3.js
index babf8d5..019289d 100644
--- a/node_modules/vite/dist/node/chunks/dep-689425f3.js
+++ b/node_modules/vite/dist/node/chunks/dep-689425f3.js
diff --git a/node_modules/vite/dist/node/chunks/dep-0a035c79.js b/node_modules/vite/dist/node/chunks/dep-0a035c79.js
index 57ca5f3..d39bfca 100644
--- a/node_modules/vite/dist/node/chunks/dep-0a035c79.js
+++ b/node_modules/vite/dist/node/chunks/dep-0a035c79.js
@@ -38271,7 +38271,7 @@ const isModernFlag = `__VITE_IS_MODERN__`;
const preloadMethod = `__vitePreload`;
const preloadMarker = `__VITE_PRELOAD__`;
Expand Down
Loading

0 comments on commit 16c99fe

Please sign in to comment.