tshtml-loader 1.1.6 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +179 -0
- package/lib/export-template.d.ts +1 -0
- package/lib/export-template.js +25 -3
- package/lib/index.js +1 -9
- package/package.json +5 -3
package/README.md
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# tshtml
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
```
|
|
5
|
+
npm i tshtml tshtml-loader
|
|
6
|
+
```
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## What is it?
|
|
10
|
+
tshtml-file is a TypeScript template that emits HTML code during build-time.
|
|
11
|
+
The simplest one could look like this:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
// test.tshtml
|
|
15
|
+
|
|
16
|
+
export default `<p>Hello world!</p>`;
|
|
17
|
+
```
|
|
18
|
+
Obviosly this does not differ much from a static HTML file with the same paragraph.
|
|
19
|
+
But now you can start write code in the template, just like this:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
export default `
|
|
23
|
+
<p>Hello world!</p>
|
|
24
|
+
<p>Build time is ${Date()}.</p>`;
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
This code is executed at build time, and only the resulting
|
|
28
|
+
HTML will be fed to Angular. This way we produce just the same fast Angular templates,
|
|
29
|
+
but with help of possible very sophisticated code in tshtml.
|
|
30
|
+
So, it's a lovely little metaprogramming: code that creates code.
|
|
31
|
+
|
|
32
|
+
You can write helper functions to extract repetitive patterns of template code,
|
|
33
|
+
import constants and static data structures used at run time to generate some
|
|
34
|
+
template elements out of them, and also create templates that inherit other templates.
|
|
35
|
+
The latter is very handy when you have Angular components derived from some
|
|
36
|
+
base components.
|
|
37
|
+
|
|
38
|
+
## _html_ function
|
|
39
|
+
Combining HTML by hands is not very pleasant task. You must ensure that it is well-formed
|
|
40
|
+
so usually it requires more efforts than just to concatenate strings together.
|
|
41
|
+
|
|
42
|
+
To simplify this task we created `html` function. It parses the text into a
|
|
43
|
+
tree of node-like objects that can be later serialized to a well-formed HTML.
|
|
44
|
+
Since the tree is just a graph of plain JavaScript objects, you can update it:
|
|
45
|
+
change attributes, add new children and so on.
|
|
46
|
+
|
|
47
|
+
The simplest example with `html` added looks basically the same:
|
|
48
|
+
```
|
|
49
|
+
import { html } from "tshtml";
|
|
50
|
+
|
|
51
|
+
export default html`<p>Hello world!</p>`;
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Let's combine two HTML trees together:
|
|
55
|
+
```
|
|
56
|
+
import { html } from "tshtml";
|
|
57
|
+
|
|
58
|
+
const buildTime = html`<p>Build time is ${Date()}.</p>`;
|
|
59
|
+
|
|
60
|
+
// Resulting tree has buildTime subtree inserted at the appropriate place
|
|
61
|
+
const res = html`
|
|
62
|
+
<p>Hello world!</p>
|
|
63
|
+
${buildTime}`;
|
|
64
|
+
|
|
65
|
+
// Tree of template elements will be converted to string by the loader
|
|
66
|
+
export default res;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Here `builtTime` is not a string, it's an object, but you can still use standard
|
|
70
|
+
template string placeholder syntax to combine HTML fragments together.
|
|
71
|
+
|
|
72
|
+
The tree can be manipulated using methods of `TemplateItem` class.
|
|
73
|
+
```
|
|
74
|
+
import { htmlEl } from "tshtml";
|
|
75
|
+
|
|
76
|
+
const res = htmlEl`<p>Hello world!</p>`;
|
|
77
|
+
|
|
78
|
+
// Add an attribute
|
|
79
|
+
res.attrs( { "color": "red } );
|
|
80
|
+
|
|
81
|
+
export default res;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## To use in your Angular project
|
|
85
|
+
First, configure Angular build as described here:
|
|
86
|
+
https://codeburst.io/customizing-angular-cli-6-build-an-alternative-to-ng-eject-a48304cd3b21
|
|
87
|
+
|
|
88
|
+
For that you need to install necessary packages:
|
|
89
|
+
```
|
|
90
|
+
npm install @angular-devkit/build-angular --save-dev
|
|
91
|
+
npm install @angular-builders/custom-webpack --save-dev
|
|
92
|
+
```
|
|
93
|
+
Add "architect" section to the "angular.json" if it is not already there. Change
|
|
94
|
+
builder configuration and specify the name of additional webpack config file:
|
|
95
|
+
```
|
|
96
|
+
"architect": {
|
|
97
|
+
"build": {
|
|
98
|
+
"builder": "@angular-builders/custom-webpack:browser",
|
|
99
|
+
"options": {
|
|
100
|
+
"customWebpackConfig": {
|
|
101
|
+
"path": "./extra-webpack.config.js",
|
|
102
|
+
"replaceDuplicatePlugins": true
|
|
103
|
+
},
|
|
104
|
+
...
|
|
105
|
+
},
|
|
106
|
+
"serve": {
|
|
107
|
+
"builder": "@angular-builders/custom-webpack:dev-server",
|
|
108
|
+
...
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Add `extra-webpack.config.js`. Configuration is a bit different depending of which
|
|
112
|
+
Webpack version do you use.
|
|
113
|
+
|
|
114
|
+
**Webpack 4**
|
|
115
|
+
|
|
116
|
+
With Webpack 4 (used in Angular 11 and earlier) you need to configure loader like this.
|
|
117
|
+
It's preferred to use `tshtml-loader` version less than 1.2 with Webpack 4.
|
|
118
|
+
```
|
|
119
|
+
module.exports = {
|
|
120
|
+
module: {
|
|
121
|
+
rules: [
|
|
122
|
+
{
|
|
123
|
+
test: /\.tshtml$/,
|
|
124
|
+
use: ["tshtml-loader"],
|
|
125
|
+
enforce: "pre"
|
|
126
|
+
},
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
**Webpack 5**
|
|
134
|
+
|
|
135
|
+
Webpack 5 (used in Angular 12 and up) brings new resource types, so the recommended loader configuration is the following:
|
|
136
|
+
```
|
|
137
|
+
module.exports = {
|
|
138
|
+
module: {
|
|
139
|
+
rules: [
|
|
140
|
+
test: /\.tshtml$/,
|
|
141
|
+
use: ["tshtml-loader"],
|
|
142
|
+
type: "asset/source"
|
|
143
|
+
]
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
You also need to have "tshtml" and "tshtml-loader" packages in your project.
|
|
150
|
+
|
|
151
|
+
Finally you just refer to `tshtml` files instead of `html` files in your Angular components.
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
## Library development
|
|
155
|
+
|
|
156
|
+
In tshtml folder:
|
|
157
|
+
```
|
|
158
|
+
npm run test
|
|
159
|
+
npm run build
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Debugging
|
|
163
|
+
|
|
164
|
+
If you need to debug the tests, then you can do the following:
|
|
165
|
+
|
|
166
|
+
1. In the console where you are going to execute tests set environment variable:
|
|
167
|
+
`set-item env:NODE_OPTIONS "--inspect"`
|
|
168
|
+
|
|
169
|
+
1. Configure you debugger to attach to Node.js at port 9229. Start the debugger
|
|
170
|
+
|
|
171
|
+
1. Start test: `node ./test/jasmine "TEST NAME"`
|
|
172
|
+
|
|
173
|
+
### tshtml-loader
|
|
174
|
+
|
|
175
|
+
In tshtml-loader folder:
|
|
176
|
+
```
|
|
177
|
+
npm run build
|
|
178
|
+
```
|
|
179
|
+
|
package/lib/export-template.d.ts
CHANGED
package/lib/export-template.js
CHANGED
|
@@ -1,20 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
"use strict";
|
|
2
3
|
exports.__esModule = true;
|
|
3
4
|
var fs_1 = require("fs");
|
|
4
5
|
var path = require("node:path");
|
|
5
6
|
var index_1 = require("./index");
|
|
7
|
+
// ----------------------------------------------------------------------------------
|
|
6
8
|
if (process.argv.length !== 3) {
|
|
7
9
|
console.error("Please specify one .tshtml file");
|
|
8
10
|
process.exit();
|
|
9
11
|
}
|
|
10
12
|
var fileName = process.argv[2];
|
|
13
|
+
var extension = path.extname(fileName).toLowerCase();
|
|
14
|
+
if (extension !== ".tshtml") {
|
|
15
|
+
console.error("Input file must have .tshtml extension");
|
|
16
|
+
process.exit();
|
|
17
|
+
}
|
|
18
|
+
var fileNameNoExtension = fileName.substr(0, fileName.length - extension.length);
|
|
19
|
+
var outputFileName = fileNameNoExtension + ".html";
|
|
11
20
|
fs_1.readFile(fileName, { encoding: 'utf-8' }, function (err, data) {
|
|
12
21
|
if (!err) {
|
|
13
|
-
|
|
22
|
+
// Transform the source
|
|
23
|
+
var result = index_1.executeTemplate(data, path.join(process.cwd(), "/test.html"));
|
|
14
24
|
var htmlResult = index_1.templateToString(result.exports["default"]);
|
|
15
|
-
|
|
25
|
+
// Write to destination
|
|
26
|
+
// process.stdout.write( htmlResult );
|
|
27
|
+
fs_1.writeFile(outputFileName, htmlResult, {
|
|
28
|
+
encoding: "utf8",
|
|
29
|
+
flag: "w"
|
|
30
|
+
}, function (writeErr) {
|
|
31
|
+
if (!writeErr) {
|
|
32
|
+
console.log("File is written: " + outputFileName);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
console.log("Error writing to the output file " + outputFileName, writeErr);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
16
38
|
}
|
|
17
39
|
else {
|
|
18
|
-
console.log(err);
|
|
40
|
+
console.log("Error reading the input file " + fileName, err);
|
|
19
41
|
}
|
|
20
42
|
});
|
package/lib/index.js
CHANGED
|
@@ -34,15 +34,7 @@ function default_1(source) {
|
|
|
34
34
|
var file = filteredDependencies_1[_i];
|
|
35
35
|
this.addDependency(file);
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
return htmlResult;
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
var json = JSON.stringify(htmlResult)
|
|
42
|
-
.replace(/\u2028/g, '\\u2028')
|
|
43
|
-
.replace(/\u2029/g, '\\u2029');
|
|
44
|
-
return "module.exports = " + json;
|
|
45
|
-
}
|
|
37
|
+
return htmlResult;
|
|
46
38
|
}
|
|
47
39
|
exports["default"] = default_1;
|
|
48
40
|
;
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tshtml-loader",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib/*.*"
|
|
8
8
|
],
|
|
9
|
+
"bin": "./lib/export-template.js",
|
|
9
10
|
"scripts": {
|
|
10
|
-
"build": "tsc ./src/index.ts ./src/export-template.ts --outdir ./lib -d",
|
|
11
|
-
"build-watch": "tsc ./src/index.ts --outdir ./lib -d -w"
|
|
11
|
+
"build": "tsc ./src/index.ts ./src/export-template.ts --outdir ./lib -d && npm run copy-docs",
|
|
12
|
+
"build-watch": "tsc ./src/index.ts --outdir ./lib -d -w",
|
|
13
|
+
"copy-docs": "npx npx shx cp ../README.md ."
|
|
12
14
|
},
|
|
13
15
|
"author": "LOGEX Group",
|
|
14
16
|
"license": "MIT",
|