chore: initial commit (#2906)

This commit is contained in:
贤心
2025-11-04 15:09:10 +08:00
committed by GitHub
parent 9e61ad04d9
commit 9ef38d8eeb
291 changed files with 160952 additions and 47199 deletions

71
scripts/dev.mjs Normal file
View File

@@ -0,0 +1,71 @@
#!/usr/bin/env node
/**
* Layui 开发模式启动脚本
*/
import { spawn } from 'cross-spawn';
import chalk from 'chalk';
const DEST = 'tests/visual/assets/dist'; // 输出目录
const env = { ...process.env, DEST, MODE: 'dev' };
// 初始提示信息
console.log(chalk.hex('#16b777').bold('🚀 Starting Layui development mode...'));
// 启动 server
spawn('npm', ['run', 'serve'], {
stdio: 'inherit',
});
function pipeOutput(child, prefix) {
// 子进程标准输出
child.stdout?.on('data', (data) => {
const lines = data.toString().trim().split('\n');
lines.forEach((line) => {
if (line.trim()) {
console.log(`[${prefix}] ${line}`);
}
});
});
// 子进程调试类输出
child.stderr?.on('data', (data) => {
const lines = data.toString().trim().split('\n');
lines.forEach((line) => {
if (line.trim()) {
console.error(`[${prefix}] ${line}`);
}
});
});
// 子进程异常事件
child.on('error', (err) => {
console.error(`[${prefix}] 启动失败:`, err.message);
});
// 子进程关闭事件
child.on('close', (code) => {
if (code !== 0 && code !== null) {
console.error(`[${prefix}] 异常退出:`, code);
}
});
}
// 启动 rollup 和 postcss watcher
const rollup = spawn('rollup', ['-c', '-w'], {
stdio: 'pipe',
env,
});
const postcss = spawn(
'postcss',
['src/css/index.css', '-o', `${DEST}/css/layui.css`, '-w', '--verbose'],
{
stdio: 'pipe',
env,
},
);
pipeOutput(rollup, 'rollup');
pipeOutput(postcss, 'postcss');

62
scripts/release.mjs Normal file
View File

@@ -0,0 +1,62 @@
/**
* 生成发行版
*/
import { createRequire } from 'node:module';
import shell from 'shelljs';
import fs from 'node:fs';
import path from 'node:path';
import archiver from 'archiver';
const require = createRequire(import.meta.url);
const pkg = require('../package.json');
let folder = `release/assets/${pkg.name}-v${pkg.version}`;
let DEST = `${folder}/dist`;
// 若传入 -V=../path/,则输出到该路径下一个带版本号的目录中
const env = process.env;
if (env.V) {
folder = env.V;
DEST = `${folder}/${pkg.version}`;
}
shell.mkdir('-p', folder);
shell.mkdir('-p', DEST);
// 复制发行版文件
shell.cp('-R', ['dist/css', 'dist/*.js'], DEST);
shell.rm('-f', `${DEST}/css/*.map`);
shell.rm('-f', `${DEST}/index.js`);
// 剔除 sourceMappingURL
shell.sed('-i', /^[ \t]*\/\/# sourceMappingURL=.*$/gm, '', `${DEST}/*.js`);
shell.sed(
'-i',
/^[ \t]*\/\*# sourceMappingURL=.*\*\/$/gm,
'',
`${DEST}/css/*.css`,
);
if (!env.V) {
// 复制可视化示例模板
shell.cp('-R', ['tests/visual/template/release/*'], folder);
// 创建压缩包
const output = fs.createWriteStream(`${folder}.zip`);
const archive = archiver('zip', {
zlib: { level: 9 }, // 压缩级别
});
output.on('close', () => {
console.log(`已生成 ${folder}.zip`);
});
archive.on('error', (err) => {
console.error('压缩失败:', err);
process.exit(1);
});
archive.pipe(output);
archive.directory(folder, path.basename(folder)); // 将整个文件夹压入 zip
await archive.finalize();
}

View File

@@ -0,0 +1,19 @@
/**
* Rollup Custom Plugins
*/
// 压缩后添加头部注释
export function banner(options = {}) {
const bannerComment = options.comment || '';
return {
name: 'banner',
generateBundle(_, bundle) {
// chunk 生成后
for (const file of Object.values(bundle)) {
if (file.type === 'chunk') {
file.code = bannerComment + file.code;
}
}
},
};
}