mirror of
https://github.com/citizenfx/cfx-server-data.git
synced 2025-02-10 07:32:56 +08:00
builders: webpack resource builder
This commit is contained in:
parent
83f6045167
commit
407b805616
2
resources/[system]/[builders]/webpack/.gitignore
vendored
Normal file
2
resources/[system]/[builders]/webpack/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
yarn.lock
|
||||
node_modules/
|
3
resources/[system]/[builders]/webpack/__resource.lua
Normal file
3
resources/[system]/[builders]/webpack/__resource.lua
Normal file
@ -0,0 +1,3 @@
|
||||
dependency 'yarn'
|
||||
server_only 'yes'
|
||||
server_script 'webpack_builder.js'
|
16
resources/[system]/[builders]/webpack/package.json
Normal file
16
resources/[system]/[builders]/webpack/package.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "webpack-builder",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"async": "^2.6.1",
|
||||
"webpack": "^4.12.0",
|
||||
"worker-farm": "^1.6.0"
|
||||
}
|
||||
}
|
86
resources/[system]/[builders]/webpack/webpack_builder.js
Normal file
86
resources/[system]/[builders]/webpack/webpack_builder.js
Normal file
@ -0,0 +1,86 @@
|
||||
const path = require('path');
|
||||
const workerFarm = require('worker-farm');
|
||||
const async = require('async');
|
||||
|
||||
const justBuilt = {};
|
||||
|
||||
const webpackBuildTask = {
|
||||
shouldBuild(resourceName) {
|
||||
const numMetaData = GetNumResourceMetadata(resourceName, 'webpack_config');
|
||||
|
||||
if (numMetaData > 0) {
|
||||
if (!(resourceName in justBuilt)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
delete justBuilt[resourceName];
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
build(resourceName, cb) {
|
||||
const configs = [];
|
||||
const numMetaData = GetNumResourceMetadata(resourceName, 'webpack_config');
|
||||
|
||||
for (let i = 0; i < numMetaData; i++) {
|
||||
configs.push(GetResourceMetadata(resourceName, 'webpack_config', i));
|
||||
}
|
||||
|
||||
async.forEachOf(configs, (configName, i, acb) => {
|
||||
const configPath = GetResourcePath(resourceName) + '/' + configName;
|
||||
|
||||
const config = require(configPath);
|
||||
|
||||
const workers = workerFarm(require.resolve('./webpack_runner'));
|
||||
|
||||
if (config) {
|
||||
const resourcePath = path.resolve(GetResourcePath(resourceName));
|
||||
|
||||
workers({
|
||||
configPath,
|
||||
resourcePath
|
||||
}, (err, outp) => {
|
||||
workerFarm.end(workers);
|
||||
|
||||
if (err) {
|
||||
console.error(err.stack || err);
|
||||
if (err.details) {
|
||||
console.error(err.details);
|
||||
}
|
||||
|
||||
acb("worker farm webpack errored out");
|
||||
return;
|
||||
}
|
||||
|
||||
if (outp.errors) {
|
||||
for (const error of outp.errors) {
|
||||
console.log(error);
|
||||
}
|
||||
acb("webpack got an error");
|
||||
return;
|
||||
}
|
||||
|
||||
acb();
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
acb("no configuration");
|
||||
}, (err) => {
|
||||
setImmediate(() => {
|
||||
if (err) {
|
||||
cb(false, err);
|
||||
return;
|
||||
}
|
||||
|
||||
justBuilt[resourceName] = true;
|
||||
|
||||
cb(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
RegisterResourceBuildTaskFactory('z_webpack', () => webpackBuildTask);
|
26
resources/[system]/[builders]/webpack/webpack_runner.js
Normal file
26
resources/[system]/[builders]/webpack/webpack_runner.js
Normal file
@ -0,0 +1,26 @@
|
||||
const weebpack = require('webpack');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = (inp, callback) => {
|
||||
const config = require(inp.configPath);
|
||||
|
||||
config.context = inp.resourcePath;
|
||||
|
||||
if (config.output && config.output.path) {
|
||||
config.output.path = path.resolve(inp.resourcePath, config.output.path);
|
||||
}
|
||||
|
||||
weebpack(config, (err, stats) => {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (stats.hasErrors()) {
|
||||
callback(null, stats.toJson());
|
||||
return;
|
||||
}
|
||||
|
||||
callback(null, {});
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue
Block a user