From 3660ec219fd9d973e06b318485071fe1ba448d31 Mon Sep 17 00:00:00 2001 From: Peter Skelin Date: Mon, 11 Sep 2023 17:50:42 +0300 Subject: [PATCH] build: fix dev server page refresh --- .../dev-server/custom-hot-update-plugin.js | 38 +++++++++++++++++++ vite.config.js | 8 +--- 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 packages/tools/lib/dev-server/custom-hot-update-plugin.js diff --git a/packages/tools/lib/dev-server/custom-hot-update-plugin.js b/packages/tools/lib/dev-server/custom-hot-update-plugin.js new file mode 100644 index 000000000000..7147bcfce64f --- /dev/null +++ b/packages/tools/lib/dev-server/custom-hot-update-plugin.js @@ -0,0 +1,38 @@ +const fs = require("fs"); + +/** + * A change is observed on MacOS since 13.5, where the build generates a large amount + * of JSON file that spotlight search has to index, as they are considered new files. + * + * Starting the vitejs dev server reads all of these files and this triggers the indexing. + * The indexing has a side effect of changing some file metadata (can be checked with `mdls `). + * This metadata change is changing the ctime of the file, but not the mtime. This can be checked with `stat -x + * + * Essentially only metadata is changed, not content. This should not cause a page refresh, + * but chokidar reports this change and vite refreshes the page. + * The indexing is running with a 10 second interval, so for roughtly 20 minutes vite is refreshing the page every 10 seconds + * + * This plugin checks if the file causing the refresh is a generated json file (dist/*.json) and if ctime is changed after mtime + * In that case, returing an empty array tells vitejs that a custom update will be made by the plugin, + * which is in effect ignoring the page refresh. + */ + +const customHotUpdate = async () => { + return { + name: 'custom-hot-update', + handleHotUpdate(ctx) { + // custom check for generated json files + if (ctx.file.includes("dist/") && ctx.file.endsWith(".json")) { + const stat = fs.statSync(ctx.file); + + // metadata change only + if (stat.ctime > stat.mtime) { + console.log("ignoring hot update for:", ctx.file); + return []; + } + } + } + } +}; + +module.exports = customHotUpdate; \ No newline at end of file diff --git a/vite.config.js b/vite.config.js index 6812f54c38f4..7087fe35fc26 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,6 +1,7 @@ const os = require("os"); const { defineConfig } = require('vite'); const virtualIndex = require("@ui5/webcomponents-tools/lib/dev-server/virtual-index-html-plugin.js"); +const customHotUpdate = require("@ui5/webcomponents-tools/lib/dev-server/custom-hot-update-plugin.js"); // do not refresh from .json/.html on MacOS due to false positives const ignored = os.platform() === "darwin" ? ["**/*.json", "**/*.html"] : []; @@ -10,11 +11,6 @@ module.exports = defineConfig(async () => { build: { emptyOutDir: false, }, - server: { - watch: { - ignored, - }, - }, - plugins: [await virtualIndex()], + plugins: [await virtualIndex(), customHotUpdate()], } });