Skip to content

Commit

Permalink
Merge pull request #3 from openwebf/feat/inline_html
Browse files Browse the repository at this point in the history
feat: add html transform
  • Loading branch information
andycall committed Sep 17, 2024
2 parents 08ba5ed + db871dc commit 402e309
Show file tree
Hide file tree
Showing 307 changed files with 59,506 additions and 126,810 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store

# Logs
logs
*.log
Expand Down
57 changes: 52 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,61 @@ The WBC file is a specially designed binary file that contains pre-compiled Java
npm install @openwebf/wbc --save
```

## Usage
## Cli Usage

**Install**
```
npm install @openwebf/wbc -g
```

**Convert JavaScript Into WBC file**

```
wbc -s /tmp/index.js -d /tmp
```

**Transform Inline Scripts in HTML**

```
wbc -s ./demo.html -d ./demo_wbc.html --convert-html
```

## Node Usage

**Convert JavaScript Into WBC buffer**

```javascript
const { compileJavaScriptToWbc } = require('@openwebf/wbc');

compileJavaScriptToWbc('function hello() { return 1 + 1};'); // <Buffer ...> the WBC bytes
```

**Transform Inline JavaScript Codes into WBC**

```javascript
const WBC = require('@openwebf/wbc');
const wbc = new WBC();
const { transformInlineScriptToWbc } = require('@openwebf/wbc');

const transformedHtml = transformInlineScriptToWbc(`
<!DOCTYPE html>
<html lang="en">
<body>
<script>
console.log('helloworld');
</script>
</body>
</html>
`);

console.log(transformedHtml); /*
<html lang="en">
<body>
<script>
// The WBC binary contents
</script>
</body></html>
*/

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

## Contribute
Expand Down
37 changes: 19 additions & 18 deletions bin/wbc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env node
const Wbc = require('./wbc_lib').Wbc;
const path = require('path');
const fs = require('fs');
const { program } = require('commander');
const packageConfig = require('../package.json');
const Qjsc = require('../index');
const { compileJavaScriptToWbc, transformInlineScriptToWbc, legacyCompileJavaScriptToKBC } = require('../index');

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

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

const qjsc = new Qjsc();

const sourceFileName = source.split('/').slice(-1)[0].split('.')[0];
const sourceCode = fs.readFileSync(source, {encoding: 'utf-8'});

let buffer = qjsc.compile(sourceCode);

if (type == 'kbc1') {
let distPath = path.join(dist, sourceFileName + '.kbc1');
fs.writeFileSync(distPath, buffer);
console.log('Quickjs bytecode generated kbc1 at: \n' + distPath);
} else if (type == 'wbc') {
const wbc = new Wbc();
let wbcBytecode = wbc.generateWbcBytecode(buffer);
let distPath = path.join(dist, sourceFileName + '.wbc1');
fs.writeFileSync(distPath, wbcBytecode);
console.log('Quickjs bytecode generated wbc1 at: \n' + distPath);
const sourceCode = fs.readFileSync(source, { encoding: 'utf-8' });

if (options.convertHtml) {
let distPath = path.join(dist, sourceFileName + '.bhtml');
const output = transformInlineScriptToWbc(sourceCode);
fs.writeFileSync(distPath, output);
console.log('Quickjs bytecode generated at: \n' + distPath);
} else {
if (type == 'kbc1') {
let distPath = path.join(dist, sourceFileName + '.kbc1');
fs.writeFileSync(distPath, legacyCompileJavaScriptToKBC(sourceCode));
console.log('Quickjs bytecode generated kbc1 at: \n' + distPath);
} else if (type == 'wbc') {
let distPath = path.join(dist, sourceFileName + '.wbc1');
fs.writeFileSync(distPath, compileJavaScriptToWbc(sourceCode));
console.log('Quickjs bytecode generated wbc1 at: \n' + distPath);
}
}
150 changes: 0 additions & 150 deletions bin/wbc_lib.js

This file was deleted.

Loading

0 comments on commit 402e309

Please sign in to comment.