Skip to content

Commit

Permalink
run format
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Jun 14, 2024
1 parent b026b82 commit d81e8bb
Show file tree
Hide file tree
Showing 26 changed files with 419 additions and 420 deletions.
100 changes: 50 additions & 50 deletions dev/build/build_code.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@

import { rollup } from 'rollup';
import { minify as terser } from 'terser';
import { minify as uglifyjs } from 'uglify-js';
import { rollup } from 'rollup'
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);
var rollup_config = await import('./rollup.config.mjs').then(m => m.default)
for (const config of rollup_config) {
const bundle = await rollup(config);
const bundle = await rollup(config)
for (const output of config.output)
await bundle.write(output);
await bundle.write(output)
}


var name_caches = {};
var name_caches = {}
function terser_minify(code, is_module) {
var compress_options = {
unsafe: true,
Expand All @@ -27,7 +27,7 @@ function terser_minify(code, is_module) {
unsafe_proto: true,
unsafe_regexp: true,
unsafe_undefined: true
};
}
return terser(code, {
compress: compress_options,
mangle: {
Expand All @@ -36,7 +36,7 @@ function terser_minify(code, is_module) {
},
nameCache: name_caches,
module: is_module
}).then(({ code }) => code);
}).then(({ code }) => code)
}
function uglifyjs_minify(code, is_module) {
var compress_options = {
Expand All @@ -48,94 +48,94 @@ function uglifyjs_minify(code, is_module) {
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,
//nameCache:name_caches
});
})
if (result.error)
reject(result.error);
reject(result.error)
else
resolve(result.code);
});
resolve(result.code)
})
}

//file io
import { readFileSync, writeFileSync } from 'fs';
import { pack } from 'packer';
import { readFileSync, writeFileSync } from 'fs'
import { pack } from 'packer'
//minify
async function jsstp_minify(code_path, is_module) {
console.log(`minifing ${code_path}`);
let code = readFileSync(code_path, 'utf8');
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 = 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_$!]+)[,;]/;
let match;
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], "");
exports_str += `${match[1]}:${match[2]},`
code = code.replace(match[0], "")
}
if (exports_str) {
var assign = name_caches.vars.props["$assign"];
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);
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`);
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);
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!");
throw new Error("minify failed!")
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 + "\
\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));
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,12 +1,12 @@

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

import dts from "rollup-plugin-dts";
import dts from "rollup-plugin-dts"

async function build_dts_file(lang) {
console.log(`正在编译${lang}语言的.d.ts文件`);
const path = `./src/.decls/${lang}`;
console.log(`正在编译${lang}语言的.d.ts文件`)
const path = `./src/.decls/${lang}`
const rollup_config = {
input: path + '/jsstp.d.ts',
output: [
Expand All @@ -17,48 +17,48 @@ async function build_dts_file(lang) {
plugins: [dts()]
}
var bundle = await rollup(rollup_config)
var dts_code = (await bundle.generate(rollup_config.output[0])).output[0].code;
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;
var in_comment = false
dts_code_array = dts_code_array.filter(line => {
if (/[ \t]*\/\*(?![\*@])/.test(line))
in_comment = true;
in_comment = true
if (in_comment && /\*\/$/.test(line)) {
in_comment = false;
return false;
in_comment = false
return false
}
if (!in_comment)
return true;
return false;
});
return true
return false
})
//remove all multi empty lines to single empty line
var last_line_is_empty = false;
var last_line_is_empty = false
dts_code_array = dts_code_array.filter(line => {
if (line.length == 0) {
if (line.length == 0)
if (last_line_is_empty)
return false;
return false
else
last_line_is_empty = true;
} else
last_line_is_empty = false;
return true;
});
dts_code = dts_code_array.join("\n");
last_line_is_empty = true
else
last_line_is_empty = false
return true
})
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"))
build_dts_file(lang);
build_dts_file(lang)
32 changes: 16 additions & 16 deletions dev/build/docs-language-sidebar.mjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// @ts-check
import td from "typedoc";
import path from "path";
import td from "typedoc"
import path from "path"

const template = `
<select onchange="location.href = event.target.selectedOptions[0].dataset.url + location.hash;">
<option data-url="{ROOT}/JP/{PAGE}" lang="ja">日本語</option>
<option data-url="{ROOT}/EN/{PAGE}" lang="en">English</option>
<option data-url="{ROOT}/CN/{PAGE}" lang="zh-CN">中文 (简体)</option>
<option data-url="{ROOT}/JP/{PAGE}" lang="ja">日本語</option>
<option data-url="{ROOT}/EN/{PAGE}" lang="en">English</option>
<option data-url="{ROOT}/CN/{PAGE}" lang="zh-CN">中文 (简体)</option>
</select>
`;
`

/** @param {td.Application} app */
export function load(app) {
app.renderer.hooks.on("pageSidebar.begin", (context) => {
const language = app.options.getValue("htmlLang");
app.renderer.hooks.on("pageSidebar.begin", (context) => {
const language = app.options.getValue("htmlLang")

const root = path.posix.join(context.relativeURL("./"), "..");
const html = template
.trim()
.replace(/\{ROOT\}/g, root)
.replace(/\{PAGE\}/g, context.page.url)
.replace(`lang="${language}"`, `lang="${language}" selected`);
const root = path.posix.join(context.relativeURL("./"), "..")
const html = template
.trim()
.replace(/\{ROOT\}/g, root)
.replace(/\{PAGE\}/g, context.page.url)
.replace(`lang="${language}"`, `lang="${language}" selected`)

return td.JSX.createElement(td.JSX.Raw, { html });
});
return td.JSX.createElement(td.JSX.Raw, { html })
})
}
2 changes: 1 addition & 1 deletion dev/build/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export default [
"fs",
]
}
];
]
24 changes: 12 additions & 12 deletions dev/link.mjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { execSync } from 'child_process';
import { join } from 'path';
import { existsSync } from 'fs';
import { execSync } from 'child_process'
import { join } from 'path'
import { existsSync } from 'fs'

// 获取旧的 PATH 环境变量
const oldPath = process.env.PATH;
const oldPath = process.env.PATH

// 获取脚本的路径
const repoPath = join(__dirname, '..')

// 切换到用户的主目录
process.chdir(process.env.USERPROFILE);
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}`);
}
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')}"`);
}
execSync(`cmd /c mklink /j "${join(repoPath, 'node_modules')}" "${join(process.env.USERPROFILE, 'node_modules')}"`)


// 切换回旧的路径
process.chdir(oldPath);
process.chdir(oldPath)
Loading

0 comments on commit d81e8bb

Please sign in to comment.