From 77f6dccef731a76dc1d7123972c2a84281efd8ca Mon Sep 17 00:00:00 2001 From: likui <2218301630@qq.com> Date: Tue, 26 May 2020 14:54:45 +0800 Subject: [PATCH 1/2] feat: skip unused json watch apply --- src/node/server/serverPluginJson.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/node/server/serverPluginJson.ts b/src/node/server/serverPluginJson.ts index ded1adfe199f32..5840a867efb045 100644 --- a/src/node/server/serverPluginJson.ts +++ b/src/node/server/serverPluginJson.ts @@ -1,14 +1,30 @@ import { ServerPlugin } from '.' import { readBody, isImportRequest } from '../utils' -export const jsonPlugin: ServerPlugin = ({ app }) => { +const usedJsonSet = new Set() + +export const jsonPlugin: ServerPlugin = ({ app, resolver, watcher }) => { app.use(async (ctx, next) => { await next() // handle .json imports // note ctx.body could be null if upstream set status to 304 - if (ctx.path.endsWith('.json') && isImportRequest(ctx) && ctx.body) { - ctx.type = 'js' - ctx.body = `export default ${await readBody(ctx.body)}` + if (ctx.path.endsWith('.json')) { + if (isImportRequest(ctx) && ctx.body) { + ctx.type = 'js' + ctx.body = `export default ${await readBody(ctx.body)}` + } + usedJsonSet.add(ctx.path) + } + }) + + watcher.on('change', (filePath) => { + if (filePath.endsWith('.json')) { + const publicPath = resolver.fileToRequest(filePath) + + // skip unused + if (!usedJsonSet.has(publicPath)) return + + watcher.handleJSReload(filePath) } }) } From b0601c33630f19ea4d98ff8524c9ebee07696fee Mon Sep 17 00:00:00 2001 From: likui <2218301630@qq.com> Date: Tue, 26 May 2020 15:20:19 +0800 Subject: [PATCH 2/2] fix: skip check for json files --- src/node/server/serverPluginHmr.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/node/server/serverPluginHmr.ts b/src/node/server/serverPluginHmr.ts index 385f9e9636dbe6..ad48ea17b822cb 100644 --- a/src/node/server/serverPluginHmr.ts +++ b/src/node/server/serverPluginHmr.ts @@ -183,6 +183,7 @@ export const hmrPlugin: ServerPlugin = ({ watcher.on('change', (file) => { if ( !( + file.endsWith('.json') || file.endsWith('.vue') || file.endsWith('.css') || cssPreprocessLangRE.test(file)