模块化,添加webpack支持

This commit is contained in:
undefined committed 2020-04-29 19:08:03 +08:00
1 parent 4ec2a46877
commit 75ebc1a746
30 files changed
+622 -459

No files matched your search

+1 -1
View File
@@ -20,4 +20,4 @@ async function post(url, data, options) {
return res;
}
module.exports = { get, post };
global.Hydro['lib.axios'] = module.exports = { get, post };
+2 -1
View File
@@ -19,4 +19,5 @@ async function download(url, path, retry = 3) {
}
return r;
}
module.exports = download;
global.Hydro['lib.download'] = module.exports = download;
+43
View File
@@ -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
View File
@@ -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;