html-bundler-webpack-plugin
Version:
Generates complete single-page or multi-page website from source assets. Built-in support for Markdown, Eta, EJS, Handlebars, Nunjucks, Pug. Alternative to html-webpack-plugin.
43 lines (36 loc) • 1.59 kB
JavaScript
// This module exports ESM loader hooks used for custom resolution and loading behavior.
// It is registered dynamically via moduleRegister.js.
/**
* Custom resolve hook.
*
* Asynchronous version accepted by module.register().
*
* Appends a unique cache-busting query parameter to module URLs when the parent
* module was imported with the `nocache` flag.
* This helps force reloads of changed ESM modules, useful for features like Live Reload or HMR.
*
* @param {string} specifier The raw import value.
* @param {{conditions: string[], importAttributes: Object, parentURL: string | undefined}} context Info.
* @param {Function} nextResolve The default or next resolve hook in the chain.
* @return {Promise<{url: string, format?: string, shortCircuit?: boolean, importAttributes?: Object}>}
*/
async function resolve(specifier, context, nextResolve) {
const resolved = await nextResolve(specifier, context);
const resolvedUrl = new URL(resolved.url);
const parentUrl = new URL(context.parentURL);
const flag = 'nocache';
// skip builtin node and npm modules in resolve hook
if (resolved.format === 'builtin' || resolvedUrl.protocol === 'node:' || resolved.url.includes('/node_modules/')) {
return resolved;
}
if (!resolvedUrl.searchParams.get(flag) && parentUrl.searchParams.get(flag)) {
resolvedUrl.searchParams.set(flag, Date.now().toString());
return {
...resolved,
url: resolvedUrl.href,
shortCircuit: false, // if false, continue to next loader in chain
};
}
return resolved;
}
module.exports = { resolve };