Skip to content

Commit b88a1bf

Browse files
authored
fix: fix not emit reload when mock deps updated, close #147 (#149)
1 parent 32c68c4 commit b88a1bf

4 files changed

Lines changed: 19 additions & 15 deletions

File tree

example/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default defineConfig(({ mode }) => ({
99
prefix: '^/api-dev/',
1010
wsPrefix: ['/socket.io'],
1111
log: 'debug',
12+
reload: true,
1213
formidableOptions: {
1314
// 配置上传资源存放目录
1415
uploadDir: path.join(process.cwd(), '/uploads'),
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { createHash } from 'node:crypto'
12
import fs, { promises as fsp } from 'node:fs'
23
import path from 'node:path'
34
import { pathToFileURL } from 'node:url'
5+
import { attempt, attemptAsync } from '@pengzhanbo/utils'
46

57
interface LoadFromCodeOptions {
68
filepath: string
@@ -17,17 +19,18 @@ export async function loadFromCode<T = any>({
1719
}: LoadFromCodeOptions): Promise<T | { [key: string]: T }> {
1820
filepath = path.resolve(cwd, filepath)
1921
const ext = isESM ? '.mjs' : '.cjs'
20-
const filepathTmp = `${filepath}.timestamp-${Date.now()}${ext}`
21-
const file = pathToFileURL(filepathTmp).toString()
22+
const filepathTmp = `${filepath}.${getHash(code)}${ext}`
2223
await fsp.writeFile(filepathTmp, code, 'utf8')
23-
try {
24-
const mod = await import(file)
25-
return mod.default || mod
26-
}
27-
finally {
28-
try {
29-
fs.unlinkSync(filepathTmp)
30-
}
31-
catch {}
32-
}
24+
const [, mod] = await attemptAsync(importDefault, String(pathToFileURL(filepathTmp)))
25+
attempt(fs.unlinkSync, filepathTmp)
26+
return mod
27+
}
28+
29+
async function importDefault(filepath: string): Promise<any> {
30+
const mod = await import(filepath)
31+
return mod.default || mod
32+
}
33+
34+
function getHash(str: string): string {
35+
return createHash('md5').update(str).digest('hex')
3336
}

vite-plugin-mock-dev-server/src/compiler/rolldown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export async function transformWithRolldown(
8585
})
8686
return {
8787
code: result.output[0].code,
88-
deps: result.output[0].imports.map(normalizePath),
88+
deps: result.output[0].moduleIds.filter(id => !id.endsWith(filename)).map(id => normalizePath(path.relative(cwd, id))),
8989
}
9090
}
9191
catch (e) {

vite-plugin-mock-dev-server/src/mockHttp/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export function createMockMiddleware(
196196

197197
if (error) {
198198
logger.error(
199-
`${ansis.red(`mock error at ${pathname}`)}\n${error}\n at body (${ansis.underline(filepath)})`,
199+
`${ansis.red(`mock error at ${pathname}`)}\n ${error}\n at body (${ansis.underline.gray(filepath)})`,
200200
logLevel,
201201
)
202202
provideResponseStatus(response, 500)
@@ -214,7 +214,7 @@ export function createMockMiddleware(
214214
logger.error(
215215
`${ansis.red(
216216
`mock error at ${pathname}`,
217-
)}\n${error}\n at response (${ansis.underline(filepath)})`,
217+
)}\n ${error}\n at response (${ansis.underline.gray(filepath)})`,
218218
logLevel,
219219
)
220220
provideResponseStatus(response, 500)

0 commit comments

Comments
 (0)