Skip to content

Commit

Permalink
feature: webpack compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
blattersturm committed Feb 17, 2020
1 parent 03e8a4d commit df25fbf
Show file tree
Hide file tree
Showing 11 changed files with 4,290 additions and 136 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extensions/*
!extensions/txAdminClient/
*.ignore.*
/start_*.bat
dist/

## Github's default node gitignore
# Logs
Expand Down
4,335 changes: 4,215 additions & 120 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@
"squirrelly": "^7.9.1",
"stream-snitch": "0.0.3",
"xss": "^1.0.6"
},
"devDependencies": {
"copy-webpack-plugin": "^5.1.1",
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11"
}
}
2 changes: 1 addition & 1 deletion src/components/fxRunner/resourceInjector.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function resetCacheFolder(basePath) {
*/
function getResourcesList() {
try {
let rootDir = './extensions/';
let rootDir = GetResourcePath(GetCurrentResourceName()) + '/extensions/';
var paths = fs.readdirSync(rootDir);
let resources = [];
paths.forEach((path)=>{
Expand Down
34 changes: 25 additions & 9 deletions src/components/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ const crypto = require('crypto');
const Polyglot = require('node-polyglot');
const { dir, log, logOk, logWarn, logError} = require('../extras/console')(modulename);

const languages = {
cs: require('../../locale/cs.json'),
da: require('../../locale/da.json'),
de: require('../../locale/de.json'),
en: require('../../locale/en.json'),
es: require('../../locale/es.json'),
fi: require('../../locale/fi.json'),
fr: require('../../locale/fr.json'),
hu: require('../../locale/hu.json'),
lv: require('../../locale/lv.json'),
nl: require('../../locale/nl.json'),
pl: require('../../locale/pl.json'),
pt_BR: require('../../locale/pt_BR.json'),
ro: require('../../locale/ro.json'),
th: require('../../locale/th.json'),
tr: require('../../locale/tr.json'),
zh: require('../../locale/zh.json'),
};

/**
* Small translation module built around Polyglot.js.
Expand Down Expand Up @@ -71,14 +89,18 @@ module.exports = class Translator {
*/
getLanguagePhrases(lang){
let raw;
let jsonData;

const thrower = (msg) => {
throw new Error(`Unable to load 'locale/${lang}.json'. (${msg})`);
}

let jsonData = null;

try {
raw = fs.readFileSync(`locale/${lang}.json`, 'utf8');
jsonData = languages[lang] || JSON.parse(fs.readFileSync(
`${GetResourcePath(GetCurrentResourceName())}/locale/${lang}.json`,
'utf8'
));
} catch (error) {
thrower('cannot read file');
}
Expand Down Expand Up @@ -107,7 +129,7 @@ module.exports = class Translator {
let hash = null;
try {
//FIXME: quickfix for git changing the line endings
let toHash = JSON.stringify(JSON.parse(raw));
let toHash = JSON.stringify(jsonData);
hash = crypto.createHash('SHA1').update(toHash).digest("hex");
if(globals.config.verbose) logOk(`Hash for ${lang} is ${hash}`);
} catch (error) {
Expand All @@ -117,12 +139,6 @@ module.exports = class Translator {
thrower('Please do not modify this file. Revert the changes and use the Custom language setting.')
}

try {
jsonData = JSON.parse(raw);
} catch (error) {
thrower('JSON parse error');
}

if(jsonData.constructor !== Object){
thrower('JSON parse error - not an object');
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/webServer/ctxUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const getRenderErrorText = (view, error, data) => {
}
function getWebViewPath(view){
if(view.includes('..')) throw new Error('Path Traversal?');
return path.join(__dirname, '../../../web/', view+'.html');
return path.join(GetResourcePath(GetCurrentResourceName()), 'web/', view+'.html');
}

//Squirrelly Filters
Expand Down
4 changes: 2 additions & 2 deletions src/components/webServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module.exports = class WebServer {
}
});
//Setting up additional middlewares:
this.app.use(KoaServe('web/public', {index: false, defer: false}));
this.app.use(KoaServe(path.join(GetResourcePath(GetCurrentResourceName()), 'web/public'), {index: false, defer: false}));
this.app.use(this.sessionInstance);
this.app.use(KoaBodyParser({jsonLimit}));

Expand All @@ -119,7 +119,7 @@ module.exports = class WebServer {
//================================================================
setupWebsocket(){
//Start SocketIO
this.io = SocketIO(HttpClass.createServer());
this.io = SocketIO(HttpClass.createServer(), { serveClient: false });
this.io.use(SessionIO(this.koaSessionKey, this.koaSessionMemoryStore))
this.io.use(requestAuth('socket'));

Expand Down
6 changes: 5 additions & 1 deletion src/extras/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ function txAdminASCII() {
* Check if the packages in package.json were installed
*/
function dependencyChecker() {
if (process.env.APP_ENV === 'webpack') {
return;
}

try {
let rawFile = fs.readFileSync('package.json');
let rawFile = fs.readFileSync(GetResourcePath(GetCurrentResourceName()) + '/package.json');
let parsedFile = JSON.parse(rawFile);
let packages = Object.keys(parsedFile.dependencies)
let missing = [];
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//Environment and Requires
process.chdir(__dirname+'/..');
const fs = require('fs');
const path = require('path');
const helpers = require('./extras/helpers');
Expand Down
2 changes: 1 addition & 1 deletion src/txAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ module.exports = class txAdmin {

//==============================================================
function HandleFatalError(err, componentName){
if(err.message.includes('Cannot find module')){
if(err.message.includes('Cannot find module') && process.env.APP_ENV !== 'webpack'){
logError(`Error starting '${componentName}' module. Make sure you executed 'npm install'.`)
if(globals.config.verbose) dir(err);
}else{
Expand Down
34 changes: 34 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const path = require('path');
const webpack = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
mode: 'development',
target: 'node',
entry: './starter.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'starter.js'
},
externals: {
// engine.io
uws: 'uws'
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
APP_ENV: JSON.stringify('webpack')
}
}),

new CopyPlugin([
'LICENSE',
'*.md',
'fxmanifest.lua',
'extensions/**/*.lua',
'extensions/**/cl_*.js',
'web/**',
'locale/custom.json',
]),
],
};

0 comments on commit df25fbf

Please sign in to comment.