Skip to content

Commit

Permalink
fix(worker): prod and dist compat (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsagetlethias committed Dec 21, 2021
1 parent de8063b commit b4f1e59
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 16 deletions.
29 changes: 23 additions & 6 deletions package.json
Expand Up @@ -19,7 +19,7 @@
"dist:mac-local": "CSC_IDENTITY_AUTO_DISCOVERY=false electron-builder --mac dmg",
"dist:linux": "electron-builder --linux AppImage",
"start": "electron dist/main/main.js",
"start:mac": "open -n dist/mac-x64/mac/archimail.app",
"start:mac": "open -n electron/dist/mac-x64/mac/archimail.app",
"lint": "eslint src/",
"lint:test": "eslint -c tests/.eslintrc.json 'tests/**/*.ts*'",
"lint:fix": "yarn lint --fix && yarn lint:test --fix",
Expand All @@ -45,17 +45,32 @@
"@nivo/core"
]
},
"//build.extraResources": "Static should be set again because it is overwritten",
"build": {
"appId": "fr.gouv.social.fabrique.archimail",
"artifactName": "archimail-${version}.${ext}",
"productName": "Archimail",
"files": [
"electron/dist",
"electron/build/icon.*",
"package.json"
"electron/build/icon.*"
],
"extraResources": [
{
"from": "dist/main/",
"to": "workers/",
"filter": [
"**/*.worker.js*"
]
},
{
"from": "static",
"to": "static",
"filter": [
"**/*"
]
}
],
"directories": {
"output": "dist/${os}-${arch}"
"output": "electron/dist/${os}-${arch}"
},
"mac": {
"category": "public.app-category.productivity",
Expand Down Expand Up @@ -84,6 +99,7 @@
"@testing-library/react": "^12.1.2",
"@tsconfig/node14": "^1.0.1",
"@types/d3": "^7.1.0",
"@types/glob": "^7.2.0",
"@types/jest": "^27.0.3",
"@types/json2csv": "^5.0.3",
"@types/node": "14",
Expand All @@ -102,6 +118,7 @@
"eslint": "^7",
"eslint-plugin-playwright": "^0.6.0",
"eslint-plugin-unused-imports": "^1",
"glob": "^7.2.0",
"husky": "^7.0.4",
"identity-obj-proxy": "^3.0.0",
"jest": "^27.4.4",
Expand Down Expand Up @@ -135,4 +152,4 @@
"uuid": "^8.3.2",
"xlsx": "^0.17.4"
}
}
}
7 changes: 3 additions & 4 deletions src/main/modules/PstExtractorModule.ts
Expand Up @@ -15,17 +15,16 @@ import type {
} from "@common/modules/pst-extractor/type";
import type { UserConfigService } from "@common/modules/UserConfigModule";
import { ipcMain } from "electron";
import path from "path";

import { TSWorker } from "../worker";
import type {
PstWorkerData,
PstWorkerMessageType,
} from "./pst-extractor/worker";
} from "./pst-extractor/pst-extractor.worker";
import {
PST_DONE_WORKER_EVENT,
PST_PROGRESS_WORKER_EVENT,
} from "./pst-extractor/worker";
} from "./pst-extractor/pst-extractor.worker";

const REGEXP_PST = /\.pst$/i;

Expand Down Expand Up @@ -115,7 +114,7 @@ export class PstExtractorModule implements Module {

console.info("Start extracting...");
this.pstWorker = new TSWorker(
path.resolve(__dirname, "pst-extractor", "worker.ts"),
"modules/pst-extractor/pst-extractor.worker.ts",
{
stderr: true,
trackUnmanagedFds: true,
Expand Down
File renamed without changes.
30 changes: 26 additions & 4 deletions src/main/worker.ts
@@ -1,24 +1,27 @@
import { IS_DIST_MODE, IS_PACKAGED } from "@common/config";
import path from "path";
import type { URL } from "url";
import type { WorkerOptions } from "worker_threads";
import { Worker as BaseWorker } from "worker_threads";

const WORKER_BRIDGE_PATH = path.resolve(__dirname, "_worker.js");
const PACKAGED_WORKERS_FOLDER_RESOURCE_PATH = "workers";

/**
* Worker thread wrapper for typescript workers.
*/
export class TSWorker<TMessageValue = unknown> extends BaseWorker {
constructor(
absoluteWorkerPath: URL | string,
mainRelativeWorkerPath: string,
options?: Omit<WorkerOptions, "eval">
) {
super(WORKER_BRIDGE_PATH, {
const _workerPath = getWorkerPath(mainRelativeWorkerPath);
console.log(_workerPath);
super(_workerPath.endsWith(".js") ? _workerPath : WORKER_BRIDGE_PATH, {
...options,
eval: false,
workerData: {
...options?.workerData,
_workerPath: absoluteWorkerPath,
_workerPath,
},
});
}
Expand All @@ -30,3 +33,22 @@ export class TSWorker<TMessageValue = unknown> extends BaseWorker {
super.postMessage(value);
}
}

const getWorkerPath = (mainRelativeWorkerPath: string) => {
// TODO: path validator
if (IS_PACKAGED()) {
const absoluteProdWorkerPath = path.resolve(
process.resourcesPath,
PACKAGED_WORKERS_FOLDER_RESOURCE_PATH,
mainRelativeWorkerPath.replace(/\.ts$/gi, ".js")
);

return absoluteProdWorkerPath;
}
const distDevWorkerPath = path.resolve(__dirname, mainRelativeWorkerPath);

if (IS_DIST_MODE) {
return distDevWorkerPath.replace(/\.ts$/gi, ".js");
}
return distDevWorkerPath;
};
25 changes: 25 additions & 0 deletions webpack.main.config.js
@@ -1,8 +1,33 @@
const path = require("path");
const glob = require("glob");

module.exports =
/** @param {import("webpack").Configuration} config */ function (config) {
if (config.resolve) {
config.resolve.alias["@common"] = config.resolve.alias["common"];
}

const workers = glob
.sync("./src/main/**/*.worker.ts")
.map((filePath) => {
const name = path.basename(filePath, path.extname(filePath));
return [
name,
path.dirname(filePath).split("src/main/")[1],
filePath,
];
})
.reduce((acc, [name, directoryPath, filePath]) => {
acc[path.join(directoryPath, name)] = filePath;
return acc;
}, {});

if (config.entry) {
config.entry = {
...config.entry,
...workers,
};
}

return config;
};
4 changes: 2 additions & 2 deletions yarn.lock
Expand Up @@ -1837,7 +1837,7 @@
resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-7946.0.8.tgz#30744afdb385e2945e22f3b033f897f76b1f12ca"
integrity sha512-1rkryxURpr6aWP7R786/UQOkJ3PcpQiWkAXBmdWc7ryFWqN6a4xfK7BtjXvFBKO9LjQ+MWQSWxYeZX1OApnArA==

"@types/glob@*", "@types/glob@^7.1.1":
"@types/glob@*", "@types/glob@^7.1.1", "@types/glob@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.2.0.tgz#bc1b5bf3aa92f25bd5dd39f35c57361bdce5b2eb"
integrity sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==
Expand Down Expand Up @@ -6191,7 +6191,7 @@ glob-parent@^5.1.2, glob-parent@~5.1.2:
dependencies:
is-glob "^4.0.1"

glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6, glob@^7.1.7:
glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6, glob@^7.1.7, glob@^7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.0.tgz#d15535af7732e02e948f4c41628bd910293f6023"
integrity sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==
Expand Down

0 comments on commit b4f1e59

Please sign in to comment.