Skip to content

Commit

Permalink
pack node executable with UPX compressor (for smaller executable) & r…
Browse files Browse the repository at this point in the history
…ebuilt dist
  • Loading branch information
pk910 committed Dec 31, 2021
1 parent 36bfea2 commit 1db0d71
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 32 deletions.
Binary file modified dist/shamir39-linux
Binary file not shown.
Binary file modified dist/shamir39-win32.exe
Binary file not shown.
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"clean-webpack-plugin": "^4.0.0",
"nexe-webpack-plugin": "^0.1.1",
"terser-webpack-plugin": "^4.2.3",
"upx": "^1.0.6",
"webpack": "^4.46.0",
"webpack-cli": "^4.9.1",
"webpack-merge": "^5.8.0"
Expand Down
42 changes: 42 additions & 0 deletions utils/nexe-upx-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const UPX = require('upx');
const fs = require('fs');
const path = require('path');

var upx = UPX({

});

module.exports = (useCompressed) => {
return (compiler, next) => {
return new Promise((resolve) => {
var nodeBinFile = path.join(compiler.options.temp, compiler.options.target);

if(useCompressed && !fs.existsSync(nodeBinFile + "-org")) {
fs.renameSync(nodeBinFile, nodeBinFile + "-org");
if(fs.existsSync(nodeBinFile + "-upx")) {
fs.renameSync(nodeBinFile + "-upx", nodeBinFile);
resolve(next());
}
else {
console.log("Compressing node executable '" + compiler.options.target + "'");
upx(nodeBinFile + "-org").output(nodeBinFile).start().catch((err) => {
console.log("UPX failed: ");
console.log(err);
fs.renameSync(nodeBinFile + "-org", nodeBinFile);
}).then((stats) => {
console.log(stats);
resolve(next());
});
}
}
else if(!useCompressed && fs.existsSync(nodeBinFile + "-org")) {
fs.renameSync(nodeBinFile, nodeBinFile + "-upx");
fs.renameSync(nodeBinFile + "-org", nodeBinFile);
resolve(next());
}
else {
resolve(next());
}
});
};
}
66 changes: 36 additions & 30 deletions webpack.config.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,41 @@ const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { NexePlugin } = require('nexe-webpack-plugin');

const nexeUpxPlugin = require('./utils/nexe-upx-plugin');

var buildTemp = path.join(__dirname, "build");

module.exports = {
entry: './src/shamir39.cli.js',
target: 'node',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'shamir39.js'
},
module: {
rules: []
},
plugins: [
new CleanWebpackPlugin(),
new NexePlugin([
{
input: 'shamir39.js',
temp: buildTemp,
arch: 'win32-x64',
output: 'shamir39-win32.exe',
target: 'windows-x64-14.15.3'
},
{
input: 'shamir39.js',
temp: buildTemp,
arch: 'linux-x64',
output: 'shamir39-linux',
target: 'linux-x64-14.15.3'
},
]),
]
}
module.exports = (devBuild) => {
return {
entry: './src/shamir39.cli.js',
target: 'node',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'shamir39.js'
},
module: {
rules: []
},
plugins: [
new CleanWebpackPlugin(),
new NexePlugin([
{
input: 'shamir39.js',
temp: buildTemp,
arch: 'win32-x64',
output: 'shamir39-win32.exe',
target: 'windows-x64-14.15.3',
plugins: [ nexeUpxPlugin(!devBuild) ]
},
{
input: 'shamir39.js',
temp: buildTemp,
arch: 'linux-x64',
output: 'shamir39-linux',
target: 'linux-x64-14.15.3',
plugins: [ nexeUpxPlugin(!devBuild) ]
},
]),
]
};
};
2 changes: 1 addition & 1 deletion webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const webpack = require('webpack');
const { merge } = require('webpack-merge');
const common = require('./webpack.config.common.js');

module.exports = merge(common, {
module.exports = merge(common(true), {
mode: 'development',
devtool: 'inline-source-map'
});
2 changes: 1 addition & 1 deletion webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { merge } = require('webpack-merge');
const common = require('./webpack.config.common.js');
const TerserPlugin = require("terser-webpack-plugin");

module.exports = merge(common, {
module.exports = merge(common(false), {
mode: 'production',
optimization: {
minimize: true,
Expand Down

0 comments on commit 1db0d71

Please sign in to comment.