Skip to content

Commit c1e35e4

Browse files
lincenyingjinmao88
andauthored
"type": "module" 下, 不存在require, 导致报错误 (#96)
"type": "module" 下, 不存在require, 导致报`require is not defined`错误 Co-authored-by: jinmao88 <[email protected]>
1 parent e9f479f commit c1e35e4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

packages/vite-plugin-mock/src/createMockServer.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ function createWatch(opt: ViteMockOptions, config: ResolvedConfig) {
133133
})
134134
}
135135

136+
137+
// clear cache
138+
function cleanRequireCache(opt: ViteMockOptions) {
139+
if (typeof require === 'undefined' || !require.cache) {
140+
return
141+
}
142+
const { absConfigPath, absMockPath } = getPath(opt)
143+
Object.keys(require.cache).forEach((file) => {
144+
if (file === absConfigPath || file.indexOf(absMockPath) > -1) {
145+
delete require.cache[file]
146+
}
147+
})
148+
}
149+
150+
136151
function parseJson(req: IncomingMessage): Promise<Recordable> {
137152
return new Promise((resolve) => {
138153
let body = ''

0 commit comments

Comments
 (0)