mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
打包模块为单个文件
This commit is contained in:
1 parent
e3bbff7e11
commit
04941936bc
6 files changed
+87
-71
No files matched your search
@@ -2,6 +2,7 @@
|
|||||||
/* eslint-disable import/no-extraneous-dependencies */
|
/* eslint-disable import/no-extraneous-dependencies */
|
||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const zlib = require('zlib');
|
||||||
const webpack = require('webpack');
|
const webpack = require('webpack');
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
|
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
|
||||||
@@ -54,6 +55,35 @@ const build = async (type) => {
|
|||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
for (const i of modules) {
|
||||||
|
if (!i.startsWith('.')) {
|
||||||
|
const current = {};
|
||||||
|
if (exist(`module/${i}/locale`)) {
|
||||||
|
const locales = fs.readdirSync(root(`module/${i}/locale`));
|
||||||
|
const lang = {};
|
||||||
|
for (const j of locales) {
|
||||||
|
const content = fs.readFileSync(root(`module/${i}/locale/${j}`)).toString();
|
||||||
|
lang[j.split('.')[0]] = yaml.safeLoad(content);
|
||||||
|
}
|
||||||
|
current.locale = lang;
|
||||||
|
}
|
||||||
|
if (exist(`module/${i}/template.build.js`)) {
|
||||||
|
const builder = require(root(`module/${i}/template.build.js`));
|
||||||
|
const [templates, exclude] = await builder.prebuild();
|
||||||
|
current.template = template(templates, exclude);
|
||||||
|
} else if (exist(`module/${i}/template`)) {
|
||||||
|
current.template = template(`module/${i}/template`);
|
||||||
|
}
|
||||||
|
if (exist(`module/${i}/model.js`)) {
|
||||||
|
current.model = fs.readFileSync(root(`.build/module/${i}/model.js`)).toString();
|
||||||
|
}
|
||||||
|
if (exist(`module/${i}/handler.js`)) {
|
||||||
|
current.handler = fs.readFileSync(root(`.build/module/${i}/handler.js`)).toString();
|
||||||
|
}
|
||||||
|
const data = zlib.gzipSync(Buffer.from(yaml.safeDump(current)), { level: 3 });
|
||||||
|
fs.writeFileSync(root(`.build/module/${i}.hydro-module`), data);
|
||||||
|
}
|
||||||
|
}
|
||||||
for (const i of modules) {
|
for (const i of modules) {
|
||||||
if (!i.startsWith('.')) {
|
if (!i.startsWith('.')) {
|
||||||
if (exist(`module/${i}/locale`)) {
|
if (exist(`module/${i}/locale`)) {
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
const fs = require('fs');
|
|
||||||
const yaml = require('js-yaml');
|
|
||||||
const root = require('./root');
|
|
||||||
const template = require('./template');
|
|
||||||
|
|
||||||
const build = async () => {
|
|
||||||
const exclude = [
|
|
||||||
'partials/training_default.json',
|
|
||||||
'partials/problem_default.md',
|
|
||||||
'bsod.html',
|
|
||||||
];
|
|
||||||
fs.writeFileSync(root('.build/template.yaml'), yaml.safeDump(template('templates', exclude)));
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = build;
|
|
||||||
+19
-2
@@ -1,13 +1,30 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const zlib = require('zlib');
|
||||||
|
const yaml = require('js-yaml');
|
||||||
const root = require('./root');
|
const root = require('./root');
|
||||||
|
const template = require('./template');
|
||||||
const ignoreFailure = require('./ignoreFailure');
|
const ignoreFailure = require('./ignoreFailure');
|
||||||
|
|
||||||
async function build(type) {
|
async function build(type) {
|
||||||
if (!['development', 'production'].includes(type)) throw new Error(`Unknown type: ${type}`);
|
if (!['development', 'production'].includes(type)) throw new Error(`Unknown type: ${type}`);
|
||||||
ignoreFailure(fs.mkdirSync, root('.build'));
|
ignoreFailure(fs.mkdirSync, root('.build'));
|
||||||
await require('./locales')(type);
|
const langs = fs.readdirSync(root('locales'));
|
||||||
|
const lang = {};
|
||||||
|
for (const i of langs) {
|
||||||
|
const content = fs.readFileSync(root(`locales/${i}`)).toString();
|
||||||
|
lang[i.split('.')[0]] = yaml.safeLoad(content);
|
||||||
|
}
|
||||||
|
const exclude = [
|
||||||
|
'partials/training_default.json',
|
||||||
|
'partials/problem_default.md',
|
||||||
|
'bsod.html',
|
||||||
|
];
|
||||||
|
const builtin = {};
|
||||||
|
builtin.locale = lang;
|
||||||
|
builtin.template = template('templates', exclude);
|
||||||
|
const data = zlib.gzipSync(Buffer.from(yaml.safeDump(builtin)), { level: 3 });
|
||||||
|
fs.writeFileSync(root('.build/module/builtin.hydro-module'), data);
|
||||||
await require('./buildModule')(type);
|
await require('./buildModule')(type);
|
||||||
await require('./buildTemplate')(type);
|
|
||||||
await require('./webpack')(type);
|
await require('./webpack')(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
const yaml = require('js-yaml');
|
|
||||||
const fs = require('fs');
|
|
||||||
const root = require('./root');
|
|
||||||
|
|
||||||
const build = async () => {
|
|
||||||
const langs = fs.readdirSync(root('locales'));
|
|
||||||
const lang = {};
|
|
||||||
for (const i of langs) {
|
|
||||||
const content = fs.readFileSync(root(`locales/${i}`)).toString();
|
|
||||||
lang[i.split('.')[0]] = yaml.safeLoad(content);
|
|
||||||
}
|
|
||||||
fs.writeFileSync(root('.build/locales.json'), JSON.stringify(lang));
|
|
||||||
};
|
|
||||||
module.exports = build;
|
|
||||||
@@ -38,6 +38,10 @@ const build = (dirOrObject, exclude = []) => {
|
|||||||
.replace(/> {{/gmi, '>{{')
|
.replace(/> {{/gmi, '>{{')
|
||||||
.replace(/{{ /gmi, '{{')
|
.replace(/{{ /gmi, '{{')
|
||||||
.replace(/ }}/gmi, '}}')
|
.replace(/ }}/gmi, '}}')
|
||||||
|
.replace(/%} </gmi, '%}<')
|
||||||
|
.replace(/> {%/gmi, '>{%')
|
||||||
|
.replace(/= /gmi, '=')
|
||||||
|
.replace(/ =/gmi, '=')
|
||||||
.trim();
|
.trim();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+34
-40
@@ -1,73 +1,66 @@
|
|||||||
/* eslint-disable import/no-dynamic-require */
|
/* eslint-disable import/no-dynamic-require */
|
||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
|
/* eslint-disable no-eval */
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const zlib = require('zlib');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
|
|
||||||
function root(name) {
|
function root(name) {
|
||||||
return path.resolve(process.cwd(), name);
|
return path.resolve(process.cwd(), name);
|
||||||
}
|
}
|
||||||
const installed = fs.readdirSync(root('.build/module'));
|
|
||||||
function exist(name) {
|
const installed = [];
|
||||||
try {
|
|
||||||
fs.statSync(root(name));
|
async function preload() {
|
||||||
} catch (e) {
|
const files = fs.readdirSync(root('.build/module'));
|
||||||
return false;
|
for (const file of files) {
|
||||||
|
if (file.endsWith('.hydro-module')) {
|
||||||
|
const f = fs.readFileSync(root(`.build/module/${file}`));
|
||||||
|
installed.push({ ...yaml.safeLoad(zlib.gunzipSync(f)), id: file.split('.')[0] });
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
const superRequire = (name) => {
|
|
||||||
let m;
|
|
||||||
try {
|
|
||||||
m = require(root(name));
|
|
||||||
} catch (e) {
|
|
||||||
m = __non_webpack_require__(root(name)); // eslint-disable-line no-undef
|
|
||||||
}
|
}
|
||||||
return m;
|
|
||||||
};
|
|
||||||
|
|
||||||
async function handler() {
|
async function handler() {
|
||||||
for (const i of installed) {
|
for (const i of installed) {
|
||||||
if (exist(`.build/module/${i}/handler.js`)) {
|
if (i.handler) {
|
||||||
superRequire(`.build/module/${i}/handler.js`);
|
console.time(`Handler init: ${i.id}`);
|
||||||
console.log(`Handler init: ${i}`);
|
const module = {}; // eslint-disable-line no-unused-vars
|
||||||
|
const exports = {}; // eslint-disable-line no-unused-vars
|
||||||
|
eval(i.handler);
|
||||||
|
console.timeEnd(`Handler init: ${i.id}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function locale() {
|
async function locale() {
|
||||||
if (exist('.build/locale.json')) {
|
|
||||||
global.Hydro.lib.i18n(superRequire('.build/locale.json'));
|
|
||||||
console.log('Locale init: builtin');
|
|
||||||
}
|
|
||||||
for (const i of installed) {
|
for (const i of installed) {
|
||||||
if (exist(`.build/module/${i}/locale.json`)) {
|
if (i.locale) {
|
||||||
global.Hydro.lib.i18n(superRequire(`.build/module/${i}/locale.json`));
|
global.Hydro.lib.i18n(i.locale);
|
||||||
console.log(`Locale init: ${i}`);
|
console.log(`Locale init: ${i.id}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function template() {
|
async function template() {
|
||||||
if (exist('.build/template.yaml')) {
|
|
||||||
const file = fs.readFileSync(root('.build/template.yaml')).toString();
|
|
||||||
Object.assign(global.Hydro.template, yaml.safeLoad(file));
|
|
||||||
console.log('Template init: builtin');
|
|
||||||
}
|
|
||||||
for (const i of installed) {
|
for (const i of installed) {
|
||||||
if (exist(`.build/module/${i}/template.yaml`)) {
|
if (i.template) {
|
||||||
const file = fs.readFileSync(root(`.build/module/${i}/template.yaml`)).toString();
|
Object.assign(global.Hydro.template, i.template);
|
||||||
Object.assign(global.Hydro.template, yaml.safeLoad(file));
|
console.log(`Template init: ${i.id}`);
|
||||||
console.log(`Template init: ${i}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function model() {
|
async function model() {
|
||||||
for (const i of installed) {
|
for (const i of installed) {
|
||||||
if (exist(`.build/module/${i}/model.js`)) {
|
if (i.model) {
|
||||||
const m = superRequire(`.build/module/${i}/model.js`);
|
console.time(`Model init: ${i.id}`);
|
||||||
if (m.index) await m.index(); // eslint-disable-line no-await-in-loop
|
const module = {};
|
||||||
console.log(`Model init: ${i}`);
|
const exports = {}; // eslint-disable-line no-unused-vars
|
||||||
|
eval(i.handler);
|
||||||
|
if ((module.exports || {}).index) await module.exports.index();
|
||||||
|
console.timeEnd(`Model init: ${i.id}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,6 +74,7 @@ async function load() {
|
|||||||
template: {},
|
template: {},
|
||||||
ui: {},
|
ui: {},
|
||||||
};
|
};
|
||||||
|
await preload();
|
||||||
await template();
|
await template();
|
||||||
require('./lib/i18n');
|
require('./lib/i18n');
|
||||||
require('./utils');
|
require('./utils');
|
||||||
@@ -114,7 +108,7 @@ async function load() {
|
|||||||
];
|
];
|
||||||
for (const i of builtinModel) {
|
for (const i of builtinModel) {
|
||||||
const m = require(`./model/${i}`);
|
const m = require(`./model/${i}`);
|
||||||
if (m.index) await m.index(); // eslint-disable-line no-await-in-loop
|
if (m.index) await m.index();
|
||||||
}
|
}
|
||||||
const builtinHandler = [
|
const builtinHandler = [
|
||||||
'home', 'problem', 'record', 'judge', 'user',
|
'home', 'problem', 'record', 'judge', 'user',
|
||||||
@@ -124,7 +118,7 @@ async function load() {
|
|||||||
await model();
|
await model();
|
||||||
await handler();
|
await handler();
|
||||||
for (const i in global.Hydro.handler) {
|
for (const i in global.Hydro.handler) {
|
||||||
await global.Hydro.handler[i].apply(); // eslint-disable-line no-await-in-loop
|
await global.Hydro.handler[i].apply();
|
||||||
}
|
}
|
||||||
const notfound = require('./handler/notfound');
|
const notfound = require('./handler/notfound');
|
||||||
await notfound.apply();
|
await notfound.apply();
|
||||||
|
|||||||
Reference in new issue
Block a user