From fb6a285e4bbc7a8be7595b01f2833a3e3788a4cd Mon Sep 17 00:00:00 2001 From: Em Zhan Date: Mon, 22 Aug 2022 15:50:10 -0500 Subject: [PATCH] Support HMR for GLSL files Now edits to GLSL files will cause dev server to automatically update the page, as expected. Previously only worked for the GLSL files directly imported by JS. Done by patching dependencies; should try to get some version of these changes merged upstream. See https://github.com/vitejs/vite/pull/9723 --- patches/glslify+7.1.1.patch | 13 ++++++++ patches/rollup-plugin-glslify+1.3.0.patch | 27 ++++++++++++++++ patches/vite+3.0.9.patch | 38 ++++++++++++++++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 patches/glslify+7.1.1.patch create mode 100644 patches/rollup-plugin-glslify+1.3.0.patch diff --git a/patches/glslify+7.1.1.patch b/patches/glslify+7.1.1.patch new file mode 100644 index 00000000..30d1ead3 --- /dev/null +++ b/patches/glslify+7.1.1.patch @@ -0,0 +1,13 @@ +diff --git a/node_modules/glslify/index.js b/node_modules/glslify/index.js +index 7bb9ede..3942421 100644 +--- a/node_modules/glslify/index.js ++++ b/node_modules/glslify/index.js +@@ -45,7 +45,7 @@ function iface () { + if (!opts) opts = {} + var depper = gdeps(opts) + var deps = depper.inline(src, opts.basedir || basedir) +- return bundle(deps) ++ return { source: bundle(deps), deps } + } + function file(filename, opts) { + if (!opts) opts = {} diff --git a/patches/rollup-plugin-glslify+1.3.0.patch b/patches/rollup-plugin-glslify+1.3.0.patch new file mode 100644 index 00000000..6e663a45 --- /dev/null +++ b/patches/rollup-plugin-glslify+1.3.0.patch @@ -0,0 +1,27 @@ +diff --git a/node_modules/rollup-plugin-glslify/index.js b/node_modules/rollup-plugin-glslify/index.js +index 85474ec..2c1e423 100644 +--- a/node_modules/rollup-plugin-glslify/index.js ++++ b/node_modules/rollup-plugin-glslify/index.js +@@ -58,7 +58,12 @@ module.exports = function glslify(userOptions = {}) { + options + ); + +- code = compile(code, fileOptions); ++ const { source, deps } = compile(code, fileOptions); ++ code = source; ++ ++ for (const dep of deps.filter(dep => !dep.entry)) { ++ this.addWatchFile(dep.file); ++ } + + if (typeof options.compress === 'function') { + code = options.compress(code); +@@ -67,7 +72,7 @@ module.exports = function glslify(userOptions = {}) { + } + + return { +- code: `export default ${JSON.stringify(code)}; // eslint-disable-line`, ++ code: `export default ${JSON.stringify(source)}; // eslint-disable-line`, + map: { mappings: '' } + }; + } diff --git a/patches/vite+3.0.9.patch b/patches/vite+3.0.9.patch index 880bb480..903b51d9 100644 --- a/patches/vite+3.0.9.patch +++ b/patches/vite+3.0.9.patch @@ -1,5 +1,5 @@ diff --git a/node_modules/vite/dist/node/chunks/dep-0fc8e132.js b/node_modules/vite/dist/node/chunks/dep-0fc8e132.js -index 54ffaf0..0ae10f7 100644 +index 54ffaf0..653c4d4 100644 --- a/node_modules/vite/dist/node/chunks/dep-0fc8e132.js +++ b/node_modules/vite/dist/node/chunks/dep-0fc8e132.js @@ -12002,6 +12002,10 @@ function copyDir(srcDir, destDir) { @@ -24,3 +24,39 @@ index 54ffaf0..0ae10f7 100644 if (headers) { for (const name in headers) { res.setHeader(name, headers[name]); +@@ -40203,7 +40211,7 @@ function importAnalysisPlugin(config) { + // properly finish the request with a 504 sent to the browser. + throwOutdatedRequest(importer); + } +- if (!imports.length) { ++ if (!imports.length && !this._addedImports) { + importerModule.isSelfAccepting = false; + isDebug$1 && + debug$7(`${timeFrom(start)} ${picocolors.exports.dim(`[no imports] ${prettyImporter}`)}`); +@@ -40224,7 +40232,7 @@ function importAnalysisPlugin(config) { + ? new Map() + : null; + const toAbsoluteUrl = (url) => path$n.posix.resolve(path$n.posix.dirname(importerModule.url), url); +- const normalizeUrl = async (url, pos) => { ++ const normalizeUrl = async (url, pos, forceSkipImportAnalysis) => { + if (base !== '/' && url.startsWith(base)) { + url = url.replace(base, '/'); + } +@@ -40303,7 +40311,7 @@ function importAnalysisPlugin(config) { + // up-to-date version of this module. + try { + // delay setting `isSelfAccepting` until the file is actually used (#7870) +- const depModule = await moduleGraph.ensureEntryFromUrl(url, ssr, canSkipImportAnalysis(url)); ++ const depModule = await moduleGraph.ensureEntryFromUrl(url, ssr, canSkipImportAnalysis(url) || forceSkipImportAnalysis); + if (depModule.lastHMRTimestamp > 0) { + url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`); + } +@@ -40518,7 +40526,7 @@ function importAnalysisPlugin(config) { + // attached by pluginContainer.addWatchFile + const pluginImports = this._addedImports; + if (pluginImports) { +- (await Promise.all([...pluginImports].map((id) => normalizeUrl(id, 0)))).forEach(([url]) => importedUrls.add(url)); ++ (await Promise.all([...pluginImports].map((id) => normalizeUrl(id, 0, true)))).forEach(([url]) => importedUrls.add(url)); + } + // HMR transforms are no-ops in SSR, so an `accept` call will + // never be injected. Avoid updating the `isSelfAccepting`