Skip to content

Commit a57a02e

Browse files
committed
fix: issue when used js dynamic import with magic comments, #88
1 parent 54fa5e8 commit a57a02e

18 files changed

Lines changed: 131 additions & 28 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change log
22

3+
## 3.9.1 (2024-04-10)
4+
5+
- fix: issue when used js dynamic import with magic comments /* webpackPrefetch: true */ and css.inline=true, #88
6+
- fix: ansi colors for verbose output in some terminals
7+
38
## 3.9.0 (2024-04-07)
49

510
- feat: add support for dynamic import of styles

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": "3.9.0",
3+
"version": "3.9.1",
44
"description": "HTML bundler plugin for webpack handles a template as an entry point, extracts CSS and JS from their sources referenced in HTML, supports template engines like Eta, EJS, Handlebars, Nunjucks.",
55
"keywords": [
66
"html",

src/Plugin/AssetCompiler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,11 @@ class AssetCompiler {
955955
return assetModule;
956956
}
957957

958+
// fix #88: when used js dynamic import with magic comments /* webpackPrefetch: true */ and css.inline=true
959+
if (!this.currentEntryPoint && entry.isTemplate) {
960+
this.currentEntryPoint = entry;
961+
}
962+
958963
// extract CSS
959964
const cssOptions = Option.getStyleOptions(sourceFile);
960965
if (cssOptions == null) return;

src/Plugin/Messages/Info.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
const path = require('path');
2-
const {
3-
red,
4-
green,
5-
cyan,
6-
cyanBright,
7-
magenta,
8-
black,
9-
ansi256,
10-
bgAnsi256,
11-
yellowBright,
12-
bgAnsi,
13-
yellow,
14-
} = require('ansis');
2+
const { red, green, cyan, cyanBright, magenta, black, yellow, yellowBright, fg, bg } = require('ansis');
153
const Collection = require('../Collection');
164
const { outToConsole, isFunction } = require('../../Common/Helpers');
175
const { relativePathForView } = require('../../Common/FileUtils');
@@ -22,7 +10,7 @@ const PluginService = require('../PluginService');
2210

2311
const { pluginLabel } = Config.get();
2412

25-
const gray = ansi256(244);
13+
const gray = fg(244);
2614
const padLevel1 = 16;
2715
const padLevel2 = padLevel1 + 10;
2816
const padLevel3 = padLevel2 + 8;
@@ -37,13 +25,11 @@ const padChunks = padLevel1 + 4;
3725
* @return {string}
3826
*/
3927
const compilationName = (error) =>
40-
error
41-
? bgAnsi(196).whiteBright` ${pluginLabel} ` + red` ▶▶▶`
42-
: bgAnsi(118).black` ${pluginLabel} ` + green` ▶▶▶`;
28+
error ? bg(196).whiteBright` ${pluginLabel} ` + red` ▶▶▶` : bg(118).black` ${pluginLabel} ` + green` ▶▶▶`;
4329

4430
const colorType = (item, pad) => {
4531
let { type, inline } = item;
46-
const color = inline ? yellowBright : ansi256(112);
32+
const color = inline ? yellowBright : fg(112);
4733

4834
if (type === Collection.type.style && item.imported) {
4935
type = inline ? `inline styles` : `import styles`;
@@ -82,7 +68,7 @@ const renderAssets = (item, pad = padLevel2) => {
8268
padLen = padLevel1;
8369
} else {
8470
padLen = padChunks;
85-
str += `${'->'.padStart(padLevel1)} ${ansi256(120)`chunks:`}` + '\n';
71+
str += `${'->'.padStart(padLevel1)} ${fg(120)`chunks:`}` + '\n';
8672
}
8773

8874
for (let { inline, chunkFile, assetFile } of item.chunks) {
@@ -102,21 +88,21 @@ const renderAssets = (item, pad = padLevel2) => {
10288
* Display all processed assets in entry points.
10389
*/
10490
const verbose = () => {
105-
let str = '\n' + black.bgGreen` ${pluginLabel} ` + bgAnsi256(193).black` Entry processing ` + '\n';
91+
let str = '\n' + black.bgGreen` ${pluginLabel} ` + bg(193).black` Entry processing ` + '\n';
10692

10793
// display loader watch dependencies
10894
if (PluginService.isWatchMode()) {
10995
const watchFiles = Dependency.files;
11096

11197
if (watchFiles && watchFiles.size > 0) {
11298
str += '\n';
113-
str += ansi256(134)`watch files:` + `\n`;
99+
str += fg(134)`watch files:` + `\n`;
114100

115101
// TODO: correct sort paths
116102
const files = Array.from(watchFiles).sort();
117103
for (let file of files) {
118104
file = relativePathForView(file);
119-
str += `${'-'.padStart(3)} ${ansi256(147)(file)}` + '\n';
105+
str += `${'-'.padStart(3)} ${fg(147)(file)}` + '\n';
120106
}
121107
}
122108
}
@@ -127,16 +113,16 @@ const verbose = () => {
127113
const outputPath = relativePathForView(entry.outputPath);
128114

129115
str += '\n';
130-
str += bgAnsi256(27).whiteBright` ENTRY ` + ansi256(195).inverse` ${entryAsset} ` + '\n';
116+
str += bg(27).whiteBright` ENTRY ` + bg(195).black` ${entryAsset} ` + '\n';
131117
// str += `${magenta`output:`} ${cyanBright(entry.outputPath)}\n`; // for debugging only
132118
str += `${magenta`source:`} ${cyanBright(entrySource)}\n`;
133119
str += `${magenta`output:`} ${cyanBright(outputPath)}\n`;
134120

135121
// preload
136122
if (preloads?.length > 0) {
137-
str += ansi256(202)(`preloads:`) + `\n`;
123+
str += fg(202)(`preloads:`) + `\n`;
138124
for (const item of preloads) {
139-
str += ansi256(209)(`${item.type}:`.padStart(padLevel1)) + ` ${yellowBright(item.tag)}\n`;
125+
str += fg(209)(`${item.type}:`.padStart(padLevel1)) + ` ${yellowBright(item.tag)}\n`;
140126
}
141127
}
142128

@@ -164,11 +150,11 @@ const verbose = () => {
164150

165151
// styles imported in JS
166152
if (item.imported) {
167-
str += `${''.padStart(padLevel1)} ${ansi256(214)`imports:`}\n`;
153+
str += `${''.padStart(padLevel1)} ${fg(214)`imports:`}\n`;
168154
// note: if a style is imported, then resource is an array
169155
for (let importItem of item.imports) {
170156
sourceFile = relativePathForView(importItem.resource);
171-
str += `${''.padStart(padChunks)} ${ansi256(143)(sourceFile)}\n`;
157+
str += `${''.padStart(padChunks)} ${fg(143)(sourceFile)}\n`;
172158
str += renderAssets(importItem, padLevel3);
173159
}
174160
} else {

test/cases/js-dynamic-import-js/expected/deferred.286c.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Test</title>
5+
<style>h1 {
6+
color: indianred;
7+
}
8+
</style>
9+
<script src="main.js"></script>
10+
</head>
11+
<body>
12+
<h1>Hello World!</h1>
13+
</body>
14+
</html>

test/cases/js-dynamic-import-js/expected/main.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/js-dynamic-import-js/expected/prefetched-delayed.ab6b.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/js-dynamic-import-js/expected/prefetched.3aad.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import log from './log';
2+
3+
export default function () {
4+
log('Executed: Deferred module!');
5+
}

0 commit comments

Comments
 (0)