Skip to content

Commit

Permalink
chore: tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 13, 2024
1 parent ab63999 commit 8c9beee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
24 changes: 15 additions & 9 deletions examples/react-server/src/features/utils/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export function vitePluginSilenceDirectiveBuildWarning(): Plugin {
};
}

//
// plugin utils
//

export function createVirtualPlugin(name: string, load: Plugin["load"]) {
name = "virtual:" + name;
return {
Expand Down Expand Up @@ -111,16 +107,26 @@ function replaceCode(
// fs utils
//

export async function traverseFiles(
export async function collectFiles(baseDir: string) {
const files: string[] = [];
await traverseFiles(baseDir, async (filepath, e) => {
if (e.isFile()) {
files.push(filepath);
}
return e.isDirectory();
});
return files;
}

async function traverseFiles(
dir: string,
callback: (filepath: string, e: fs.Dirent) => void,
onEntry: (filepath: string, e: fs.Dirent) => Promise<boolean>,
) {
const entries = await fs.promises.readdir(dir, { withFileTypes: true });
for (const e of entries) {
const filepath = path.join(e.path, e.name);
callback(filepath, e);
if (e.isDirectory()) {
await traverseFiles(filepath, callback);
if (await onEntry(filepath, e)) {
await traverseFiles(filepath, onEntry);
}
}
}
9 changes: 2 additions & 7 deletions examples/react-server/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { __global } from "./src/global";
import react from "@vitejs/plugin-react";
import { vitePluginSsrMiddleware } from "../react-ssr/vite.config";
import {
collectFiles,
createVirtualPlugin,
parseExports,
traverseFiles,
vitePluginSilenceDirectiveBuildWarning,
} from "./src/features/utils/plugin";
import fs from "node:fs";
Expand Down Expand Up @@ -263,12 +263,7 @@ function vitePluginServerAction(): PluginOption {
async function () {
tinyassert(this.environment?.name === "react-server");
tinyassert(this.environment.mode === "build");
const files: string[] = [];
await traverseFiles(path.resolve("./src"), (file, e) => {
if (e.isFile()) {
files.push(file);
}
});
const files = await collectFiles(path.resolve("./src"));
const ids: string[] = [];
for (const file of files) {
const code = await fs.promises.readFile(file, "utf-8");
Expand Down

0 comments on commit 8c9beee

Please sign in to comment.