模块化,添加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

+53
View File
@@ -0,0 +1,53 @@
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));
} catch (e) {
return false;
}
return true;
}
const build = async () => {
const modules = fs.readdirSync(root('module'));
const config = {
mode: 'production',
entry: {},
output: {
filename: 'module/[name].js',
path: root('.build')
},
target: 'node',
module: {},
plugins: [
new webpack.ProgressPlugin(),
new FriendlyErrorsPlugin(),
]
};
for (let i of modules) {
if (!i.startsWith('.')) {
if (exist(`module/${i}/model.js`)) {
config.entry[`${i}/model`] = root(`module/${i}/model.js`);
}
if (exist(`module/${i}/handler.js`)) {
config.entry[`${i}/handler`] = root(`module/${i}/handler.js`);
}
}
}
const compiler = webpack(config);
await new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) console.error(err.details);
reject();
}
if (stats.hasErrors()) process.exitCode = 1;
resolve();
});
})
}
module.exports = build;
+3 -5
View File
@@ -4,8 +4,6 @@ try {
fs.rmdirSync(root('.build'));
} catch (e) { }
fs.mkdirSync(root('.build'));
let progress = (name) => ((prog) => {
console.log(name, prog);
});
require('./locales')('locales', progress);
require('./webpack')('webpack', progress);
require('./locales')();
require('./buildModule')();
require('./webpack')();
+1 -4
View File
@@ -1,15 +1,12 @@
const yaml = require('js-yaml');
const fs = require('fs');
const root = require('./root');
const build = async (next) => {
const build = async () => {
let langs = fs.readdirSync(root('locales'));
next({ total: langs.length });
let lang = {};
let count = 0;
for (let i of langs) {
const content = fs.readFileSync(root(`locales/${i}`)).toString();
lang[i.split('.')[0]] = yaml.safeLoad(content);
next({ progress: ++count });
}
fs.writeFileSync(root('.build/locales.json'), JSON.stringify(lang));
}
+34 -22
View File
@@ -1,11 +1,24 @@
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const root = require('./root');
const build = async (next) => {
const modules = fs.readdirSync(root('hydro', 'module'));
const build = async () => {
function hackNodeModuleFormidable() {
const tasks = ['incoming_form', 'file', 'json_parser', 'querystring_parser'];
for (let 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');
file[0] = '';
file = file.join('\n');
fs.writeFileSync(root(`node_modules/formidable/lib/${task}.js`), file);
}
}
}
hackNodeModuleFormidable();
const config = {
mode: 'production',
mode: 'development',
entry: {
development: root('hydro/development.js'),
install: root('hydro/install.js'),
@@ -16,25 +29,24 @@ const build = async (next) => {
path: root('.build')
},
target: 'node',
module: {}
module: {},
plugins: [
new webpack.ProgressPlugin(),
new FriendlyErrorsPlugin(),
]
};
for (let i of modules) {
if (fs.statSync(path.resolve(__dirname, 'hydro', 'module', i)).isDirectory()) {
config.entry[root(`.build/module/${i}`)] = root(`./hydro/module/${i}/index.js`);
} else {
config.entry[root(`.build/module/${i}`)] = root(`./hydro/module/${i}`);
}
}
const compiler = webpack(config);
function compilerCallback(err, stats) {
if (err) {
console.error(err.stack || err);
if (err.details) console.error(err.details);
process.exit(1);
}
}
if (!watch && stats.hasErrors()) process.exitCode = 1;
compiler.run(compilerCallback);
next({ total: 100 });
await new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) console.error(err.details);
reject(1);
}
if (stats.hasErrors()) process.exitCode = 1;
resolve();
});
})
}
module.exports = build;