This commit is contained in:
AncTe committed 2024-02-05 20:51:57 +08:00
1 parent 6ce86501f1
commit 84620ce779
42 files changed
+233 -3803

No files matched your search

+126 -124
View File
@@ -13,130 +13,132 @@ import compressing from 'compressing';
const type = process.argv[2];
const map = false;
const resorce = type !== 'dev';
const resorce = false;
const compress = type === 'dist';
(async function () {
const timestamp = Date.now();
// 1. 去除未使用的文件
const data = (() => {
const data = fss.readFileSync('./public/project/data.js', 'utf-8');
const json = JSON.parse(
data
.split(/(\n|\r\n)/)
.slice(1)
.join('\n')
);
return json;
})() as { main: Record<string, string[]> };
const main = data.main;
// const data = (() => {
// const data = fss.readFileSync('./public/project/data.js', 'utf-8');
// const json = JSON.parse(
// data
// .split(/(\n|\r\n)/)
// .slice(1)
// .join('\n')
// );
// return json;
// })() as { main: Record<string, string[]> };
// const main = data.main;
// try {
// const data = [
// ['./dist/project/floors', '.js', 'floorIds'],
// ['./dist/project/bgms', '', 'bgms'],
// ['./dist/project/sounds', '', 'sounds'],
// ['./dist/project/images', '', 'images'],
// ['./dist/project/animates', '.animate', 'animates'],
// ['./dist/project/tilesets', '', 'tilesets'],
// ['./dist/project/fonts', '.ttf', 'fonts']
// ];
// await Promise.all(
// data.map(async v => {
// const all = await fs.readdir(`${v[0]}`);
// const data = main[v[2]].map(vv => vv + v[1]);
// all.forEach(async vv => {
// if (!data.includes(vv)) {
// await fs.rm(`${v[0]}/${vv}`);
// }
// });
// })
// );
// if (!map) await fs.remove('./dist/maps/');
// // 在线查看什么都看不到,这编辑器难道还需要留着吗?
// await fs.remove('./dist/_server');
// await fs.remove('./dist/editor.html');
// await fs.remove('./dist/server.cjs');
// await fs.remove('./dist/project/materials/airwall.png');
// await fs.remove('./dist/project/materials/ground.png');
// await fs.remove('./dist/project/materials/icons_old.png');
// } catch (e) {
// console.log('去除未使用的文件失败!');
// console.log(e);
// }
// // 2. 压缩字体
// try {
// // 获取要压缩的文字列表,libs & projects下的所有js文件
// let texts = ``;
// const exclude = `\n \t`;
// const libs = await fs.readdir('./public/libs');
// const project = await fs.readdir('./public/project');
// const floors = await fs.readdir('./public/project/floors');
// const assets = await fs.readdir('./dist/assets/');
// const all = [
// ...libs.map(v => `./public/libs/${v}`),
// ...project.map(v => `./public/project/${v}`),
// ...floors.map(v => `./public/project/floors/${v}`),
// ...assets.map(v => `./dist/assets/${v}`)
// ];
// for await (const dir of all) {
// const stat = await fs.stat(dir);
// if (!stat.isFile()) continue;
// if (dir.endsWith('.ttf')) continue;
// const file = await fs.readFile(dir, 'utf-8');
// for (let i = 0; i < file.length; i++) {
// const char = file[i];
// if (!texts.includes(char) && !exclude.includes(char))
// texts += char;
// }
// }
// // 获取所有字体(直接压缩字体会报错
// const fonts = main.fonts;
// await Promise.all([
// ...fonts.map(v =>
// (async () => {
// const fontmin = new Fontmin();
// fontmin
// .src<string>(`./public/project/fonts/${v}.ttf`)
// .dest('./dist/project/fonts')
// .use(
// Fontmin.glyph({
// text: texts
// })
// );
// await new Promise(res => {
// fontmin.run(err => {
// if (err) throw err;
// res('');
// });
// });
// })()
// )
// ]);
// await Promise.all([
// ...fonts.map(v => {
// return fs.rename(
// `./dist/project/fonts/${v}.ttf`,
// `./dist/project/fonts/${v}-${timestamp}.ttf`
// );
// })
// ]);
// } catch (e) {
// console.log('字体压缩失败');
// console.log(e);
// }
// 3. 压缩游戏进程
try {
const data = [
['./dist/project/floors', '.js', 'floorIds'],
['./dist/project/bgms', '', 'bgms'],
['./dist/project/sounds', '', 'sounds'],
['./dist/project/images', '', 'images'],
['./dist/project/animates', '.animate', 'animates'],
['./dist/project/tilesets', '', 'tilesets'],
['./dist/project/fonts', '.ttf', 'fonts']
];
await Promise.all(
data.map(async v => {
const all = await fs.readdir(`${v[0]}`);
const data = main[v[2]].map(vv => vv + v[1]);
all.forEach(async vv => {
if (!data.includes(vv)) {
await fs.rm(`${v[0]}/${vv}`);
}
});
})
);
if (!map) await fs.remove('./dist/maps/');
// 在线查看什么都看不到,这编辑器难道还需要留着吗?
await fs.remove('./dist/_server');
await fs.remove('./dist/editor.html');
await fs.remove('./dist/server.cjs');
await fs.remove('./dist/project/materials/airwall.png');
await fs.remove('./dist/project/materials/ground.png');
await fs.remove('./dist/project/materials/icons_old.png');
} catch (e) {
console.log('去除未使用的文件失败!');
console.log(e);
}
// 2. 压缩字体
try {
// 获取要压缩的文字列表,libs & projects下的所有js文件
let texts = ``;
const exclude = `\n \t`;
const libs = await fs.readdir('./public/libs');
const project = await fs.readdir('./public/project');
const floors = await fs.readdir('./public/project/floors');
const assets = await fs.readdir('./dist/assets/');
const all = [
...libs.map(v => `./public/libs/${v}`),
...project.map(v => `./public/project/${v}`),
...floors.map(v => `./public/project/floors/${v}`),
...assets.map(v => `./dist/assets/${v}`)
];
for await (const dir of all) {
const stat = await fs.stat(dir);
if (!stat.isFile()) continue;
if (dir.endsWith('.ttf')) continue;
const file = await fs.readFile(dir, 'utf-8');
for (let i = 0; i < file.length; i++) {
const char = file[i];
if (!texts.includes(char) && !exclude.includes(char))
texts += char;
}
}
// 获取所有字体(直接压缩字体会报错
const fonts = main.fonts;
await Promise.all([
...fonts.map(v =>
(async () => {
const fontmin = new Fontmin();
fontmin
.src<string>(`./public/project/fonts/${v}.ttf`)
.dest('./dist/project/fonts')
.use(
Fontmin.glyph({
text: texts
})
);
await new Promise(res => {
fontmin.run(err => {
if (err) throw err;
res('');
});
});
})()
)
]);
await Promise.all([
...fonts.map(v => {
return fs.rename(
`./dist/project/fonts/${v}.ttf`,
`./dist/project/fonts/${v}-${timestamp}.ttf`
);
})
]);
} catch (e) {
console.log('字体压缩失败');
console.log(e);
}
// 3. 压缩js插件
try {
await fs.remove('./dist/project/plugin.min.js');
await fs.remove('./dist/project/processG.min.js');
const build = await rollup.rollup({
input: 'src/plugin/game/index.js',
input: 'src/game/index.ts',
plugins: [
typescript({
sourceMap: false
sourceMap: false,
declaration: true,
declarationDir: './dist/types/'
}),
rollupBabel({
// todo: 是否需要添加 polyfill?
@@ -151,7 +153,7 @@ const compress = type === 'dist';
await build.write({
format: 'iife',
name: 'CorePlugin',
file: './dist/project/plugin.min.js'
file: './dist/project/processG.min.js'
});
await fs.remove('./dist/project/plugin/');
@@ -181,17 +183,17 @@ const compress = type === 'dist';
}
// 5. 杂项
try {
await fs.copy('./LICENSE', './dist/LICENSE');
} catch (e) {
console.log('添加杂项失败');
console.log(e);
}
// try {
// await fs.copy('./LICENSE', './dist/LICENSE');
// } catch (e) {
// console.log('添加杂项失败');
// console.log(e);
// }
// 6. 资源分离
if (resorce) {
await splitResorce(type);
}
// if (resorce) {
// await splitResorce(type);
// }
// 7. 压缩
if (compress) {
+25
View File
@@ -0,0 +1,25 @@
import typescript from '@rollup/plugin-typescript';
import fs from 'fs-extra';
import * as rollup from 'rollup';
import resolve from '@rollup/plugin-node-resolve';
export async function buildDeclaration() {
const build = await rollup.rollup({
input: './src/core/index.ts',
plugins: [
typescript({
sourceMap: false,
declaration: true,
emitDeclarationOnly: true,
outDir: './dist/types/',
noEmit: false,
jsx: 'preserve'
}),
resolve()
]
});
build.write({
file: './dist/types/index.d.ts'
});
}
buildDeclaration();
+1 -1
View File
@@ -594,7 +594,7 @@ async function ensureConfig() {
// 1. 启动vite服务
const vite = await createServer();
await vite.listen(5173);
console.log(`游戏地址:http://localhost:5173/games/${config.name}/`);
console.log(`游戏地址:http://localhost:5173`);
// 2. 启动样板http服务
await ensureConfig();