mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
模块化,添加webpack支持
This commit is contained in:
1 parent
4ec2a46877
commit
75ebc1a746
30 files changed
+622
-459
No files matched your search
+1
-1
@@ -20,4 +20,4 @@ async function post(url, data, options) {
|
||||
return res;
|
||||
}
|
||||
|
||||
module.exports = { get, post };
|
||||
global.Hydro['lib.axios'] = module.exports = { get, post };
|
||||
@@ -19,4 +19,5 @@ async function download(url, path, retry = 3) {
|
||||
}
|
||||
return r;
|
||||
}
|
||||
module.exports = download;
|
||||
|
||||
global.Hydro['lib.download'] = module.exports = download;
|
||||
@@ -0,0 +1,43 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
let installed;
|
||||
function root(name) {
|
||||
return path.resolve(process.cwd(), name);
|
||||
}
|
||||
function exist(name) {
|
||||
try {
|
||||
fs.statSync(root(name));
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const superRequire = (name) => (__non_webpack_require__
|
||||
? __non_webpack_require__(root(name))
|
||||
: require(root(name)));
|
||||
|
||||
async function prepare() {
|
||||
installed = fs.readdirSync(root('.build/module'));
|
||||
}
|
||||
|
||||
async function handler() {
|
||||
for (const i of installed) {
|
||||
if (exist(`.build/module/${i}/handler.js`)) {
|
||||
superRequire(`.build/module/${i}/handler.js`);
|
||||
}
|
||||
console.log(`Handler init: ${i}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function model() {
|
||||
for (const i of installed) {
|
||||
if (exist(`.build/module/${i}/model.js`)) {
|
||||
superRequire(`.build/module/${i}/model.js`);
|
||||
}
|
||||
console.log(`Model init: ${i}`);
|
||||
}
|
||||
}
|
||||
|
||||
global.Hydro['lib.loader'] = module.exports = { prepare, handler, model };
|
||||
+4
-2
@@ -1,4 +1,4 @@
|
||||
module.exports = function* ranked(diter, equ = (a, b) => a === b) {
|
||||
function* ranked(diter, equ = (a, b) => a === b) {
|
||||
let last = null;
|
||||
let r = 0;
|
||||
let count = 0;
|
||||
@@ -8,4 +8,6 @@ module.exports = function* ranked(diter, equ = (a, b) => a === b) {
|
||||
last = doc;
|
||||
yield [r, doc];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
global.Hydro['lib.rank'] = module.exports = ranked;
|
||||
Reference in new issue
Block a user