Skip to content

Commit a8c5dee

Browse files
committed
fix: catching of the error when a peer dependency for a Pug filter is not installed
1 parent c237902 commit a8c5dee

5 files changed

Lines changed: 74 additions & 26 deletions

File tree

CHANGELOG.md

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

3+
## 3.6.4 (2024-03-17)
4+
5+
- fix: catching of the error when a peer dependency for a Pug filter is not installed
6+
37
## 3.6.3 (2024-03-14)
48

59
- fix: resolving asset files on windows

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3591,22 +3591,29 @@ For all available options, see the [Nunjucks API configure](https://mozilla.gith
35913591
35923592
// available useful embedded filters
35933593
embedFilters: {
3594-
// enable the :escape filter
3594+
// enable the `:escape` filter
35953595
escape: true,
35963596
3597-
// enable the :code filter
3597+
// enable the `:code` filter
35983598
code: {
35993599
className: 'language-', // class name of `<code>` tag
36003600
},
36013601
3602-
// enable :highlight filter
3602+
// enable `:highlight` filter
36033603
highlight: {
3604+
use: 'prismjs', // use the `prismjs` module as highlighter, must be installed
36043605
verbose: true,
3605-
use: 'prismjs', // use the prismjs as highlighter
36063606
},
36073607
3608-
// enable :markdown filter
3608+
// enable `:markdown` filter for markdown only, w/o code blocks
36093609
markdown: true,
3610+
// - OR - you can enable highlighter for code blocks used in markdown
3611+
markdown: {
3612+
highlight: {
3613+
use: 'prismjs', // use the `prismjs` module as highlighter, must be installed
3614+
verbose: true,
3615+
},
3616+
},
36103617
}
36113618
},
36123619
},

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.6.3",
3+
"version": "3.6.4",
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/Loader/Preprocessors/Pug/Filter.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,7 @@ class Filter {
3333
try {
3434
filter = require(filterPath);
3535
} catch (error) {
36-
const message = error.toString();
37-
const posEOL = message.indexOf('\n');
38-
const messageFirstLine = message.slice(0, posEOL);
39-
40-
if (messageFirstLine.indexOf('Cannot find module') >= 0) {
41-
if (messageFirstLine.indexOf(filterPath) > 0) {
42-
const entries = fs.readdirSync(filtersPath, { withFileTypes: true });
43-
const files = entries
44-
.filter((file) => !file.isDirectory())
45-
.map((file) => path.basename(file.name, '.js'));
46-
47-
filterNotFoundException(filterName, files.join(', '));
48-
} else {
49-
const [, module] = /Cannot find module '(.*?)' from/.exec(messageFirstLine);
50-
51-
loadNodeModuleException(module);
52-
}
53-
}
54-
55-
filterLoadException(filterName, filterPath, error);
36+
this.loadModuleException({ filterName, filterPath, error });
5637
}
5738

5839
try {
@@ -67,6 +48,34 @@ class Filter {
6748
}
6849
}
6950
}
51+
52+
/**
53+
* @param {string} filterName
54+
* @param {string} filterPath
55+
* @param {Error} error
56+
*/
57+
static loadModuleException({ filterName, filterPath, error }) {
58+
const message = error.toString();
59+
const posEOL = message.indexOf('\n');
60+
const messageFirstLine = message.slice(0, posEOL);
61+
62+
if (messageFirstLine.indexOf('Cannot find module') >= 0) {
63+
if (messageFirstLine.indexOf(filterPath) > 0) {
64+
const entries = fs.readdirSync(filtersPath, { withFileTypes: true });
65+
const files = entries.filter((file) => !file.isDirectory()).map((file) => path.basename(file.name, '.js'));
66+
67+
filterNotFoundException(filterName, files.join(', '));
68+
} else {
69+
const [, module] = /Cannot find module '(.*?)'/.exec(messageFirstLine) || [];
70+
71+
if (module) {
72+
loadNodeModuleException(module);
73+
}
74+
}
75+
}
76+
77+
filterLoadException(filterName, filterPath, error);
78+
}
7079
}
7180

7281
module.exports = Filter;

test/integration-pug.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { filterLoadException } from '../src/Loader/Preprocessors/Pug/Exeptions';
33
import adapterHighlight from '../src/Loader/Preprocessors/Pug/filters/highlight/adapter';
44
import filterHighlight from '../src/Loader/Preprocessors/Pug/filters/highlight';
55

6+
import FugFilter from '../src/Loader/Preprocessors/Pug/Filter';
7+
68
// TODO: add in docs
79
// BRAKING CHANGE (to compare with pug-loader)
810
// The inlined loader in URL is no longer supported because it is an ugly/terrible usage syntax:
@@ -139,6 +141,32 @@ describe('exception tests', () => {
139141
done();
140142
});
141143

144+
test('exception 1: by load peer dependency for a filter', (done) => {
145+
const expected = /The required (.+?) module not found/;
146+
147+
const result = () => {
148+
FugFilter.loadModuleException({
149+
error: new Error(`Cannot find module 'parse5'\n`),
150+
filterName: 'highlight',
151+
});
152+
};
153+
expect(result).toThrow(expected);
154+
done();
155+
});
156+
157+
test('exception 2: by load peer dependency for a filter', (done) => {
158+
const expected = /Error by load the (.+?) filter/;
159+
160+
const result = () => {
161+
FugFilter.loadModuleException({
162+
error: new Error(`Other exception`),
163+
filterName: 'highlight',
164+
});
165+
};
166+
expect(result).toThrow(expected);
167+
done();
168+
});
169+
142170
test('exception: filter :highlight - unsupported module', () => {
143171
const filterHighlight = require('../src/Loader/Preprocessors/Pug/filters/highlight');
144172
const adapterHighlight = require('../src/Loader/Preprocessors/Pug/filters/highlight/adapter');

0 commit comments

Comments
 (0)