Skip to content

Commit aff9eba

Browse files
committed
fix: use resolve hook for modules to avoid caching of ESM files
1 parent 86ecaf9 commit aff9eba

24 files changed

Lines changed: 197 additions & 686 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "4.20.0-beta.4",
3+
"version": "4.20.0-beta.5",
44
"description": "Generates complete single-page or multi-page website from source assets. Build-in support for Markdown, Eta, EJS, Handlebars, Nunjucks, Pug. Alternative to html-webpack-plugin.",
55
"keywords": [
66
"html",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { pathToFileURL } = require('url');
2+
3+
const esmLoader = (absolutePath) => {
4+
const fileUrl = pathToFileURL(absolutePath).href;
5+
6+
// Note: read real data after changes from root file, sub-imported files are still cached.
7+
return import(`${fileUrl}?nocache=${Date.now()}`);
8+
};
9+
10+
module.exports = esmLoader;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This module exports ESM loader hooks used for custom resolution and loading behavior.
2+
// It is registered dynamically via moduleRegister.js.
3+
4+
/**
5+
* Custom resolve hook.
6+
*
7+
* Appends a unique cache-busting query parameter to module URLs when the parent
8+
* module was imported with the `nocache` flag.
9+
* This helps force reloads of changed ESM modules, useful for features like Live Reload or HMR.
10+
*
11+
* @param {string} specifier The raw import value.
12+
* @param {{conditions: string[], importAttributes: Object, parentURL: string | undefined}} context Info.
13+
* @param {Function} nextResolve The default or next resolve hook in the chain.
14+
* @return {Promise<{url: string, format?: string, shortCircuit?: boolean, importAttributes?: Object}>}
15+
*/
16+
async function resolve(specifier, context, nextResolve) {
17+
const resolved = await nextResolve(specifier, context);
18+
const resolvedUrl = new URL(resolved.url);
19+
const parentUrl = new URL(context.parentURL);
20+
const flag = 'nocache';
21+
22+
if (!resolvedUrl.searchParams.get(flag) && parentUrl.searchParams.get(flag)) {
23+
resolvedUrl.searchParams.set(flag, Date.now().toString());
24+
25+
return {
26+
...resolved,
27+
url: resolvedUrl.href,
28+
shortCircuit: false, // if false, continue to next loader in chain
29+
};
30+
}
31+
32+
return resolved;
33+
}
34+
35+
module.exports = { resolve };
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const path = require('path');
2+
const { pathToFileURL } = require('url');
3+
const { register } = require('module');
4+
5+
/**
6+
* Registers the module loader.
7+
*
8+
* @param {string} loader
9+
*/
10+
function registerLoader(loader = 'nocacheLoader.js') {
11+
const loaderPath = path.resolve(__dirname, loader);
12+
13+
register(pathToFileURL(loaderPath));
14+
}
15+
16+
module.exports = registerLoader;

src/Common/FileSystem/ModuleLoader/child.mjs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Common/FileSystem/ModuleLoader/loader.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)