Skip to content

Commit 4be3ffa

Browse files
committed
initial commit
0 parents  commit 4be3ffa

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Anton Maklakov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# hmpl-loader
2+
3+
[![npm-version](https://img.shields.io/npm/v/hmpl-loader?logo=npm&color=fff)](https://www.npmjs.com/package/hmpl-loader)
4+
5+
This loader was created for files with the `.hmpl` extension, which are converted using the [hmpl-js](https://github.com/hmpljs/hmpl) package. This loader is designed for webpack.
6+
7+
> Loader works with hmpl-js version 1.0.4 or higher
8+
9+
## Installation
10+
11+
```bash
12+
npm i hmpl-loader
13+
```
14+
15+
## Usage:
16+
17+
In the `webpack.config.js` file you can specify the following lines of code:
18+
19+
```javascript
20+
module.exports = {
21+
module: {
22+
rules: [
23+
{
24+
test: /\.hmpl$/i,
25+
use: ["hmpl-loader"],
26+
},
27+
],
28+
},
29+
};
30+
```
31+
32+
After `webpack.config.js` has been changed, in js files (for example, in `main.js`), you can import a file with the `.hmpl` extension and receive a [template function](https://hmpljs.github.io/#/?id=compile) in response.
33+
34+
### main.hmpl
35+
36+
```hmpl
37+
<div>
38+
{
39+
{
40+
"src":"/api/test"
41+
}
42+
}
43+
</div>
44+
```
45+
46+
### main.js
47+
48+
```javascript
49+
const templateFn = require("./main.hmpl");
50+
51+
const elementObj = templateFn();
52+
```
53+
54+
## Options
55+
56+
The loader supports the compile function options.
57+
58+
```javascript
59+
module.exports = {
60+
module: {
61+
rules: [
62+
{
63+
test: /\.hmpl$/i,
64+
use: {
65+
loader: "hmpl-loader",
66+
options: {
67+
memo: true,
68+
autoBody: {
69+
formData: true,
70+
},
71+
},
72+
},
73+
},
74+
],
75+
},
76+
};
77+
```
78+
79+
The list of options is described in the documentation [here](https://hmpl-lang.github.io/hmpl.html#options).
80+
81+
## Changelog
82+
83+
[Changelog](https://github.com/hmpljs/hmpl-loader/releases)
84+
85+
## License
86+
87+
[Licensed under MIT](https://github.com/hmpljs/hmpl-loader/blob/master/LICENSE)

index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const path = require("path");
2+
const schemaUtils = require("schema-utils");
3+
4+
const schema = {
5+
type: "object",
6+
properties: {
7+
memo: {
8+
type: "boolean",
9+
},
10+
autoBody: {
11+
anyOf: [
12+
{
13+
type: "object",
14+
properties: {
15+
formData: {
16+
type: "boolean",
17+
},
18+
},
19+
},
20+
{ type: "boolean" },
21+
],
22+
},
23+
},
24+
};
25+
26+
module.exports = function (source) {
27+
if (this.cacheable) this.cacheable();
28+
if (typeof source !== "string")
29+
throw Error(
30+
"Template was not found or the type of the passed value is not string"
31+
);
32+
const modulePath = JSON.stringify(
33+
path.join("hmpl-js", "dist", "hmpl.runtime")
34+
);
35+
const result = [];
36+
const template = JSON.stringify(source);
37+
const options = this.getOptions();
38+
schemaUtils.validate(schema, options, {
39+
name: "hmpl-loader",
40+
baseDataPath: "options",
41+
});
42+
const stringOptions = JSON.stringify(options);
43+
result.push(`const hmpl = require(${modulePath});\n`);
44+
result.push(`const template = hmpl.compile(${template},${stringOptions});\n`);
45+
result.push(`module.exports = template;`);
46+
return result.join("");
47+
};
48+
49+
module.exports.seperable = true;

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "hmpl-loader",
3+
"version": "0.0.4",
4+
"description": "hmpl loader for webpack",
5+
"main": "index.js",
6+
"scripts": {},
7+
"files": [
8+
"LICENSE",
9+
"README.md",
10+
"index.js"
11+
],
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/hmpljs/hmpl-loader"
15+
},
16+
"bugs": {
17+
"url": "https://github.com/hmpljs/hmpl-loader/issues"
18+
},
19+
"homepage": "https://hmpljs.github.io",
20+
"keywords": [
21+
"hmpl",
22+
"hmpl-js",
23+
"loader",
24+
"webpack",
25+
"webpack-loader"
26+
],
27+
"author": "Anton Maklakov",
28+
"license": "MIT",
29+
"engines": {
30+
"node": ">=10.12.0"
31+
},
32+
"dependencies": {
33+
"schema-utils": "^4.1.0"
34+
}
35+
}

0 commit comments

Comments
 (0)