diff --git a/package.json b/package.json index 5b6141c..79da650 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,9 @@ "format": "pretty-quick", "dev": "microbundle watch", "build": "rimraf lib && microbundle --no-sourcemap --compress=false", - "dev-rust-vite": "pnpm -filter 'examples-rust-vite*' run dev", - "dev-vite-vite": "pnpm -filter 'examples-vite-vite*' run dev", - "preview-vite-vite": "pnpm -filter 'examples-vite-vite*' run preview" + "dev-rv": "pnpm -filter 'examples-rust-vite*' run dev", + "dev-vv": "pnpm -filter 'examples-vite-vite*' run dev", + "preview-vv": "pnpm -filter 'examples-vite-vite*' run preview" }, "repository": { "type": "git", diff --git a/src/utils/VirtualModule.ts b/src/utils/VirtualModule.ts index f8ad9fe..0b631f7 100644 --- a/src/utils/VirtualModule.ts +++ b/src/utils/VirtualModule.ts @@ -1,12 +1,24 @@ -import { mkdirSync, writeFile, writeFileSync } from "fs"; -import { resolve } from "pathe"; +import { existsSync, mkdirSync, writeFile, writeFileSync } from "fs"; +import { dirname, join, parse, resolve } from "pathe"; import { packageNameEncode } from "../utils/packageNameUtils"; -const nodeModulesDir = resolve("./node_modules") +const nodeModulesDir = function findNodeModulesDir(startDir = process.cwd()) { + let currentDir = startDir; + + while (currentDir !== parse(currentDir).root) { + const nodeModulesPath = join(currentDir, 'node_modules'); + if (existsSync(nodeModulesPath)) { + return nodeModulesPath; + } + currentDir = dirname(currentDir); + } + + return ""; +}() export const virtualPackageName = "__mf__virtual" -try { +if (!existsSync(resolve(nodeModulesDir, virtualPackageName))) { mkdirSync(resolve(nodeModulesDir, virtualPackageName)) -} catch (e) { } +} writeFileSync(resolve(nodeModulesDir, virtualPackageName, "empty.js"), "") writeFileSync(resolve(nodeModulesDir, virtualPackageName, "package.json"), JSON.stringify({ name: virtualPackageName,