Skip to content

Commit

Permalink
added altair-electron-settings-static to reduce electron package size
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Feb 10, 2024
1 parent 678ce33 commit 66cc210
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
21 changes: 21 additions & 0 deletions packages/altair-electron-settings-static/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@altairgraphql/electron-settings-static",
"version": "6.3.0",
"private": true,
"main": "./build/index.js",
"devDependencies": {
"@altairgraphql/electron-settings": "^6.3.0",
"dts-bundle-generator": "^6.11.0",
"esbuild": "^0.14.43",
"typescript": "5.2.2"
},
"license": "MIT",
"scripts": {
"compile": "node scripts/build.js",
"prepare": "yarn prepare-dist && yarn compile && yarn types",
"prepare-dist": "node scripts/prepare_dist.js",
"test": "jest",
"types": "dts-bundle-generator src/index.ts -o build/index.d.ts --external-inlines altair-graphql-core --external-inlines altair-static"
},
"types": "./build/index.d.ts"
}
15 changes: 15 additions & 0 deletions packages/altair-electron-settings-static/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const esbuild = require('esbuild');

async function main() {
await esbuild.build({
entryPoints: ['src/index.ts'],
outfile: 'build/index.js',
platform: 'node',
target: 'node13.2',
format: 'cjs',
bundle: true,
minify: false,
});
}

main();
42 changes: 42 additions & 0 deletions packages/altair-electron-settings-static/scripts/prepare_dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const ncp = require('ncp').ncp;
const path = require('path');
const fs = require('fs');

ncp.limit = 16;

const deleteFolderRecursive = function(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file, index) {
const curPath = path + '/' + file;
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
deleteFolderRecursive(curPath);
} else {
// delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
const altairAppDistFile = require.resolve('@altairgraphql/electron-settings'); // should resolve to <altair-dir>/dist/main.js
const distSrc = path.join(altairAppDistFile, '..'); // From the altair-electron-settings dist folder
const distDestination = path.join(__dirname, '../build/dist'); // To altair-static dist folder
deleteFolderRecursive(distDestination);
fs.mkdirSync(distDestination, { recursive: true });

// const cdnFile = fileName => `https://cdn.jsdelivr.net/npm/altair-static/dist/${fileName}`;

/**
* Copy dist files into dist directory.
* Set base to ./
* Set the scripts and styles in template.html
* Add template.html to dist directory.
*/
ncp(distSrc, distDestination, function(err) {
if (err) {
console.error(err);
throw err;
}
console.log('Done copying dist folder!');
});
6 changes: 6 additions & 0 deletions packages/altair-electron-settings-static/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { resolve } from 'path';

/**
* Returns the path to Altair assets, for resolving the assets when rendering Altair
*/
export const getDistDirectory = () => resolve(__dirname, './dist');
15 changes: 15 additions & 0 deletions packages/altair-electron-settings-static/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "build/",
"rootDir": "src",
"declaration": true,
"declarationMap": true,
"target": "es2020",
"module": "commonjs",
"sourceMap": true,
// "skipLibCheck": true,
"lib": ["dom", "esnext"],
"types": ["node"]
}
}
2 changes: 1 addition & 1 deletion packages/altair-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@altairgraphql/electron-interop": "^6.3.0",
"@altairgraphql/electron-settings": "^6.3.0",
"@altairgraphql/electron-settings-static": "^6.3.0",
"@altairgraphql/login-redirect": "^6.3.0",
"@sentry/electron": "^4.14.0",
"altair-static": "^6.3.0",
Expand Down
9 changes: 5 additions & 4 deletions packages/altair-electron/src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { checkForUpdates } from '../updates';
import { BrowserWindow, MenuItem } from 'electron';
import url from 'url';
import path from 'path';
import { getStaticDirectory } from '../utils';
import { getDistDirectory } from '@altairgraphql/electron-settings-static';
import { WindowManager } from './window';

export class ActionManager {
Expand Down Expand Up @@ -80,9 +80,10 @@ export class ActionManager {
try {
return prefWindow.loadURL(
url.format({
pathname: path.resolve(
require.resolve('@altairgraphql/electron-settings')
), // path.resolve(getStaticDirectory(), 'settings/index.html'),
// pathname: path.resolve(
// require.resolve('@altairgraphql/electron-settings')
// ),
pathname: path.resolve(getDistDirectory(), 'index.html'),
protocol: 'file:',
slashes: true,
})
Expand Down

0 comments on commit 66cc210

Please sign in to comment.