@qwerchuan/little-byte
v0.1.5
Published
Node.js bytecode compiler
Readme
little-byte
将 Node.js 应用代码编译为字节码
安装
$ npm install --save-dev little-byte编译应用
准备编译脚本
创建 build/index.js
const { walker } = require('little-byte').default;
const path = require('path');
walker.start({
inputDir: path.join(__dirname, '../src'),
outputDir: path.join(__dirname, '../dist'),
onFile(fileInfo, defaultAction) {
if (fileInfo.relativePath.startsWith('foobar/')) {
return 'ignore';
}
if (fileInfo.ext === '.jpg') {
return 'ignore';
}
if (fileInfo.name === 'my-dog.txt') {
return 'ignore';
}
if (fileInfo.isScript) {
return 'compile';
} else {
// copy none-js files to dist folder
return 'copy';
}
}
});编译
$ node build/index.js运行编译后的应用
创建 app-entry.js
require('little-byte');
// now you can require *.bytecode
require('./dist/index');
限制
编译和运行字节码需要使用相同版本的 Node.js
不同版本的 Node.js 可能使用不同的字节码格式
字节码不能保护代码中的常量值
可以使用16进制编辑器从字节码中恢复出字符串等常量的值
API
littleByte.compiler.compileFile(filePath: string, outputDir?: string): Promise<void>
littleByte.loader.loadBytecode(filePath: string): vm.Script;
littleByte.loader.execByteCode(filePath: string): any;
type WalkAction = 'ignore' | 'compile' | 'copy';
type FileInfo = {
path: string;
relativePath: string;
name: string;
ext: string;
isScript: boolean;
};
interface WalkOptions {
silent?: boolean;
inputDir: string;
outputDir: string;
onFile: (fileinfo: FileInfo, defaultAction: WalkAction) => WalkAction;
}
littleByte.walker.start(options: WalkOptions): Promise<void>;
