Skip to content

Commit 402e309

Browse files
authored
Merge pull request #3 from openwebf/feat/inline_html
feat: add html transform
2 parents 08ba5ed + db871dc commit 402e309

307 files changed

Lines changed: 59506 additions & 126810 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.DS_Store
2+
13
# Logs
24
logs
35
*.log

README.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,61 @@ The WBC file is a specially designed binary file that contains pre-compiled Java
1010
npm install @openwebf/wbc --save
1111
```
1212

13-
## Usage
13+
## Cli Usage
14+
15+
**Install**
16+
```
17+
npm install @openwebf/wbc -g
18+
```
19+
20+
**Convert JavaScript Into WBC file**
21+
22+
```
23+
wbc -s /tmp/index.js -d /tmp
24+
```
25+
26+
**Transform Inline Scripts in HTML**
27+
28+
```
29+
wbc -s ./demo.html -d ./demo_wbc.html --convert-html
30+
```
31+
32+
## Node Usage
33+
34+
**Convert JavaScript Into WBC buffer**
35+
36+
```javascript
37+
const { compileJavaScriptToWbc } = require('@openwebf/wbc');
38+
39+
compileJavaScriptToWbc('function hello() { return 1 + 1};'); // <Buffer ...> the WBC bytes
40+
```
41+
42+
**Transform Inline JavaScript Codes into WBC**
1443

1544
```javascript
16-
const WBC = require('@openwebf/wbc');
17-
const wbc = new WBC();
45+
const { transformInlineScriptToWbc } = require('@openwebf/wbc');
46+
47+
const transformedHtml = transformInlineScriptToWbc(`
48+
<!DOCTYPE html>
49+
<html lang="en">
50+
<body>
51+
<script>
52+
console.log('helloworld');
53+
</script>
54+
</body>
55+
</html>
56+
`);
57+
58+
console.log(transformedHtml); /*
59+
<html lang="en">
60+
<body>
61+
<script>
62+
// The WBC binary contents
63+
</script>
64+
65+
</body></html>
66+
*/
1867

19-
// Dump bytecode from javascript source;
20-
wbc.compile('function hello() { return 1 + 1};'); // <Buffer ...>
2168
```
2269

2370
## Contribute

bin/wbc.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env node
2-
const Wbc = require('./wbc_lib').Wbc;
32
const path = require('path');
43
const fs = require('fs');
54
const { program } = require('commander');
65
const packageConfig = require('../package.json');
7-
const Qjsc = require('../index');
6+
const { compileJavaScriptToWbc, transformInlineScriptToWbc, legacyCompileJavaScriptToKBC } = require('../index');
87

98
program
109
.version(packageConfig.version)
1110
.description('The WBC file generator')
1211
.requiredOption('-s, --source <source>', 'the Javascript source file path')
1312
.requiredOption('-d, --dist <dist>', 'the generated bytecode file path')
1413
.option('--legacy_kbc1', 'generate legacy kbc1 file, (compact with openkraken project)')
14+
.option('--convert-html', 'Given an HTML string as input, convert all inline JavaScript code to WBC format.')
1515
.parse(process.argv);
1616

1717
let options = program.opts();
@@ -30,21 +30,22 @@ if (!path.isAbsolute(dist)) {
3030
dist = path.join(process.cwd(), dist);
3131
}
3232

33-
const qjsc = new Qjsc();
34-
3533
const sourceFileName = source.split('/').slice(-1)[0].split('.')[0];
36-
const sourceCode = fs.readFileSync(source, {encoding: 'utf-8'});
37-
38-
let buffer = qjsc.compile(sourceCode);
39-
40-
if (type == 'kbc1') {
41-
let distPath = path.join(dist, sourceFileName + '.kbc1');
42-
fs.writeFileSync(distPath, buffer);
43-
console.log('Quickjs bytecode generated kbc1 at: \n' + distPath);
44-
} else if (type == 'wbc') {
45-
const wbc = new Wbc();
46-
let wbcBytecode = wbc.generateWbcBytecode(buffer);
47-
let distPath = path.join(dist, sourceFileName + '.wbc1');
48-
fs.writeFileSync(distPath, wbcBytecode);
49-
console.log('Quickjs bytecode generated wbc1 at: \n' + distPath);
34+
const sourceCode = fs.readFileSync(source, { encoding: 'utf-8' });
35+
36+
if (options.convertHtml) {
37+
let distPath = path.join(dist, sourceFileName + '.bhtml');
38+
const output = transformInlineScriptToWbc(sourceCode);
39+
fs.writeFileSync(distPath, output);
40+
console.log('Quickjs bytecode generated at: \n' + distPath);
41+
} else {
42+
if (type == 'kbc1') {
43+
let distPath = path.join(dist, sourceFileName + '.kbc1');
44+
fs.writeFileSync(distPath, legacyCompileJavaScriptToKBC(sourceCode));
45+
console.log('Quickjs bytecode generated kbc1 at: \n' + distPath);
46+
} else if (type == 'wbc') {
47+
let distPath = path.join(dist, sourceFileName + '.wbc1');
48+
fs.writeFileSync(distPath, compileJavaScriptToWbc(sourceCode));
49+
console.log('Quickjs bytecode generated wbc1 at: \n' + distPath);
50+
}
5051
}

bin/wbc_lib.js

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)