Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Jan 2, 2024
1 parent 6f7d5b0 commit 26c7664
Show file tree
Hide file tree
Showing 42 changed files with 373 additions and 355 deletions.
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
"program": "${workspaceFolder}/dev/tests/unit.mjs"
},
{
"command": "cls\nnpm run test:dev:repl\nexit",
"command": "cls\nnpm run test:dev:repl",
"name": "启动REPL",
"request": "launch",
"type": "node-terminal"
},
{
"command": "cls\nnpm run build:dist\nexit",
"command": "cls\nnpm run build:dist",
"name": "构建dist",
"request": "launch",
"type": "node-terminal"
},
{
"command": "cls\nnpm run build:doc\nexit",
"command": "cls\nnpm run build:doc",
"name": "构建doc",
"request": "launch",
"type": "node-terminal"
Expand Down
166 changes: 83 additions & 83 deletions dev/build/build_code.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,59 @@ import { minify as terser } from 'terser';
import { minify as uglifyjs } from 'uglify-js';

//rollup -c ./.github/rollup.config.mjs
var rollup_config=await import('./rollup.config.mjs').then(m=>m.default);
for(const config of rollup_config){
var rollup_config = await import('./rollup.config.mjs').then(m => m.default);
for (const config of rollup_config) {
const bundle = await rollup(config);
for(const output of config.output)
for (const output of config.output)
await bundle.write(output);
}


var name_caches={};
function terser_minify(code,is_module){
var compress_options={
unsafe:true,
var name_caches = {};
function terser_minify(code, is_module) {
var compress_options = {
unsafe: true,
//inline:2,
computed_props:false,
unsafe_arrows:true,
unsafe_comps:true,
unsafe_Function:true,
unsafe_math:true,
unsafe_symbols:true,
unsafe_methods:true,
unsafe_proto:true,
unsafe_regexp:true,
unsafe_undefined:true
computed_props: false,
unsafe_arrows: true,
unsafe_comps: true,
unsafe_Function: true,
unsafe_math: true,
unsafe_symbols: true,
unsafe_methods: true,
unsafe_proto: true,
unsafe_regexp: true,
unsafe_undefined: true
};
return terser(code,{
compress:compress_options,
mangle:{
properties:{regex:/^_.*_$/},
keep_classnames:/^(base_sstp_info_t|list_info_t|fmo_info_t|ghost_events_queryer_t|jsstp_t|sstp_info_t)$/,
return terser(code, {
compress: compress_options,
mangle: {
properties: { regex: /^_.*_$/ },
keep_classnames: /^(base_sstp_info_t|list_info_t|fmo_info_t|ghost_events_queryer_t|jsstp_t|sstp_info_t)$/,
},
nameCache: name_caches,
module:is_module
}).then(({code})=>code);
module: is_module
}).then(({ code }) => code);
}
function uglifyjs_minify(code,is_module){
var compress_options={
unsafe:true,
unsafe_comps:true,
unsafe_Function:true,
unsafe_math:true,
unsafe_proto:true,
unsafe_regexp:true,
unsafe_undefined:true,
properties:false
function uglifyjs_minify(code, is_module) {
var compress_options = {
unsafe: true,
unsafe_comps: true,
unsafe_Function: true,
unsafe_math: true,
unsafe_proto: true,
unsafe_regexp: true,
unsafe_undefined: true,
properties: false
};
return new Promise((resolve,reject)=>{
let result=uglifyjs(code,{
compress:compress_options,
mangle:false,
module:is_module,
return new Promise((resolve, reject) => {
let result = uglifyjs(code, {
compress: compress_options,
mangle: false,
module: is_module,
//nameCache:name_caches
});
if(result.error)
if (result.error)
reject(result.error);
else
resolve(result.code);
Expand All @@ -67,75 +67,75 @@ function uglifyjs_minify(code,is_module){
import { readFileSync, writeFileSync } from 'fs';
import { pack } from 'packer';
//minify
async function jsstp_minify(code_path,is_module){
async function jsstp_minify(code_path, is_module) {
console.log(`minifing ${code_path}`);
let code=readFileSync(code_path,'utf8');
if(code.startsWith("Object.defineProperty(exports, '__esModule', { value: true });")){
code=code.substring("Object.defineProperty(exports, '__esModule', { value: true });".length);
code+="exports.__esModule=true;\n"
let code = readFileSync(code_path, 'utf8');
if (code.startsWith("Object.defineProperty(exports, '__esModule', { value: true });")) {
code = code.substring("Object.defineProperty(exports, '__esModule', { value: true });".length);
code += "exports.__esModule=true;\n"
}
return terser_minify(code,is_module).then(code => uglifyjs_minify(code,is_module)).then(async code=>{
if(is_module){
let exports_str="";
let exports_reg=/exports\.([a-zA-Z0-9_$]+)\s*=\s*([a-zA-Z0-9_$!]+)[,;]/;
return terser_minify(code, is_module).then(code => uglifyjs_minify(code, is_module)).then(async code => {
if (is_module) {
let exports_str = "";
let exports_reg = /exports\.([a-zA-Z0-9_$]+)\s*=\s*([a-zA-Z0-9_$!]+)[,;]/;
let match;
while(match=exports_reg.exec(code)){
exports_str+=`${match[1]}:${match[2]},`;
code=code.replace(match[0],"");
while (match = exports_reg.exec(code)) {
exports_str += `${match[1]}:${match[2]},`;
code = code.replace(match[0], "");
}
if(exports_str){
var assign=name_caches.vars.props["$assign"];
if(assign){
code=code.replace(/,$/,";");
code+=assign+"(exports,{"+exports_str.replace(/,$/,"});");
code=await uglifyjs_minify(code,is_module);
if (exports_str) {
var assign = name_caches.vars.props["$assign"];
if (assign) {
code = code.replace(/,$/, ";");
code += assign + "(exports,{" + exports_str.replace(/,$/, "});");
code = await uglifyjs_minify(code, is_module);
}
else
console.error("assign not found")
}
}
else{
code=code.replace(/^var [a-zA-Z0-9_$]+=function\(\){/,`var jsstp=(()=>{`);
code=code.replace(/}\(\);$/g,`})()\n`);
else {
code = code.replace(/^var [a-zA-Z0-9_$]+=function\(\){/, `var jsstp=(()=>{`);
code = code.replace(/}\(\);$/g, `})()\n`);
}
code = code.replace("document.currentScript&&document.currentScript.src","document.currentScript?.src");
code = code.replace("document.currentScript&&document.currentScript.src", "document.currentScript?.src");
code = code.replace(
"_=(s,r,n)=>{return new Proxy(s,e({get:(i=r,(e,s)=>{var r;if(!i.t?.(e,s))return(r=o(s,String)?i.i?.(e,s):i.h?.(e,s))!==t?r:i.o?i.o(e,s):o(r=e[s],Function)?r.bind(e):r}),set:u},n));var i}",
"_=(i,n,r)=>new Proxy(i,e({get(i,r){var a;if(!n.t?.(i,r))return(a=o(r,String)?n.i?.(i,r):n.h?.(i,r))!==t?a:n.o?n.o(i,r):o(a=i[r],Function)?a.bind(i):a},set:u},r))"
);
code = code.replace("t=>{return(t=+t)==t}","t=>+t==t");
return code.replace(/;$/g,`\n`);
}).catch(e=>console.error(e));
code = code.replace("t=>{return(t=+t)==t}", "t=>+t==t");
return code.replace(/;$/g, `\n`);
}).catch(e => console.error(e));
}


//minify
var cjscode = await jsstp_minify("./dist/jsstp.cjs",true);
var jscode = await jsstp_minify("./dist/jsstp.min.js",false);
if(!jscode.startsWith("var jsstp=(()=>{") || !jscode.endsWith("})()\n"))
var cjscode = await jsstp_minify("./dist/jsstp.cjs", true);
var jscode = await jsstp_minify("./dist/jsstp.min.js", false);
if (!jscode.startsWith("var jsstp=(()=>{") || !jscode.endsWith("})()\n"))
throw new Error("minify failed!");
var jsstpdefcode = jscode.substring("var jsstp=(()=>{".length,jscode.length-"})()\n".length).replace(
var jsstpdefcode = jscode.substring("var jsstp=(()=>{".length, jscode.length - "})()\n".length).replace(
/return (\w+)\(jsstp_t.prototype/g,
"$1(jsstp_t.prototype"
);
jsstpdefcode = jsstpdefcode.replace(/new jsstp_t$/g,"[new jsstp_t,jsstp_t,base_sstp_info_t,sstp_info_t,list_info_t,fmo_info_t,ghost_events_queryer_t]");
jsstpdefcode = jsstpdefcode.replace(/new jsstp_t$/g, "[new jsstp_t,jsstp_t,base_sstp_info_t,sstp_info_t,list_info_t,fmo_info_t,ghost_events_queryer_t]");
//pack
jscode=pack(jscode,true,true);
cjscode=pack(cjscode,true,true);
jsstpdefcode=pack(jsstpdefcode,true,true);
jscode = pack(jscode, true, true);
cjscode = pack(cjscode, true, true);
jsstpdefcode = pack(jsstpdefcode, true, true);
//build mjs code
var mjscode="var[a,b,c,d,e,f,g]="+jsstpdefcode+"\
var mjscode = "var[a,b,c,d,e,f,g]=" + jsstpdefcode + "\
\nexport{a as default,a as jsstp,b as jsstp_t,c as base_sstp_info_t,d as sstp_info_t,e as list_info_t,f as fmo_info_t,g as ghost_events_queryer_t}"
//re minify
name_caches={}
function reminify(code,is_module){
return terser_minify(code,is_module).then(code => uglifyjs_minify(code,is_module)).then(code=>code.replace(/;$/g,`\n`)).catch(e=>console.error(e));
name_caches = {}
function reminify(code, is_module) {
return terser_minify(code, is_module).then(code => uglifyjs_minify(code, is_module)).then(code => code.replace(/;$/g, `\n`)).catch(e => console.error(e));
}
jscode=await reminify(jscode,false);
cjscode=await reminify(cjscode,false);//作为普通的js而非模块
mjscode=await reminify(mjscode,true);
jscode = await reminify(jscode, false);
cjscode = await reminify(cjscode, false);//作为普通的js而非模块
mjscode = await reminify(mjscode, true);
//write
writeFileSync("./dist/jsstp.min.js",jscode);
writeFileSync("./dist/jsstp.cjs",cjscode);
writeFileSync("./dist/jsstp.mjs",mjscode);
writeFileSync("./dist/jsstp.min.js", jscode);
writeFileSync("./dist/jsstp.cjs", cjscode);
writeFileSync("./dist/jsstp.mjs", mjscode);
//*/
56 changes: 28 additions & 28 deletions dev/build/build_dts.mjs
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@

import { rollup } from 'rollup';
import { readdirSync ,writeFileSync } from 'fs';
import { readdirSync, writeFileSync } from 'fs';

import dts from "rollup-plugin-dts";

async function build_dts_file(lang){
async function build_dts_file(lang) {
console.log(`正在编译${lang}语言的.d.ts文件`);
const path=`./src/.decls/${lang}`;
const rollup_config={
input: path+'/jsstp.d.ts',
const path = `./src/.decls/${lang}`;
const rollup_config = {
input: path + '/jsstp.d.ts',
output: [
{
format: 'esm'
}
],
plugins: [dts()]
}
var bundle=await rollup(rollup_config)
var dts_code= (await bundle.generate(rollup_config.output[0])).output[0].code;
var bundle = await rollup(rollup_config)
var dts_code = (await bundle.generate(rollup_config.output[0])).output[0].code;

//修补d.ts
//`info_object$1`=>`info_object`
dts_code=dts_code.replace(/info_object\$1/g,'info_object');
dts_code = dts_code.replace(/info_object\$1/g, 'info_object');
// `some ${string}` => string
dts_code=dts_code.replace(/`some \${string}`/g,'string');
dts_code = dts_code.replace(/`some \${string}`/g, 'string');

{
var dts_code_array=dts_code.split("\n");
var dts_code_array = dts_code.split("\n");
//remove all single line comments
dts_code_array=dts_code_array.filter(line=>!/^[ \t]*\/\//.test(line));
dts_code_array = dts_code_array.filter(line => !/^[ \t]*\/\//.test(line));
//remove all multi line comments
var in_comment=false;
dts_code_array=dts_code_array.filter(line=>{
if(/[ \t]*\/\*(?![\*@])/.test(line))
in_comment=true;
if(in_comment && /\*\/$/.test(line)){
in_comment=false;
var in_comment = false;
dts_code_array = dts_code_array.filter(line => {
if (/[ \t]*\/\*(?![\*@])/.test(line))
in_comment = true;
if (in_comment && /\*\/$/.test(line)) {
in_comment = false;
return false;
}
if(!in_comment)
if (!in_comment)
return true;
return false;
});
//remove all multi empty lines to single empty line
var last_line_is_empty=false;
dts_code_array=dts_code_array.filter(line=>{
if(line.length==0){
if(last_line_is_empty)
var last_line_is_empty = false;
dts_code_array = dts_code_array.filter(line => {
if (line.length == 0) {
if (last_line_is_empty)
return false;
else
last_line_is_empty=true;
}else
last_line_is_empty=false;
last_line_is_empty = true;
} else
last_line_is_empty = false;
return true;
});
dts_code=dts_code_array.join("\n");
dts_code = dts_code_array.join("\n");
}

writeFileSync(`dist/${lang}/jsstp.d.ts`,dts_code);
writeFileSync(`dist/${lang}/jsstp.d.ts`, dts_code);
}
//对于每个在src/.decls/中的文件夹,我们都需要调用一次build_dts_file
for(const lang of readdirSync("./src/.decls"))
for (const lang of readdirSync("./src/.decls"))
build_dts_file(lang);
4 changes: 2 additions & 2 deletions dev/link.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const repoPath = join(__dirname, '..')
process.chdir(process.env.USERPROFILE);

// 测试node_modules/jsstp是否存在
if(!existsSync(join(process.env.USERPROFILE, 'node_modules', 'jsstp'))) {
if (!existsSync(join(process.env.USERPROFILE, 'node_modules', 'jsstp'))) {
// 如果不存在则创建软链接
execSync(`npm link ${repoPath}`);
}

// 测试node_modules是否存在
if(!existsSync(join(repoPath, 'node_modules'))) {
if (!existsSync(join(repoPath, 'node_modules'))) {
// 如果不存在则创建软链接
execSync(`cmd /c mklink /j "${join(repoPath, 'node_modules')}" "${join(process.env.USERPROFILE, 'node_modules')}"`);
}
Expand Down
24 changes: 12 additions & 12 deletions dev/tests/base_jsstp_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@
var base_jsstp_test = (
/** @type {typeof import("jsstp").jsstp} */
jsstp
)=>{
jsstp.if_available(async ()=>{
) => {
jsstp.if_available(async () => {
console.log("jsstp test");
console.log((await jsstp.get_fmo_infos()).text_content);

console.log((await jsstp.OnTest()).text_content);
console.log(await jsstp.has_event("OnTest"));
console.log(jsstp.clone);

jsstp.On_ShioriEcho.GetName().then(({GhostName})=>
GhostName=="Taromati2"?
console.log("cool Taromati2!"):
console.log(GhostName||"ghost not support On_ShioriEcho.GetName")
jsstp.On_ShioriEcho.GetName().then(({ GhostName }) =>
GhostName == "Taromati2" ?
console.log("cool Taromati2!") :
console.log(GhostName || "ghost not support On_ShioriEcho.GetName")
);

jsstp.for_all_ghosts(jsstp => jsstp.OnTest().then(console.log));

console.log(JSON.stringify(await jsstp.On_ShioriEcho('1000-7'),null,'\t'));
}).catch((e)=>
e?
console.error(e):
console.log(JSON.stringify(await jsstp.On_ShioriEcho('1000-7'), null, '\t'));
}).catch((e) =>
e ?
console.error(e) :
console.log("none ghost was found")
);

if(jsstp.default_security_level!="local")
if (jsstp.default_security_level != "local")
console.error("jsstp.default_security_level!=local !!!!!!!");
};

export{
export {
base_jsstp_test as default,
base_jsstp_test,
};
Loading

0 comments on commit 26c7664

Please sign in to comment.