优化结构

This commit is contained in:
undefined committed 2020-04-30 18:07:35 +08:00
1 parent 75ebc1a746
commit 2554cf660a
34 files changed
+401 -150

No files matched your search

+10 -8
View File
@@ -1,7 +1,9 @@
/* eslint-disable import/no-extraneous-dependencies */
const fs = require('fs');
const webpack = require('webpack');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const root = require('./root');
const exist = (name) => {
try {
fs.statSync(root(name));
@@ -9,24 +11,24 @@ const exist = (name) => {
return false;
}
return true;
}
const build = async () => {
};
const build = async (type) => {
const modules = fs.readdirSync(root('module'));
const config = {
mode: 'production',
mode: type,
entry: {},
output: {
filename: 'module/[name].js',
path: root('.build')
path: root('.build'),
},
target: 'node',
module: {},
plugins: [
new webpack.ProgressPlugin(),
new FriendlyErrorsPlugin(),
]
],
};
for (let i of modules) {
for (const i of modules) {
if (!i.startsWith('.')) {
if (exist(`module/${i}/model.js`)) {
config.entry[`${i}/model`] = root(`module/${i}/model.js`);
@@ -47,7 +49,7 @@ const build = async () => {
if (stats.hasErrors()) process.exitCode = 1;
resolve();
});
})
}
});
};
module.exports = build;
+45
View File
@@ -0,0 +1,45 @@
const fs = require('fs');
const yaml = require('js-yaml');
const root = require('./root');
function getFiles(folder) {
const files = [];
const f = fs.readdirSync(root(folder));
for (const i of f) {
if (!i.startsWith('.')) {
if (fs.statSync(root(`${folder}/${i}`)).isDirectory()) {
const g = getFiles(`${folder}/${i}`);
for (const j of g) files.push(`${i}/${j}`);
} else files.push(i);
}
}
return files;
}
const build = async () => {
const files = getFiles('templates');
const exclude = ['partials/training_default.json', 'partials/problem_default.md'];
const templates = {};
for (const i of files) {
let template = fs.readFileSync(root(`templates/${i}`)).toString();
if (!exclude.includes(i)) {
template = template
.trim()
.replace(/ *\n */gmi, ' ')
.replace(/, /gmi, ',')
.replace(/%} {%/gmi, '%}{%')
.replace(/ %}/gmi, '%}')
.replace(/{% /gmi, '{%')
.replace(/> </gmi, '><')
.replace(/}} </gmi, '}}<')
.replace(/> {{/gmi, '>{{')
.replace(/{{ /gmi, '{{')
.replace(/ }}/gmi, '}}')
.trim();
}
templates[i] = template;
}
fs.writeFileSync(root('.build/template.yaml'), yaml.safeDump(templates));
};
module.exports = build;
+3
View File
@@ -0,0 +1,3 @@
require('./index.js')('development').catch((e) => {
console.error(e);
});
+22 -7
View File
@@ -1,9 +1,24 @@
const fs = require('fs');
const root = require('./root');
try {
fs.rmdirSync(root('.build'));
} catch (e) { }
fs.mkdirSync(root('.build'));
require('./locales')();
require('./buildModule')();
require('./webpack')();
async function build(type) {
if (!['development', 'production'].includes(type)) throw new Error('Unknown type: ' + type);
try {
fs.rmdirSync(root('.build'));
} catch (e) { }
fs.mkdirSync(root('.build'));
await Promise.all([
require('./locales')(type),
require('./buildModule')(type),
require('./buildTemplate')(type),
require('./webpack')(type),
]);
}
module.exports = build;
if (!module.parent) {
build('development').catch((e) => {
console.error(e);
});
}
+5 -4
View File
@@ -1,13 +1,14 @@
const yaml = require('js-yaml');
const fs = require('fs');
const root = require('./root');
const build = async () => {
let langs = fs.readdirSync(root('locales'));
let lang = {};
for (let i of langs) {
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;
+3
View File
@@ -0,0 +1,3 @@
require('./index.js')('production').catch((e) => {
console.error(e);
});
+1
View File
@@ -1,4 +1,5 @@
const path = require('path');
function root(name) {
return path.resolve(__dirname, '..', name);
}
+11 -9
View File
@@ -1,11 +1,13 @@
/* eslint-disable import/no-extraneous-dependencies */
const fs = require('fs');
const webpack = require('webpack');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const root = require('./root');
const build = async () => {
const build = async (type) => {
function hackNodeModuleFormidable() {
const tasks = ['incoming_form', 'file', 'json_parser', 'querystring_parser'];
for (let task of tasks) {
for (const task of tasks) {
let file = fs.readFileSync(root(`node_modules/formidable/lib/${task}.js`)).toString();
if (file.startsWith('if (global.GENTLY) require = GENTLY.hijack(require);')) {
file = file.split('\n');
@@ -18,22 +20,22 @@ const build = async () => {
hackNodeModuleFormidable();
const config = {
mode: 'development',
mode: type,
entry: {
development: root('hydro/development.js'),
install: root('hydro/install.js'),
uninstall: root('hydro/uninstall.js')
uninstall: root('hydro/uninstall.js'),
},
output: {
filename: '[name].js',
path: root('.build')
path: root('.build'),
},
target: 'node',
module: {},
plugins: [
new webpack.ProgressPlugin(),
new FriendlyErrorsPlugin(),
]
],
};
const compiler = webpack(config);
await new Promise((resolve, reject) => {
@@ -41,12 +43,12 @@ const build = async () => {
if (err) {
console.error(err.stack || err);
if (err.details) console.error(err.details);
reject(1);
reject();
}
if (stats.hasErrors()) process.exitCode = 1;
resolve();
});
})
}
});
};
module.exports = build;