Mon 21 Jul 22:43:21 CEST 2025
This commit is contained in:
parent
2eec2d6266
commit
1f776f9aae
213
js/top/makeweblib
Normal file
213
js/top/makeweblib
Normal file
|
@ -0,0 +1,213 @@
|
|||
#!/usr/bin/node
|
||||
// Make a standalone and self-contained JS libraray publishing main file exports (as <MAIN> name object)
|
||||
// (WEB edition)
|
||||
// Version 1.6.3
|
||||
|
||||
var TOP='/home/sbosse/proj/jam/js';
|
||||
global.PATH=[process.cwd(),TOP];
|
||||
global.MODE='compile';
|
||||
global.TARGET='browser';
|
||||
window={};
|
||||
document={};
|
||||
XMLHttpRequest=function(){};
|
||||
XMLHttpRequest.prototype.open=function () {};
|
||||
location={};
|
||||
|
||||
|
||||
var Fs = require('fs');
|
||||
var Path = require("path");
|
||||
|
||||
var Modules = [];
|
||||
var Objects = [];
|
||||
var Files = [];
|
||||
var FilesEmbedded = {};
|
||||
var Main = undefined;
|
||||
var Bundle = 'bundle.js';
|
||||
var shift=0;
|
||||
|
||||
function FileExtension(filename) {
|
||||
return filename.split('.').pop();
|
||||
}
|
||||
|
||||
function search(index,file) {
|
||||
if (PATH.length==index) return file;
|
||||
var path=PATH[index];
|
||||
if (Fs.existsSync(path+'/'+file+'.js')) return path+'/'+file+'.js';
|
||||
else if (Fs.existsSync(path+'/'+file)) return path+'/'+file;
|
||||
else return search(index+1,file);
|
||||
}
|
||||
|
||||
var CoreModule = {};
|
||||
CoreModule['com/io']='com/io.browser';
|
||||
CoreModule['crypto']='os/crypto';
|
||||
CoreModule['util']='os/util';
|
||||
CoreModule['http']='os/http.browser';
|
||||
CoreModule['url']='os/url';
|
||||
CoreModule['path']='os/path';
|
||||
CoreModule['string_decoder']='os/string_decoder';
|
||||
CoreModule['fs']='';
|
||||
CoreModule['stream']='';
|
||||
CoreModule['fs']='';
|
||||
CoreModule['zlib']='';
|
||||
CoreModule['dgram']='';
|
||||
CoreModule['net']='';
|
||||
CoreModule['child_process']='';
|
||||
CoreModule['dns']='';
|
||||
|
||||
Require = function (module,noeval) {
|
||||
var file,filepath;
|
||||
try {
|
||||
if (CoreModule[module]!=undefined) module=CoreModule[module];
|
||||
if (module=='') return undefined;
|
||||
if (Modules[module]) return Modules[module];
|
||||
else if (Objects[module]) return Objects[module];
|
||||
file=search(0,module);
|
||||
filepath=Fs.realpathSync(file);
|
||||
Files.push([module,filepath]);
|
||||
if (FileExtension(filepath)=='json') {
|
||||
var Object = noeval?Fs.readFileSync(file):require(file);
|
||||
Objects[module]=Object;
|
||||
return Object;
|
||||
} else {
|
||||
var Module = noeval?Fs.readFileSync(file):require(file);
|
||||
Modules[module]=Module;
|
||||
return Module;
|
||||
}
|
||||
} catch (e) {
|
||||
var more='';
|
||||
if (e.name==='SyntaxError' && filepath) {
|
||||
var src=Fs.readFileSync(filepath,'utf8');
|
||||
var Esprima = Require('parser/esprima');
|
||||
try {
|
||||
var ast = Esprima.parse(src, { tolerant: true, loc:true });
|
||||
if (ast.errors && ast.errors.length>0) more = ', '+ast.errors[0];
|
||||
} catch (e) {
|
||||
if (e.lineNumber) more = ', in line '+e.lineNumber;
|
||||
}
|
||||
}
|
||||
if (e.stack) console.log(e.stack);
|
||||
console.log('Require import of '+module+' failed: '+e+more);
|
||||
process.exit(-1);
|
||||
}
|
||||
}
|
||||
global.Require=Require;
|
||||
|
||||
|
||||
FileEmbedd = function (path,format) {
|
||||
var fullpath=search(0,path);
|
||||
console.log('*'+path);
|
||||
try {
|
||||
FilesEmbedded[path]=Fs.readFileSync(fullpath,format);
|
||||
console.log('#'+path+':'+FilesEmbedded[path].length+' bytes');
|
||||
} catch (e) {
|
||||
console.log('FileEmbedd failed for: '+fullpath)
|
||||
process.exit();};
|
||||
}
|
||||
global.FileEmbedd = FileEmbedd;
|
||||
|
||||
FileEmbedded = function (path,format) {
|
||||
if (FilesEmbedded[path]) return FilesEmbedded[path];
|
||||
else {
|
||||
FileEmbedd(path,format);
|
||||
return FilesEmbedded[path];
|
||||
}
|
||||
}
|
||||
global.FileEmbedded = FileEmbedded;
|
||||
|
||||
var Base64 = require(TOP+'/os/base64');
|
||||
var Comp = require(TOP+'/com/compat');
|
||||
|
||||
var exit=false;
|
||||
var argv=[];
|
||||
|
||||
Comp.args.parse(process.argv,[
|
||||
['-exit',0,function (arg) {
|
||||
if (!Main) exit=true;
|
||||
else argv.push(arg);}],
|
||||
['-o',1,function (arg) {
|
||||
if (!Main) Bundle=arg;
|
||||
else argv.push(arg);}],
|
||||
[function (arg) {
|
||||
if (!Main) Main=arg;
|
||||
argv.push(arg);
|
||||
}]
|
||||
],2);
|
||||
|
||||
|
||||
if (!Main) {
|
||||
console.log('usage: makeapp [-o <bundle.js>] <main.js> [<program args>]');
|
||||
process.exit();
|
||||
}
|
||||
|
||||
var MainModule = Path.basename(Main).replace(/\..+/,'').toUpperCase();
|
||||
|
||||
process.argv=Comp.array.concat(['node'],argv);
|
||||
|
||||
var Base64=Require('os/base64');
|
||||
var Buffer=Require('os/buffer');
|
||||
var Process=Require('os/process');
|
||||
|
||||
if (Main) Require(Main);
|
||||
//Files=Files.slice(0,8);
|
||||
var NL = '\n';
|
||||
var coremodules = "";
|
||||
for(var m in CoreModule) {
|
||||
coremodules = coremodules + "CoreModule['"+m+"']='"+CoreModule[m]+"';"+NL;
|
||||
}
|
||||
var header=
|
||||
'function '+MainModule+'init() { var CoreModule = {};'+NL+
|
||||
coremodules+NL+
|
||||
'var BundleModuleCode=[];'+NL+
|
||||
'var BundleObjectCode=[];'+NL+
|
||||
'var BundleModules = [];'+NL+
|
||||
'PATH=[".","'+TOP+'"];'+NL+
|
||||
'if (typeof global == "undefined") global=(typeof window != "undefined"?window:{})'+NL+
|
||||
'if (typeof process == "undefined") var process={};'+NL+
|
||||
'Require=function(modupath) {'+NL+
|
||||
" if (CoreModule[modupath]!=undefined) modupath=CoreModule[modupath];"+NL+
|
||||
" if (modupath=='') return undefined;"+NL+
|
||||
' if (BundleModules[modupath]) return BundleModules[modupath];'+NL+
|
||||
' var exports={}, module={exports:exports};'+NL+
|
||||
' if (BundleModuleCode[modupath]) BundleModuleCode[modupath](module,exports,window,process);'+NL+
|
||||
' else if (BundleObjectCode[modupath]) BundleObjectCode[modupath](module,exports,window,process);'+NL+
|
||||
' else return undefined;'+NL+
|
||||
' BundleModules[modupath]=module.exports||module;'+NL+
|
||||
' return module.exports||module;};'+NL+
|
||||
'var FilesEmbedded = {};'+NL+
|
||||
'var FileEmbedd = function (path,format) {};'+NL+
|
||||
'var FileEmbedded = function (path,format) {return FilesEmbedded[path](format);};'+NL+
|
||||
"global.TARGET='"+global.TARGET+"';"+NL+
|
||||
"if (typeof self != 'undefined' && typeof window == 'undefined') window=self;"+NL;
|
||||
|
||||
var trailer=
|
||||
"Base64=Require('os/base64');"+NL+
|
||||
"Buffer=Require('os/buffer').Buffer;"+NL+
|
||||
"process=Require('os/process');"+NL+
|
||||
"if (typeof window != 'undefined') window."+MainModule+"="+MainModule+" = Require('"+Main+"');"+NL+
|
||||
"}; "+MainModule+"init();"+NL;
|
||||
var code = '';
|
||||
for (var file in Files) {
|
||||
var modupath=Files[file][0];
|
||||
var path=Files[file][1];
|
||||
console.log('+'+path);
|
||||
if (FileExtension(path)=='json')
|
||||
code=code+"BundleObjectCode['"+modupath+"']=function (module,exports){\nexports="+Fs.readFileSync(path,'utf8')+'};\n';
|
||||
else
|
||||
code=code+"BundleModuleCode['"+modupath+"']=function (module,exports,global,process){\n"+Fs.readFileSync(path,'utf8')+'};\n';
|
||||
}
|
||||
for (var file in FilesEmbedded) {
|
||||
var data = FilesEmbedded[file];
|
||||
console.log('+'+file);
|
||||
if (typeof data == 'string')
|
||||
code=code+"FilesEmbedded['"+file+"']=function (format){return Base64.decode('"+Base64.encode(data)+"')};\n";
|
||||
else
|
||||
code=code+"FilesEmbedded['"+file+"']=function (format){return Base64.decodeBuf('"+Base64.encodeBuf(data)+"')};\n";
|
||||
}
|
||||
|
||||
console.log('Writing bundled JS file '+Bundle);
|
||||
Fs.writeFileSync(Bundle, header+NL+code+NL+trailer, 'utf8');
|
||||
|
||||
console.log('Finished. Exit ..');
|
||||
if (exit) process.exit();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user