Skip to content

Commit c867948

Browse files
committed
fix: use custom esm loader only by serv/match mode
1 parent d283184 commit c867948

14 files changed

Lines changed: 83 additions & 60 deletions

File tree

README.npm.md

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,16 @@
1313
[![codecov](https://codecov.io/gh/webdiscus/html-bundler-webpack-plugin/branch/master/graph/badge.svg?token=Q6YMEN536M)](https://codecov.io/gh/webdiscus/html-bundler-webpack-plugin)
1414
[![node](https://img.shields.io/npm/dm/html-bundler-webpack-plugin)](https://www.npmjs.com/package/html-bundler-webpack-plugin)
1515

16-
> This plugin is all you need to generate a complete single- or multi-page website from your source assets.
16+
The plugin automates the creation of complete web pages by processing HTML templates with linked assets.
17+
It resolves dependencies, compiles templates, and ensures that the output HTML contains correct output URLs.
1718

18-
The plugin automates the processing of source files such as JS/TS, SCSS, images and other assets referenced in an HTML or template file.
19-
This plugin will generate an HTML file containing all the necessary links to JS, CSS, images and other resources.
19+
## Install
2020

21-
## Why use the HTML Bundler Plugin?
21+
```bash
22+
npm install html-bundler-webpack-plugin --save-dev
23+
```
2224

23-
This plugin is a powerful alternative to [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) and a replacement for many [plugins and loaders](https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/README.md#list-of-plugins).
24-
25-
The HTML Bundler Plugin works a bit differently than `html-webpack-plugin`.
26-
It doesn't just inject JavaScript and CSS into an HTML.
27-
Instead, it resolves all the source files of the assets referenced directly in the template
28-
and ensures the generated HTML contains the correct output URLs of resources after Webpack processes them.
29-
Additionally, CSS extracted from styles imported in JS can be injected into HTML as a `<link>` tag or as an inlined CSS.
30-
31-
---
32-
33-
<h3 align="center">
34-
📋 <a href="https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/README.md#contents">Table of Contents</a> 🚀<a href="https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/README.md#install">Install and Quick Start</a> 🖼 <a href="https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/README.md#usage-examples">Usage examples</a>
35-
</h3>
36-
37-
---
38-
39-
## 💡 Highlights
25+
## Highlights
4026

4127
- An [entry point](https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/README.md#option-entry) is any HTML template. **Start from HTML or template**, not from JS.
4228
- **Automatically** processes templates found in the [entry directory](https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/README.md#option-entry-path).
@@ -57,8 +43,16 @@ Additionally, CSS extracted from styles imported in JS can be injected into HTML
5743
- Generates the [integrity](https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/README.md#option-integrity) attribute in the `link` and `script` tags.
5844
- Generates the [favicons](https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/README.md#favicons-bundler-plugin) of different sizes for various platforms.
5945
- You can create custom plugins using the provided [Plugin Hooks](https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/README.md#plugin-hooks-and-callbacks).
60-
- Over 700 [tests](https://github.com/webdiscus/html-bundler-webpack-plugin/tree/master/test) for various use cases.
6146

62-
---
47+
[GitHub](https://github.com/webdiscus/html-bundler-webpack-plugin)
48+
49+
## Documentation
50+
51+
- [Get Started](https://webdiscus.github.io/html-bundler-webpack-plugin/category/getting-started)
52+
- [Migrating from `html-webpack-plugin`](https://webdiscus.github.io/html-bundler-webpack-plugin/getting-started/migrating-from-html-webpack-plugin)
53+
- [Features](https://webdiscus.github.io/html-bundler-webpack-plugin/introduction#key-features)
54+
- [Options](https://webdiscus.github.io/html-bundler-webpack-plugin/category/options)
55+
- [Guides](https://webdiscus.github.io/html-bundler-webpack-plugin/guides)
56+
- [F.A.Q.](https://webdiscus.github.io/html-bundler-webpack-plugin/faq/import-url-in-css)
6357

64-
📖 See [full documentation on GitHub](https://github.com/webdiscus/html-bundler-webpack-plugin).
58+
For full documentation, visit [HTML Bundler Docs](https://webdiscus.github.io/html-bundler-webpack-plugin).

package.json

Lines changed: 3 additions & 4 deletions
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.6",
3+
"version": "4.20.0-beta.7",
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",
@@ -25,10 +25,9 @@
2525
"integrity",
2626
"js",
2727
"javascript",
28+
"style",
2829
"css",
29-
"scss",
30-
"style-loader",
31-
"html-webpack-plugin"
30+
"scss"
3231
],
3332
"license": "ISC",
3433
"author": "webdiscus (https://github.com/webdiscus)",

src/Common/FileSystem/Module/nocacheLoader.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
/**
55
* Custom resolve hook.
66
*
7+
* Asynchronous version accepted by module.register().
8+
*
79
* Appends a unique cache-busting query parameter to module URLs when the parent
810
* module was imported with the `nocache` flag.
911
* This helps force reloads of changed ESM modules, useful for features like Live Reload or HMR.

src/Common/FileSystem/loadModule.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const path = require('path');
2-
const nativeEsmLoader = require('./Module/moduleLoader');
3-
const registerLoader = require('./Module/register');
4-
const { fileExistsAsync } = require('./Utils');
52
const { cyan, yellow } = require('ansis');
3+
const { fileExistsAsync } = require('./Utils');
4+
const nativeEsmLoader = require('./Module/moduleLoader');
65

7-
// Register the custom module loader.
8-
registerLoader();
6+
// Register the custom module loader to read real data w/o cache from changed ESM files.
7+
// It is required for using loadModuleAsync() by serv/watch mode.
8+
// const registerLoader = require('./Module/register');
9+
// registerLoader();
910

1011
/**
1112
* Load a CommonJS or ESM module.

src/Loader/Option.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const PluginService = require('../Plugin/PluginService');
33
const { parseQuery, outToConsole } = require('../Common/Helpers');
44
const { rootSourceDir, filterParentPaths } = require('../Common/FileUtils');
55
const { loadModuleAsync } = require('../Common/FileSystem/loadModule');
6+
const registerLoader = require('../Common/FileSystem/Module/register');
67
const { fileExistsAsync } = require('../Common/FileSystem/Utils');
78
const { findRootIssuer } = require('../Common/CompilationHelpers');
89
const { getDataFileNotFoundException, getDataFileException } = require('./Messages/Exeptions');
@@ -100,6 +101,12 @@ class Option {
100101
this.#preprocessorModule = preprocessorModule;
101102
this.#initPreprocessor(loaderContext);
102103

104+
if (this.isWatchMode()) {
105+
// Register the custom module loader to read real data w/o cache from changed ESM files.
106+
// It is required for using loadModuleAsync() by serv/watch mode.
107+
registerLoader();
108+
}
109+
103110
const promises = [options.data, loaderContext.entryData, this.#queryData].map((file) => this.#loadData(file));
104111

105112
return Promise.all(promises)

test/cases/option-entry-data-file-esm-js/expected/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<h1>Welcome to Awesome Corp</h1>
88
<footer>
99
<p>&copy; Awesome Corp - All rights reserved</p>
10-
<p>Address: 123 Main Street, Springfield</p>
10+
<p>Address: Wall Street 123, New York</p>
1111
<p>Phone: +1 234 567 890</p>
1212
<p>Email: admin@awesomecorp.com</p>
1313
</footer>
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
function getData() {
2-
return {
3-
name: 'Awesome Corp',
4-
address: '123 Main Street, Springfield',
5-
phone: '+1 234 567 890',
6-
email: 'info@awesomecorp.com',
7-
};
8-
}
1+
import address from './companyAddress.js';
92

10-
export default getData;
3+
const data = {
4+
name: 'Awesome Corp',
5+
address: `${address.street}, ${address.city}`,
6+
phone: '+1 234 567 890',
7+
email: 'info@awesomecorp.com',
8+
};
9+
10+
export default data;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const address = {
2+
street: 'Wall Street 123',
3+
city: 'New York',
4+
};
5+
6+
export default address;

test/cases/option-entry-data-file-esm-js/src/data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import company from './company.js';
22

33
const data = {
44
company: {
5-
...company(),
5+
...company,
66
email: 'admin@awesomecorp.com', // override company.email
77
},
88
};

test/cases/option-entry-data-file-esm-mjs/expected/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<h1>Welcome to Awesome Corp</h1>
88
<footer>
99
<p>&copy; Awesome Corp - All rights reserved</p>
10-
<p>Address: 123 Main Street, Springfield</p>
10+
<p>Address: Wall Street 123, New York</p>
1111
<p>Phone: +1 234 567 890</p>
12-
<p>Email: info@awesomecorp.com</p>
12+
<p>Email: admin@awesomecorp.com</p>
1313
</footer>
1414
</body>
1515
</html>

0 commit comments

Comments
 (0)