vite-plugin-mock-dev-server
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -8,5 +8,5 @@ // src/mockMiddleware.ts | ||
import Debug from "debug"; | ||
import { build } from "esbuild"; | ||
import fastGlob from "fast-glob"; | ||
import { match, pathToRegexp } from "path-to-regexp"; | ||
import { transformWithEsbuild } from "vite"; | ||
@@ -26,6 +26,7 @@ // src/utils.ts | ||
const includePaths = await fastGlob(include, { cwd: process.cwd() }); | ||
const external = await getExternal(process.cwd()); | ||
const modules = /* @__PURE__ */ Object.create(null); | ||
let mockList; | ||
for (const filepath of includePaths) { | ||
modules[filepath] = await loadModule(filepath); | ||
modules[filepath] = await loadModule(filepath, external); | ||
} | ||
@@ -42,3 +43,3 @@ setupMockList(); | ||
debug("watcher add: ", filepath); | ||
modules[filepath] = await loadModule(filepath); | ||
modules[filepath] = await loadModule(filepath, external); | ||
setupMockList(); | ||
@@ -48,3 +49,3 @@ }); | ||
debug("watcher change", filepath); | ||
modules[filepath] = await loadModule(filepath); | ||
modules[filepath] = await loadModule(filepath, external); | ||
setupMockList(); | ||
@@ -174,9 +175,13 @@ }); | ||
} | ||
async function loadModule(filepath) { | ||
const ext = path.extname(filepath); | ||
if (ext === ".ts") { | ||
const tsText = await fs.readFile(filepath, "utf-8"); | ||
const { code } = await transformWithEsbuild(tsText, filepath, { | ||
async function loadModule(filepath, external) { | ||
try { | ||
const result = await build({ | ||
entryPoints: [filepath], | ||
outfile: "out.js", | ||
write: false, | ||
target: "es2020", | ||
platform: "node", | ||
bundle: true, | ||
external, | ||
metafile: true, | ||
format: "esm" | ||
@@ -191,12 +196,22 @@ }); | ||
await fs.mkdir(tempBasename, { recursive: true }); | ||
await fs.writeFile(tempFile, code, "utf8"); | ||
return await loadESModule(tempFile); | ||
await fs.writeFile(tempFile, result.outputFiles[0].text, "utf8"); | ||
const handle = await import(`${tempFile}?${Date.now()}`); | ||
if (handle && handle.default) | ||
return handle.default; | ||
return Object.keys(handle || {}).map((key) => handle[key]); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
return await loadESModule(filepath); | ||
return { url: "" }; | ||
} | ||
async function loadESModule(filepath) { | ||
const handle = await import(`${filepath}?${Date.now()}`); | ||
if (handle && handle.default) | ||
return handle.default; | ||
return Object.keys(handle || {}).map((key) => handle[key]); | ||
async function getExternal(cwd) { | ||
const filepath = path.resolve(cwd, "package.json"); | ||
const content = await fs.readFile(filepath, "utf-8"); | ||
const pkg = JSON.parse(content); | ||
const { dependencies = {}, devDependencies = {} } = pkg; | ||
const external = [ | ||
...Object.keys(dependencies), | ||
...Object.keys(devDependencies) | ||
]; | ||
return external; | ||
} | ||
@@ -203,0 +218,0 @@ function validate(request, validator) { |
{ | ||
"name": "vite-plugin-mock-dev-server", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"repository": { | ||
@@ -41,2 +41,3 @@ "type": "git", | ||
"debug": "^4.3.4", | ||
"esbuild": "^0.15.12", | ||
"fast-glob": "^3.2.12", | ||
@@ -43,0 +44,0 @@ "path-to-regexp": "^6.2.1", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31065
622
7
+ Addedesbuild@^0.15.12