0.0.1
This commit is contained in:
2
resources/[cfx-default]/[system]/[builders]/webpack/.gitignore
vendored
Normal file
2
resources/[cfx-default]/[system]/[builders]/webpack/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.yarn.installed
|
||||
node_modules/
|
||||
@@ -0,0 +1,13 @@
|
||||
-- This resource is part of the default Cfx.re asset pack (cfx-server-data)
|
||||
-- Altering or recreating for local use only is strongly discouraged.
|
||||
|
||||
version '1.0.0'
|
||||
author 'Cfx.re <root@cfx.re>'
|
||||
description 'Builds resources with webpack. To learn more: https://webpack.js.org'
|
||||
repository 'https://github.com/citizenfx/cfx-server-data'
|
||||
|
||||
dependency 'yarn'
|
||||
server_script 'webpack_builder.js'
|
||||
|
||||
fx_version 'adamant'
|
||||
game 'common'
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "webpack-builder",
|
||||
"version": "1.0.1",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"async": "^3.1.0",
|
||||
"webpack": "^4.41.2",
|
||||
"worker-farm": "^1.7.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const workerFarm = require('worker-farm');
|
||||
const async = require('async');
|
||||
let buildingInProgress = false;
|
||||
let currentBuildingModule = '';
|
||||
|
||||
// some modules will not like the custom stack trace logic
|
||||
const ops = Error.prepareStackTrace;
|
||||
Error.prepareStackTrace = undefined;
|
||||
|
||||
const webpackBuildTask = {
|
||||
shouldBuild(resourceName) {
|
||||
const numMetaData = GetNumResourceMetadata(resourceName, 'webpack_config');
|
||||
|
||||
if (numMetaData > 0) {
|
||||
for (let i = 0; i < numMetaData; i++) {
|
||||
const configName = GetResourceMetadata(resourceName, 'webpack_config');
|
||||
|
||||
if (shouldBuild(configName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
function loadCache(config) {
|
||||
const cachePath = `cache/${resourceName}/${config.replace(/\//g, '_')}.json`;
|
||||
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(cachePath, {encoding: 'utf8'}));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function shouldBuild(config) {
|
||||
const cache = loadCache(config);
|
||||
|
||||
if (!cache) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const file of cache) {
|
||||
const stats = getStat(file.name);
|
||||
|
||||
if (!stats ||
|
||||
stats.mtime !== file.stats.mtime ||
|
||||
stats.size !== file.stats.size ||
|
||||
stats.inode !== file.stats.inode) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function getStat(path) {
|
||||
try {
|
||||
const stat = fs.statSync(path);
|
||||
|
||||
return stat ? {
|
||||
mtime: stat.mtimeMs,
|
||||
size: stat.size,
|
||||
inode: stat.ino,
|
||||
} : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
build(resourceName, cb) {
|
||||
let buildWebpack = async () => {
|
||||
let error = null;
|
||||
const configs = [];
|
||||
const promises = [];
|
||||
const numMetaData = GetNumResourceMetadata(resourceName, 'webpack_config');
|
||||
|
||||
for (let i = 0; i < numMetaData; i++) {
|
||||
configs.push(GetResourceMetadata(resourceName, 'webpack_config', i));
|
||||
}
|
||||
|
||||
for (const configName of configs) {
|
||||
const configPath = GetResourcePath(resourceName) + '/' + configName;
|
||||
|
||||
const cachePath = `cache/${resourceName}/${configName.replace(/\//g, '_')}.json`;
|
||||
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(cachePath));
|
||||
} catch {
|
||||
}
|
||||
|
||||
const config = require(configPath);
|
||||
|
||||
const workers = workerFarm(require.resolve('./webpack_runner'));
|
||||
|
||||
if (config) {
|
||||
const resourcePath = path.resolve(GetResourcePath(resourceName));
|
||||
|
||||
while (buildingInProgress) {
|
||||
console.log(`webpack is busy: we are waiting to compile ${resourceName} (${configName})`);
|
||||
await sleep(3000);
|
||||
}
|
||||
|
||||
console.log(`${resourceName}: started building ${configName}`);
|
||||
|
||||
buildingInProgress = true;
|
||||
currentBuildingModule = resourceName;
|
||||
|
||||
promises.push(new Promise((resolve, reject) => {
|
||||
workers({
|
||||
configPath,
|
||||
resourcePath,
|
||||
cachePath
|
||||
}, (err, outp) => {
|
||||
workerFarm.end(workers);
|
||||
|
||||
if (err) {
|
||||
console.error(err.stack || err);
|
||||
if (err.details) {
|
||||
console.error(err.details);
|
||||
}
|
||||
|
||||
buildingInProgress = false;
|
||||
currentBuildingModule = '';
|
||||
currentBuildingScript = '';
|
||||
reject("worker farm webpack errored out");
|
||||
return;
|
||||
}
|
||||
|
||||
if (outp.errors) {
|
||||
for (const error of outp.errors) {
|
||||
console.log(error);
|
||||
}
|
||||
buildingInProgress = false;
|
||||
currentBuildingModule = '';
|
||||
currentBuildingScript = '';
|
||||
reject("webpack got an error");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`${resourceName}: built ${configName}`);
|
||||
buildingInProgress = false;
|
||||
resolve();
|
||||
});
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await Promise.all(promises);
|
||||
} catch (e) {
|
||||
error = e.toString();
|
||||
}
|
||||
|
||||
buildingInProgress = false;
|
||||
currentBuildingModule = '';
|
||||
|
||||
if (error) {
|
||||
cb(false, error);
|
||||
} else cb(true);
|
||||
};
|
||||
buildWebpack().then();
|
||||
}
|
||||
};
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
RegisterResourceBuildTaskFactory('z_webpack', () => webpackBuildTask);
|
||||
@@ -0,0 +1,75 @@
|
||||
const webpack = require('webpack');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
function getStat(path) {
|
||||
try {
|
||||
const stat = fs.statSync(path);
|
||||
|
||||
return stat ? {
|
||||
mtime: stat.mtimeMs,
|
||||
size: stat.size,
|
||||
inode: stat.ino,
|
||||
} : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class SaveStatePlugin {
|
||||
constructor(inp) {
|
||||
this.cache = [];
|
||||
this.cachePath = inp.cachePath;
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
compiler.hooks.afterCompile.tap('SaveStatePlugin', (compilation) => {
|
||||
for (const file of compilation.fileDependencies) {
|
||||
this.cache.push({
|
||||
name: file,
|
||||
stats: getStat(file)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
compiler.hooks.done.tap('SaveStatePlugin', (stats) => {
|
||||
if (stats.hasErrors()) {
|
||||
return;
|
||||
}
|
||||
|
||||
fs.writeFile(this.cachePath, JSON.stringify(this.cache), () => {
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (!config.plugins) {
|
||||
config.plugins = [];
|
||||
}
|
||||
|
||||
config.plugins.push(new SaveStatePlugin(inp));
|
||||
|
||||
webpack(config, (err, stats) => {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (stats.hasErrors()) {
|
||||
callback(null, stats.toJson());
|
||||
return;
|
||||
}
|
||||
|
||||
callback(null, {});
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
-- This resource is part of the default Cfx.re asset pack (cfx-server-data)
|
||||
-- Altering or recreating for local use only is strongly discouraged.
|
||||
|
||||
version '1.0.0'
|
||||
author 'Cfx.re <root@cfx.re>'
|
||||
description 'Builds resources with yarn. To learn more: https://classic.yarnpkg.com'
|
||||
repository 'https://github.com/citizenfx/cfx-server-data'
|
||||
|
||||
fx_version 'adamant'
|
||||
game 'common'
|
||||
|
||||
server_script 'yarn_builder.js'
|
||||
@@ -0,0 +1,81 @@
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const child_process = require('child_process');
|
||||
let buildingInProgress = false;
|
||||
let currentBuildingModule = '';
|
||||
|
||||
const initCwd = process.cwd();
|
||||
const trimOutput = (data) => {
|
||||
return `[yarn]\t` + data.toString().replace(/\s+$/, '');
|
||||
}
|
||||
|
||||
const yarnBuildTask = {
|
||||
shouldBuild(resourceName) {
|
||||
try {
|
||||
const resourcePath = GetResourcePath(resourceName);
|
||||
|
||||
const packageJson = path.resolve(resourcePath, 'package.json');
|
||||
const yarnLock = path.resolve(resourcePath, '.yarn.installed');
|
||||
|
||||
const packageStat = fs.statSync(packageJson);
|
||||
|
||||
try {
|
||||
const yarnStat = fs.statSync(yarnLock);
|
||||
|
||||
if (packageStat.mtimeMs > yarnStat.mtimeMs) {
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
// no yarn.installed, but package.json - install time!
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
build(resourceName, cb) {
|
||||
(async () => {
|
||||
while (buildingInProgress && currentBuildingModule !== resourceName) {
|
||||
console.log(`yarn is currently busy: we are waiting to compile ${resourceName}`);
|
||||
await sleep(3000);
|
||||
}
|
||||
buildingInProgress = true;
|
||||
currentBuildingModule = resourceName;
|
||||
const proc = child_process.fork(
|
||||
require.resolve('./yarn_cli.js'),
|
||||
['install', '--ignore-scripts', '--cache-folder', path.join(initCwd, 'cache', 'yarn-cache'), '--mutex', 'file:' + path.join(initCwd, 'cache', 'yarn-mutex')],
|
||||
{
|
||||
cwd: path.resolve(GetResourcePath(resourceName)),
|
||||
stdio: 'pipe',
|
||||
});
|
||||
proc.stdout.on('data', (data) => console.log(trimOutput(data)));
|
||||
proc.stderr.on('data', (data) => console.error(trimOutput(data)));
|
||||
proc.on('exit', (code, signal) => {
|
||||
setImmediate(() => {
|
||||
if (code != 0 || signal) {
|
||||
buildingInProgress = false;
|
||||
currentBuildingModule = '';
|
||||
cb(false, 'yarn failed!');
|
||||
return;
|
||||
}
|
||||
|
||||
const resourcePath = GetResourcePath(resourceName);
|
||||
const yarnLock = path.resolve(resourcePath, '.yarn.installed');
|
||||
fs.writeFileSync(yarnLock, '');
|
||||
|
||||
buildingInProgress = false;
|
||||
currentBuildingModule = '';
|
||||
cb(true);
|
||||
});
|
||||
});
|
||||
})();
|
||||
}
|
||||
};
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
RegisterResourceBuildTaskFactory('yarn', () => yarnBuildTask);
|
||||
147392
resources/[cfx-default]/[system]/[builders]/yarn/yarn_cli.js
Normal file
147392
resources/[cfx-default]/[system]/[builders]/yarn/yarn_cli.js
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user