Skip to content

Commit

Permalink
Remove logging (#13)
Browse files Browse the repository at this point in the history
* Remove logging of assets from middleware

* Simplify event emitting by extending FSWatcher
  • Loading branch information
craigbeck authored Apr 19, 2023
1 parent e389c0f commit 0bdc2ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
62 changes: 27 additions & 35 deletions lib/FileWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* only watches paths under app path
* does not watch anything in node_modules/
*/
const chokidar = require('chokidar');
const { resolve } = require('path');
const chokidar = require('chokidar');

const watchers = new Map();

Expand All @@ -24,53 +24,45 @@ module.exports = function createWatcher(path, options) {
return watcher;
};

function FileWatcher(path, options) {
const chokidarOptions = {
ignored: options?.ignored || ['**/*.html'],
useFsEvents: Boolean(JSON.parse(process.env.WATCH_FSEVENTS ?? false)),
usePolling: Boolean(JSON.parse(process.env.WATCH_POLL ?? false)),
};

const root = resolve(path);

const watcher = chokidar.watch(root, options);

watcher
.on('change', function (subpath) {
const path = resolve(subpath);
console.log('---> 🔁 WATCHER CHANGE', path);
purgeRefs(path, root);
})
.on('ready', function () {
console.log('===> 🔎 watching', root);
})

this.watcher = watcher;
}

FileWatcher.prototype.on = function (event, callback) {
this.watcher.on(event, callback);
return this;
}
const EVENTS = {
decached: 'decached'
};

FileWatcher.prototype.close = function () {
this.watcher.close();
class FileWatcher extends chokidar.FSWatcher {
constructor(path, options) {
const chokidarOptions = {
ignored: options?.ignored || ['**/*.html'],
useFsEvents: Boolean(JSON.parse(process.env.WATCH_FSEVENTS ?? false)),
usePolling: Boolean(JSON.parse(process.env.WATCH_POLL ?? false)),
};
super(chokidarOptions);
const root = resolve(path);
this.add(root);
this.on('change', function (subpath) {
setTimeout(() => {
const path = resolve(subpath);
const purgedIds = purgeRefs(path, root);
this._emit(EVENTS.decached, purgedIds);
}, 0);
});
}
}

function findChildRefs(idStr) {
const entries = Object.values(require.cache);
return entries.filter(entry => entry.children.some(child => child.id === idStr)).map(entry => entry.id);
}

function purgeRefs(idStr, root) {
function purgeRefs(idStr, root, purged = []) {
if (!require.cache[idStr]) {
return;
return purged;
}
console.log('---> 🗑️ decache', idStr);
delete require.cache[idStr];
purged.push(idStr);
const markedForDecache = findChildRefs(idStr);
markedForDecache.filter(idStr => idStr.startsWith(root))
.forEach(idStr => {
purgeRefs(idStr, root);
purgeRefs(idStr, root, purged);
});
return purged;
}
5 changes: 2 additions & 3 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ function pluginForAppInstance(app) {
function middlewareAssets(page) {
const { devMiddleware } = page.res.locals.webpack;
const jsonWebpackStats = devMiddleware.stats.toJson();
const { assetsByChunkName, outputPath } = jsonWebpackStats;
const { assetsByChunkName } = jsonWebpackStats;
const publicPath = devMiddleware.options.publicPath ?? '/';
console.log(assetsByChunkName);
console.log(devMiddleware.outputFileSystem.readdirSync(outputPath));

return Object.values(assetsByChunkName)
.flatMap(normalizeAssets)
.filter(key => sourcesRe.test(key))
Expand Down

0 comments on commit 0bdc2ac

Please sign in to comment.