Skip to content

Commit 618fa46

Browse files
committed
feat: add supports the require of CommonJS and JSON files in EJS templates
1 parent c2af4cc commit 618fa46

File tree

18 files changed

+226
-19
lines changed

18 files changed

+226
-19
lines changed

CHANGELOG.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
# Change log
22

3+
## 4.1.0 (2024-09-29)
4+
5+
- feat: add supports the `require` of CommonJS and JSON files in EJS templates:
6+
```html
7+
<% const data = require('./data.js') %>
8+
<div>Film: <%= data.title %></div>
9+
<div>Genre: <%= data.genre %></div>
10+
```
11+
or
12+
```html
13+
<% const data = require('./data.json') %>
14+
<div>Film: <%= data.title %></div>
15+
<div>Genre: <%= data.genre %></div>
16+
```
17+
- chore: update peerDependencies
18+
- test: refactor test cases for preprocessor
19+
320
<a id="v4-0-0" name="v4-0-0"></a>
4-
## 4.0.0 Release (24-09-08)
21+
## 4.0.0 Release (2024-09-08)
522

623
### BREAKING CHANGES
724

package-lock.json

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

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "4.0.0",
3+
"version": "4.1.0",
44
"description": "HTML Bundler Plugin for Webpack renders HTML templates containing source files of scripts, styles, images. Supports template engines: Eta, EJS, Handlebars, Nunjucks, Pug, TwigJS. Alternative to html-webpack-plugin.",
55
"keywords": [
66
"html",
@@ -92,16 +92,16 @@
9292
"node": ">=16.20.0"
9393
},
9494
"peerDependencies": {
95-
"ejs": ">=3.1.9",
96-
"favicons": ">=7.1.4",
97-
"handlebars": ">=4.7.7",
98-
"liquidjs": ">=10.7.0",
95+
"ejs": ">=3.1.10",
96+
"favicons": ">=7.2.0",
97+
"handlebars": ">=4.7.8",
98+
"liquidjs": ">=10.17.0",
9999
"markdown-it": ">=12",
100100
"mustache": ">=4.2.0",
101101
"nunjucks": ">=3.2.3",
102102
"parse5": ">=7.1.2",
103103
"prismjs": ">=1.29.0",
104-
"pug": ">=3.0.2",
104+
"pug": ">=3.0.3",
105105
"twig": ">=1.17.1",
106106
"webpack": ">=5.81.0"
107107
},
@@ -170,7 +170,7 @@
170170
"handlebars": "^4.7.8",
171171
"handlebars-layouts": "^3.1.4",
172172
"jest": "^29.7.0",
173-
"liquidjs": "^10.16.7",
173+
"liquidjs": "^10.17.0",
174174
"markdown-it": "^14.1.0",
175175
"mustache": "^4.2.0",
176176
"normalize.css": "^8.0.1",

src/Loader/Preprocessors/Ejs/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { readFileSync } = require('fs');
2+
const path = require('path');
13
const { loadModule } = require('../../../Common/FileUtils');
24
const { stringifyJSON } = require('../../Utils');
35

@@ -9,6 +11,19 @@ const includeRegexp = /include\((.+?)(?:\)|,\s*{(.+?)}\))/g;
911
// node module name
1012
const moduleName = 'ejs';
1113

14+
/**
15+
* Require CommonJS or JSON file in EJS template.
16+
*
17+
* @param {string} file
18+
* @param {string} dir
19+
* @return {*}
20+
*/
21+
const requireFile = (file, dir) => {
22+
const fullFilePath = path.join(dir, file);
23+
24+
return file.endsWith('.json') ? JSON.parse(readFileSync(fullFilePath, 'utf-8')) : require(fullFilePath);
25+
};
26+
1227
/**
1328
* Transform the raw template source to a template function or HTML.
1429
*
@@ -36,6 +51,10 @@ const preprocessor = (loaderContext, options) => {
3651
* @return {string}
3752
*/
3853
render(source, { resourcePath, data = {} }) {
54+
const contextPath = path.dirname(resourcePath);
55+
56+
data.require = (file) => requireFile(file, contextPath);
57+
3958
return Ejs.render(source, data, {
4059
async: false,
4160
root: rootContext, // root path for includes with an absolute path (e.g., /file.html)
1.74 KB
Loading
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<title>Home</title>
6+
</head>
7+
<body>
8+
<h1>Breaking Bad</h1>
9+
<ul class="people">
10+
11+
<li>Walter White</li>
12+
13+
<li>Jesse Pinkman</li>
14+
15+
</ul>
16+
<img src="assets/img/apple.02a7c382.png" alt="apple" />
17+
<div>footer</div>
18+
19+
</body>
20+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
title: 'Home',
3+
headline: 'Breaking Bad',
4+
people: ['Walter White', 'Jesse Pinkman'],
5+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<% const data = require('./data.js') %>
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<title><%= data.title %></title>
6+
</head>
7+
<body>
8+
<h1><%= data.headline %></h1>
9+
<ul class="people">
10+
<% for (let i = 0; i < data.people.length; i++) {%>
11+
<li><%= data.people[i] %></li>
12+
<% } %>
13+
</ul>
14+
<img src="@images/apple.png" alt="apple" />
15+
<%- include('partials/footer.html'); %>
16+
</body>
17+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>footer</div>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const path = require('path');
2+
const HtmlBundlerPlugin = require('@test/html-bundler-webpack-plugin');
3+
4+
module.exports = {
5+
mode: 'production',
6+
7+
output: {
8+
path: path.join(__dirname, 'dist/'),
9+
},
10+
11+
resolve: {
12+
alias: {
13+
'@images': path.join(__dirname, '../../../fixtures/images'),
14+
},
15+
},
16+
17+
plugins: [
18+
new HtmlBundlerPlugin({
19+
test: /\.(html|ejs)$/,
20+
entry: {
21+
index: {
22+
import: './src/home.ejs',
23+
},
24+
},
25+
preprocessor: 'ejs',
26+
}),
27+
],
28+
29+
module: {
30+
rules: [
31+
{
32+
test: /\.(png|svg|jpe?g|webp)$/i,
33+
type: 'asset/resource',
34+
generator: {
35+
filename: 'assets/img/[name].[hash:8][ext]',
36+
},
37+
},
38+
],
39+
},
40+
};

0 commit comments

Comments
 (0)