@cilix/lightjs 0.0.6 → 0.0.8
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 +150 -150
- package/cli.js +19 -0
- package/core.js +5 -4
- package/index.js +12 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -72,156 +72,6 @@ Create a project boilerplate using Light.js's built-in backend router. Usage bel
|
|
|
72
72
|
npx lightjs create my-app
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
-
## Usage
|
|
76
|
-
|
|
77
|
-
### `lightjs.compile()`
|
|
78
|
-
|
|
79
|
-
Fundamentally, Light.js is a library that takes in a Light.js source tree and outputs a single HTML file or string.
|
|
80
|
-
|
|
81
|
-
```javascript
|
|
82
|
-
const lightjs = require('lightjs');
|
|
83
|
-
|
|
84
|
-
lightjs.compile({
|
|
85
|
-
input: 'src/index.lightjs',
|
|
86
|
-
output: 'public/index.html'
|
|
87
|
-
});
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
No other step is required for a fully reactive, bundled, minified application.
|
|
91
|
-
|
|
92
|
-
This function may also be used to serve dynamic content in real time.
|
|
93
|
-
|
|
94
|
-
```javascript
|
|
95
|
-
const express = require('express');
|
|
96
|
-
const lightjs = require('lightjs');
|
|
97
|
-
|
|
98
|
-
const app = express();
|
|
99
|
-
|
|
100
|
-
app.get('/', async (req, res) => {
|
|
101
|
-
const html = await lightjs.compile({
|
|
102
|
-
input: 'src/index.lightjs'
|
|
103
|
-
});
|
|
104
|
-
res.end(html);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
app.listen(3838);
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
In order for the above example to have production quality speed, you must enable caching. And if desired, Javascript minification can be enabled as well:
|
|
111
|
-
|
|
112
|
-
```javascript
|
|
113
|
-
const html = await lightjs.compile({
|
|
114
|
-
input: 'src/index.lightjs',
|
|
115
|
-
cache: true,
|
|
116
|
-
minify: true
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
The last thing to know about Light.js's compile function is document data.
|
|
121
|
-
|
|
122
|
-
```javascript
|
|
123
|
-
const html = await lightjs.compile({
|
|
124
|
-
input: 'src/about.lightjs',
|
|
125
|
-
data: {
|
|
126
|
-
pageTitle: 'About me'
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
In your Light.js document pageTitle can be found on the global 'data' object:
|
|
132
|
-
|
|
133
|
-
```javascript
|
|
134
|
-
// index.lightjs
|
|
135
|
-
html [
|
|
136
|
-
head []
|
|
137
|
-
body [
|
|
138
|
-
h1 '{{data.pageTitle}}'
|
|
139
|
-
]
|
|
140
|
-
]
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
### `lightjs.app()`
|
|
144
|
-
|
|
145
|
-
As mentioned above, Light.js ships with a built-in backend router. This router is exposed through the 'app' function, which returns a Node.js HTTP request handler.
|
|
146
|
-
|
|
147
|
-
```javascript
|
|
148
|
-
const http = require('http');
|
|
149
|
-
const light = require('lightjs');
|
|
150
|
-
|
|
151
|
-
const PORT = 4477;
|
|
152
|
-
|
|
153
|
-
const app = light.app({
|
|
154
|
-
publicDir: './public',
|
|
155
|
-
cache: true,
|
|
156
|
-
minify: true,
|
|
157
|
-
routes: {
|
|
158
|
-
// map a route directly to a light file
|
|
159
|
-
'/': 'src/index.light',
|
|
160
|
-
|
|
161
|
-
// or for more complex arrangements, you can map the route to the same arguments you would pass to light.compile()
|
|
162
|
-
'/blog/:id': {
|
|
163
|
-
input: 'src/blog.light',
|
|
164
|
-
data: async (req) => {
|
|
165
|
-
const post = 'hi' // get blog post with req.params.id
|
|
166
|
-
return post
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
http.createServer(app).listen(PORT);
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
Light.js's router is also completely compatible with Express:
|
|
176
|
-
|
|
177
|
-
```javascript
|
|
178
|
-
const http = require('http');
|
|
179
|
-
const light = require('lightjs');
|
|
180
|
-
const express = require('express');
|
|
181
|
-
|
|
182
|
-
const PORT = 4477;
|
|
183
|
-
|
|
184
|
-
const app = express();
|
|
185
|
-
|
|
186
|
-
const lightRouter = light.app({
|
|
187
|
-
cache: true,
|
|
188
|
-
minify: true,
|
|
189
|
-
routes: {
|
|
190
|
-
// ...
|
|
191
|
-
}
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
app.use(express.static('public'));
|
|
195
|
-
app.use(lightRouter);
|
|
196
|
-
|
|
197
|
-
http.createServer(app).listen(PORT);
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
Or, as previously mentioned, you can simply use `light.compile` with Express:
|
|
201
|
-
|
|
202
|
-
```javascript
|
|
203
|
-
const http = require('http');
|
|
204
|
-
const light = require('lightjs');
|
|
205
|
-
const express = require('express');
|
|
206
|
-
|
|
207
|
-
const PORT = 4477;
|
|
208
|
-
|
|
209
|
-
const app = express();
|
|
210
|
-
|
|
211
|
-
app.get('/', async (req, res) => {
|
|
212
|
-
const html = await light.compile({
|
|
213
|
-
input: 'src/index.light',
|
|
214
|
-
// for production
|
|
215
|
-
minify: true,
|
|
216
|
-
cache: true
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
res.end(html);
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
http.createServer(app).listen(PORT);
|
|
223
|
-
```
|
|
224
|
-
|
|
225
75
|
## Syntax
|
|
226
76
|
|
|
227
77
|
Light.js syntax is extremely terse but it is **not** whitespace sensitive. It's a much less shift-y version of HTML. You simply drop the angle brackets, and put children between square brackets. Tags without children as well as void tags are ended with an empty set of square brackets.
|
|
@@ -994,6 +844,156 @@ html [
|
|
|
994
844
|
]
|
|
995
845
|
```
|
|
996
846
|
|
|
847
|
+
## Usage
|
|
848
|
+
|
|
849
|
+
### `lightjs.compile()`
|
|
850
|
+
|
|
851
|
+
Fundamentally, Light.js is a library that takes in a Light.js source tree and outputs a single HTML file or string.
|
|
852
|
+
|
|
853
|
+
```javascript
|
|
854
|
+
const lightjs = require('lightjs');
|
|
855
|
+
|
|
856
|
+
lightjs.compile({
|
|
857
|
+
input: 'src/index.lightjs',
|
|
858
|
+
output: 'public/index.html'
|
|
859
|
+
});
|
|
860
|
+
```
|
|
861
|
+
|
|
862
|
+
No other step is required for a fully reactive, bundled, minified application.
|
|
863
|
+
|
|
864
|
+
This function may also be used to serve dynamic content in real time.
|
|
865
|
+
|
|
866
|
+
```javascript
|
|
867
|
+
const express = require('express');
|
|
868
|
+
const lightjs = require('lightjs');
|
|
869
|
+
|
|
870
|
+
const app = express();
|
|
871
|
+
|
|
872
|
+
app.get('/', async (req, res) => {
|
|
873
|
+
const html = await lightjs.compile({
|
|
874
|
+
input: 'src/index.lightjs'
|
|
875
|
+
});
|
|
876
|
+
res.end(html);
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
app.listen(3838);
|
|
880
|
+
```
|
|
881
|
+
|
|
882
|
+
In order for the above example to have production quality speed, you must enable caching. And if desired, Javascript minification can be enabled as well:
|
|
883
|
+
|
|
884
|
+
```javascript
|
|
885
|
+
const html = await lightjs.compile({
|
|
886
|
+
input: 'src/index.lightjs',
|
|
887
|
+
cache: true,
|
|
888
|
+
minify: true
|
|
889
|
+
});
|
|
890
|
+
```
|
|
891
|
+
|
|
892
|
+
The last thing to know about Light.js's compile function is document data.
|
|
893
|
+
|
|
894
|
+
```javascript
|
|
895
|
+
const html = await lightjs.compile({
|
|
896
|
+
input: 'src/about.lightjs',
|
|
897
|
+
data: {
|
|
898
|
+
pageTitle: 'About me'
|
|
899
|
+
}
|
|
900
|
+
});
|
|
901
|
+
```
|
|
902
|
+
|
|
903
|
+
In your Light.js document pageTitle can be found on the global 'data' object:
|
|
904
|
+
|
|
905
|
+
```javascript
|
|
906
|
+
// index.lightjs
|
|
907
|
+
html [
|
|
908
|
+
head []
|
|
909
|
+
body [
|
|
910
|
+
h1 '{{data.pageTitle}}'
|
|
911
|
+
]
|
|
912
|
+
]
|
|
913
|
+
```
|
|
914
|
+
|
|
915
|
+
### `lightjs.app()`
|
|
916
|
+
|
|
917
|
+
As mentioned above, Light.js ships with a built-in backend router. This router is exposed through the 'app' function, which returns a Node.js HTTP request handler.
|
|
918
|
+
|
|
919
|
+
```javascript
|
|
920
|
+
const http = require('http');
|
|
921
|
+
const light = require('lightjs');
|
|
922
|
+
|
|
923
|
+
const PORT = 4477;
|
|
924
|
+
|
|
925
|
+
const app = light.router({
|
|
926
|
+
publicDir: './public',
|
|
927
|
+
cache: true,
|
|
928
|
+
minify: true,
|
|
929
|
+
routes: {
|
|
930
|
+
// map a route directly to a light file
|
|
931
|
+
'/': 'src/index.light',
|
|
932
|
+
|
|
933
|
+
// or for more complex arrangements, you can map the route to the same arguments you would pass to light.compile()
|
|
934
|
+
'/blog/:id': {
|
|
935
|
+
input: 'src/blog.light',
|
|
936
|
+
data: async (req) => {
|
|
937
|
+
const post = 'hi' // get blog post with req.params.id
|
|
938
|
+
return post
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
})
|
|
943
|
+
|
|
944
|
+
http.createServer(app).listen(PORT);
|
|
945
|
+
```
|
|
946
|
+
|
|
947
|
+
Light.js's router is also completely compatible with Express:
|
|
948
|
+
|
|
949
|
+
```javascript
|
|
950
|
+
const http = require('http');
|
|
951
|
+
const light = require('lightjs');
|
|
952
|
+
const express = require('express');
|
|
953
|
+
|
|
954
|
+
const PORT = 4477;
|
|
955
|
+
|
|
956
|
+
const app = express();
|
|
957
|
+
|
|
958
|
+
const lightRouter = light.router({
|
|
959
|
+
cache: true,
|
|
960
|
+
minify: true,
|
|
961
|
+
routes: {
|
|
962
|
+
// ...
|
|
963
|
+
}
|
|
964
|
+
})
|
|
965
|
+
|
|
966
|
+
app.use(express.static('public'));
|
|
967
|
+
app.use(lightRouter);
|
|
968
|
+
|
|
969
|
+
http.createServer(app).listen(PORT);
|
|
970
|
+
```
|
|
971
|
+
|
|
972
|
+
Or, as previously mentioned, you can simply use `light.compile` with Express:
|
|
973
|
+
|
|
974
|
+
```javascript
|
|
975
|
+
const http = require('http');
|
|
976
|
+
const light = require('lightjs');
|
|
977
|
+
const express = require('express');
|
|
978
|
+
|
|
979
|
+
const PORT = 4477;
|
|
980
|
+
|
|
981
|
+
const app = express();
|
|
982
|
+
|
|
983
|
+
app.get('/', async (req, res) => {
|
|
984
|
+
const html = await light.compile({
|
|
985
|
+
input: 'src/index.light',
|
|
986
|
+
// for production
|
|
987
|
+
minify: true,
|
|
988
|
+
cache: true
|
|
989
|
+
});
|
|
990
|
+
|
|
991
|
+
res.end(html);
|
|
992
|
+
});
|
|
993
|
+
|
|
994
|
+
http.createServer(app).listen(PORT);
|
|
995
|
+
```
|
|
996
|
+
|
|
997
997
|
|
|
998
998
|
This is the entire feature set of Lightjs. If there is even one aspect of Lightjs that is missing from this document, it is considered a bug.
|
|
999
999
|
|
package/cli.js
CHANGED
|
@@ -137,6 +137,15 @@ for (let i = 0; i < process.argv.length; i++) {
|
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
break;
|
|
140
|
+
case '-i':
|
|
141
|
+
config.input = process.argv[++i];
|
|
142
|
+
break;
|
|
143
|
+
case '-o':
|
|
144
|
+
config.output = process.argv[++i];
|
|
145
|
+
break;
|
|
146
|
+
case '--minify':
|
|
147
|
+
config.minify = true;
|
|
148
|
+
break;
|
|
140
149
|
default:
|
|
141
150
|
break
|
|
142
151
|
}
|
|
@@ -158,6 +167,16 @@ if (config.dev) {
|
|
|
158
167
|
fs.writeFileSync(path.join(p, 'todo.light'), todoFile);
|
|
159
168
|
fs.writeFileSync(path.join(p, 'server.js'), quickServer);
|
|
160
169
|
fs.writeFileSync(path.join(p, 'package.json'), packageFile(config.create, pf.version));
|
|
170
|
+
} else if (config.input) {
|
|
171
|
+
if (config.output) {
|
|
172
|
+
light.compile({
|
|
173
|
+
input: config.input,
|
|
174
|
+
output: config.output,
|
|
175
|
+
minify: config.minify || false
|
|
176
|
+
});
|
|
177
|
+
} else {
|
|
178
|
+
light.compile(config.input).then((output) => console.log(output));
|
|
179
|
+
}
|
|
161
180
|
} else {
|
|
162
181
|
console.log(help);
|
|
163
182
|
}
|
package/core.js
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
|
|
22
22
|
module.exports = (config, fs, path) => {
|
|
23
23
|
const _files = {};
|
|
24
|
-
let includeRuntime
|
|
24
|
+
let includeRuntime;
|
|
25
25
|
|
|
26
26
|
function throw_error (err) {
|
|
27
27
|
err.origin = 'light';
|
|
@@ -1564,8 +1564,8 @@ module.exports = (config, fs, path) => {
|
|
|
1564
1564
|
} else {
|
|
1565
1565
|
node.children.forEach(walk);
|
|
1566
1566
|
if (node.data.name === 'head') {
|
|
1567
|
-
if (
|
|
1568
|
-
emit('<script>');
|
|
1567
|
+
if (js) {
|
|
1568
|
+
emit('<script type="module">');
|
|
1569
1569
|
emit(`document.addEventListener('DOMContentLoaded', function () {\n`);
|
|
1570
1570
|
emit(`(function (data){${js}})(${JSON.stringify(initial_state)})`);
|
|
1571
1571
|
emit('});');
|
|
@@ -2758,10 +2758,11 @@ module.exports = (config, fs, path) => {
|
|
|
2758
2758
|
};
|
|
2759
2759
|
|
|
2760
2760
|
const renderToAst = async (file, actions, _fileText) => {
|
|
2761
|
+
includeRuntime = false;
|
|
2761
2762
|
const fileText = file === 'default' ? _fileText : openFile(file);
|
|
2762
2763
|
const tokens = tokenize(fileText, file);
|
|
2763
2764
|
const ast = parse(tokens);
|
|
2764
|
-
let js
|
|
2765
|
+
let js;
|
|
2765
2766
|
if (includeRuntime) {
|
|
2766
2767
|
const runtime = generateRuntime(ast, actions);
|
|
2767
2768
|
|
package/index.js
CHANGED
|
@@ -53,6 +53,14 @@ const addParentPaths = (code, p) => {
|
|
|
53
53
|
return out;
|
|
54
54
|
};
|
|
55
55
|
|
|
56
|
+
const hideDynamicImports = (code) => {
|
|
57
|
+
return code.split('import(').join('__IMPORT_PLACEHOLDER__(');
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const restoreDynamicImports = (code) => {
|
|
61
|
+
return code.split('__IMPORT_PLACEHOLDER__(').join('import(');
|
|
62
|
+
};
|
|
63
|
+
|
|
56
64
|
const esbuild_to_light_error = (e, chunk) => {
|
|
57
65
|
let row = e.location.line - 1;
|
|
58
66
|
let col = e.location.column;
|
|
@@ -88,7 +96,8 @@ Light.compile = async (name, opts) => {
|
|
|
88
96
|
jsTransform: async (chunk) => {
|
|
89
97
|
try {
|
|
90
98
|
const opts = { format: 'cjs', loader: 'ts' };
|
|
91
|
-
const
|
|
99
|
+
const codeChunk = hideDynamicImports(chunk.code);
|
|
100
|
+
const result = await esbuild.transform(codeChunk, opts);
|
|
92
101
|
return addParentPaths(result.code, chunk.parent_dir);
|
|
93
102
|
} catch (e) {
|
|
94
103
|
e.origin = 'esbuild';
|
|
@@ -106,7 +115,8 @@ Light.compile = async (name, opts) => {
|
|
|
106
115
|
minify: opts.minify,
|
|
107
116
|
write: false
|
|
108
117
|
});
|
|
109
|
-
|
|
118
|
+
const output = restoreDynamicImports(result.outputFiles[0].text);
|
|
119
|
+
return output;
|
|
110
120
|
}
|
|
111
121
|
}, fs, path);
|
|
112
122
|
try {
|