Skip to content

Commit

Permalink
Support HMR for GLSL files
Browse files Browse the repository at this point in the history
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 vitejs/vite#9723
  • Loading branch information
zqianem committed Aug 22, 2022
1 parent e5cae02 commit fb6a285
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
13 changes: 13 additions & 0 deletions 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 = {}
27 changes: 27 additions & 0 deletions 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: '' }
};
}
38 changes: 37 additions & 1 deletion 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) {
Expand All @@ -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`

0 comments on commit fb6a285

Please sign in to comment.