Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix vite build configurations #525

Open
wants to merge 27 commits into
base: feat-v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bdbf3a2
build: Remove the zip maker
RobertGemmaJr Jul 31, 2024
eac2e46
chore: comment
RobertGemmaJr Jul 31, 2024
9ccb89d
chore: comments and remove logging
RobertGemmaJr Jul 31, 2024
e73ce4f
feat: Add base vite config for browser usage
RobertGemmaJr Jul 31, 2024
d6aec7a
chore: comments
RobertGemmaJr Jul 31, 2024
5bbcc7f
fix: Delete fuse that was causing an error
RobertGemmaJr Jul 31, 2024
385d028
ref: Revert to CJS repository
RobertGemmaJr Jul 31, 2024
5c4bf03
g commit -a -m "chore: more fixups"
RobertGemmaJr Jul 31, 2024
d00b857
chore: add a forge env.d.ts file
RobertGemmaJr Jul 31, 2024
c547470
chore: comments
RobertGemmaJr Jul 31, 2024
983a07d
chore: comments
RobertGemmaJr Jul 31, 2024
7715cc4
chore: TODOs
RobertGemmaJr Aug 1, 2024
6f68551
chore: Fix version file as module
RobertGemmaJr Aug 2, 2024
d9e6d0a
chore: remove uneeded ts file
RobertGemmaJr Aug 2, 2024
e4a96dd
ref: Handle a default vite config shared between electron-forge and t…
RobertGemmaJr Aug 2, 2024
74c911b
chore: import order
RobertGemmaJr Aug 2, 2024
207b0b3
ref: Cleanup configs
RobertGemmaJr Aug 2, 2024
507abca
ref: Have a custom name for the vite config files
RobertGemmaJr Aug 2, 2024
29d6e46
Update src/App/App.jsx
RobertGemmaJr Aug 2, 2024
28cdd8b
Merge branch 'feat-v4' into fix-make-cjs
RobertGemmaJr Aug 5, 2024
7b316c7
fix: Remove osX code signing
RobertGemmaJr Aug 5, 2024
3471912
chore: comments
RobertGemmaJr Aug 5, 2024
b1a453d
ci: Test ci with pure intel
RobertGemmaJr Aug 5, 2024
c75d3b9
ci: Test x64
RobertGemmaJr Aug 7, 2024
aae48ff
chore: comment out osx
RobertGemmaJr Aug 7, 2024
1a95de0
ci: Test intel build on mac runner
RobertGemmaJr Aug 7, 2024
92e840b
ci: Test universal build on arm
RobertGemmaJr Aug 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
2 changes: 1 addition & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file defines specific rules for Prettier. It adjusts their default settings.
* We recommend these settings if your lab does not have specific style standards.
*/
export default {
module.exports = {
printWidth: 100, // Sets the maximum line size to 100 (default is 80)
quoteProps: "as-needed", // Add quotes around props as needed
trailingComma: "es5", // Add a trailing comma to all es5 modules
Expand Down
20 changes: 6 additions & 14 deletions forge.config.js → forge.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export default {
packagerConfig: {
asar: true,
icon: "assets/icons/icon",
osxSign: {}, // TODO: From @eldu, confirm it's needed?
},
rebuildConfig: {},
makers: [
{
// Windows Distribution
Expand All @@ -32,10 +34,6 @@ export default {
},
},
},
{
// zip files
name: "@electron-forge/maker-zip",
},
],
plugins: [
{ name: "@electron-forge/plugin-auto-unpack-natives", config: {} },
Expand All @@ -44,16 +42,11 @@ export default {
config: {
build: [
// Build files that use the main config
{ entry: "src/Electron/main.js", config: "vite.main.config.js" },
{ entry: "src/Electron/main.js", config: "vite.main.config.mjs" },
// Build files that use the preload config
{ entry: "src/Electron/preload.js", config: "vite.preload.config.js" },
{ entry: "src/Electron/preload.js", config: "vite.preload.config.mjs" },
],
renderer: [{ name: "main_window", config: "vite.renderer.config.js" }],
define: {
// Add the package name and version to import.meta.env
"import.meta.env.PACKAGE_NAME": JSON.stringify(process.env.npm_package_name),
"import.meta.env.PACKAGE_VERSION": JSON.stringify(process.env.npm_package_version),
},
renderer: [{ name: "main_window", config: "vite.renderer.config.mjs" }],
},
},
{
Expand All @@ -62,12 +55,11 @@ export default {
version: FuseVersion.V1,
[FuseV1Options.RunAsNode]: false, // Disables ELECTRON_RUN_AS_NODE
[FuseV1Options.GrantFileProtocolExtraPrivileges]: true, // Grants the file protocol extra privileges (for the built application)
[FuseV1Options.EnableCookieEncryption]: false, // Disables cookie encryption
[FuseV1Options.EnableCookieEncryption]: true, // Enables cookie encryption
[FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false, // Disables the NODE_OPTIONS environment variable
[FuseV1Options.EnableNodeCliInspectArguments]: false, // Disables the --inspect and --inspect-brk family of CLI options
[FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true, // Enforces validation of the app.asar archive on macOS
[FuseV1Options.OnlyLoadAppFromAsar]: true, // Enforces that Electron will only load your app from "app.asar" instead of its normal search paths
[FuseV1Options.LoadBrowserProcessSpecificV8Snapshot]: true, // Loads V8 Snapshot from `browser_v8_context_snapshot.bin` for the browser process
},
},
],
Expand Down
673 changes: 390 additions & 283 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"repository": "https://github.com/brown-ccv/honeycomb",
"license": "MIT",
"private": true,
"type": "module",
"main": ".vite/build/main.cjs",
"main": ".vite/build/main.js",
"homepage": ".",
"dependencies": {
"@electron-forge/plugin-fuses": "^7.4.0",
Expand Down Expand Up @@ -50,7 +49,6 @@
"@electron-forge/maker-deb": "^7.4.0",
"@electron-forge/maker-dmg": "^7.4.0",
"@electron-forge/maker-squirrel": "^7.4.0",
"@electron-forge/maker-zip": "^7.4.0",
"@electron-forge/plugin-auto-unpack-natives": "^7.4.0",
"@electron-forge/plugin-vite": "^7.4.0",
"@inquirer/prompts": "^5.3.6",
Expand All @@ -70,7 +68,7 @@
"vite": "^5.2.11"
},
"scripts": {
"start:browser": "vite --port 3000",
"start:browser": "vite -c vite.browser.config.js",
"start:electron": "electron-forge start",
"dev:home": "dotenv -e env/.env.home npm run start:electron",
"dev:home:video": "dotenv -e env/.env.home dotenv -e env/.env.video npm run start:electron",
Expand All @@ -96,7 +94,7 @@
"format": "prettier --write .",
"lint": "eslint .",
"prepare": "husky",
"postinstall": "node version.js"
"postinstall": "node version.mjs"
},
"browserslist": {
"production": [
Expand Down
7 changes: 7 additions & 0 deletions src/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export default function App() {
"Task Settings": SETTINGS,
});

console.log(
"ENVIRONMENT",
import.meta.env.PACKAGE_NAME,
import.meta.env.PACKAGE_VERSION,
import.meta.env
);
RobertGemmaJr marked this conversation as resolved.
Show resolved Hide resolved

// If on desktop
if (config.USE_ELECTRON) {
// TODO @brown-ccv #443 : Pass NODE_ENV here as well
Expand Down
4 changes: 3 additions & 1 deletion src/App/components/JsPsychExperiment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export default function JsPsychExperiment({
tempJsPsych.data.addProperties({
app_name: import.meta.env.PACKAGE_NAME,
app_version: import.meta.env.PACKAGE_VERSION,
app_commit: await window.electronAPI.getCommit(),
// TODO: This does NOT work when using Firebase as electronAPI isn't set up
// TODO: Can I just get the file from here anyways?
// app_commit: await window.electronAPI.getCommit(),
study_id: studyID,
participant_id: participantID,
start_date: startDate,
Expand Down
3 changes: 2 additions & 1 deletion src/Electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import _ from "lodash";
import { getPort, sendToPort } from "./lib/serialport";

/* global MAIN_WINDOW_VITE_DEV_SERVER_URL MAIN_WINDOW_VITE_NAME */
// TODO: If we can get eslint to play nice we can remove this

// TODO @RobertGemmaJr: Do more testing with the environment variables - are home/clinic being built correctly?
// TODO @brown-ccv #192: Handle data writing to desktop in a utility process
Expand Down Expand Up @@ -296,7 +297,7 @@ function createWindow() {
// Create the browser window
const mainWindow = new BrowserWindow({
icon: "./favicon.ico",
webPreferences: { preload: path.join(__dirname, "preload.cjs") },
webPreferences: { preload: path.join(__dirname, "preload.js") },
width: 1500,
height: 900,
// TODO @brown-ccv: Settings for preventing the menu bar from ever showing up
Expand Down
File renamed without changes.
80 changes: 0 additions & 80 deletions vite.base.config.js

This file was deleted.

82 changes: 82 additions & 0 deletions vite.base.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { builtinModules } from "node:module";
import { defineConfig } from "vite";
import pkg from "./package.json";

/** Base vite config shared between electron-forge and the browser */
export default defineConfig({
base: "./",
define: {
// TODO: Rename as __APP_NAME__ and __APP_VERSION__
"import.meta.env.PACKAGE_NAME": JSON.stringify(process.env.npm_package_name),
"import.meta.env.PACKAGE_VERSION": JSON.stringify(process.env.npm_package_version),
},
server: { port: 3000 },
resolve: { preserveSymlinks: true },
clearScreen: false,
});

/** The complete list of dependencies used in the project */
export const external = [
// Builtin dependencies
...["electron", ...builtinModules.map((m) => [m, `node:${m}`]).flat()],
// External dependencies
...Object.keys(pkg.dependencies || {}),
];

/**
* Helper function for building the Vite config shared across the electron-forge processes
* @type {(env: import('vite').ConfigEnv<'build'>) => import('vite').UserConfig}
*/
export const getBuildConfig = ({ root, mode, command }) => {
return {
root,
mode,
build: {
// 🚧 Prevent multiple builds from interfering with each other.
emptyOutDir: false,
outDir: ".vite/build",
watch: command === "serve" ? {} : null,
minify: command === "build",
},
clearScreen: false,
};
};

/**
* Helper function for generating the keys used to build the "define" configuration
* @type {(names: string[]) => { [name: string]: VitePluginRuntimeKeys } }
*/
export const getDefineKeys = (names) => {
/** @type {{ [name: string]: VitePluginRuntimeKeys }} */
const define = {};
return names.reduce((acc, name) => {
const NAME = name.toUpperCase();
/** @type {VitePluginRuntimeKeys} */
const keys = {
VITE_DEV_SERVER_URL: `${NAME}_VITE_DEV_SERVER_URL`,
VITE_NAME: `${NAME}_VITE_NAME`,
};
return { ...acc, [name]: keys };
}, define);
};

/**
* Plugin that enables hot reloading and hot restarting in the electron-forge processes
* @type {(command: 'reload' | 'restart') => import('vite').Plugin}
*/
export const pluginHotRestart = (command) => {
return {
name: "@electron-forge/plugin-vite:hot-restart",
closeBundle() {
if (command === "reload") {
for (const server of Object.values(process.viteDevServers)) {
// Preload scripts hot reload.
server.ws.send({ type: "full-reload" });
}
} else {
// Main process hot restart: https://github.com/electron/forge/blob/v7.2.0/packages/api/core/src/api/start.ts#L216-L223
process.stdin.emit("data", "rs");
}
},
};
};
7 changes: 7 additions & 0 deletions vite.browser.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import react from "@vitejs/plugin-react";
import { defineConfig, mergeConfig } from "vite";

import baseConfig from "./vite.base.config.mjs";

/** Vite configuration used when running in the browser */
export default defineConfig(mergeConfig(baseConfig, defineConfig({ plugins: [react()] })));
51 changes: 23 additions & 28 deletions vite.main.config.js → vite.main.config.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
import { defineConfig, mergeConfig } from "vite";
import { external, getBuildConfig, getDefineKeys, pluginHotRestart } from "./vite.base.config.mjs";

import { getBuildConfig, getDefineKeys, external, pluginHotRestart } from "./vite.base.config.js";

/** @type {(env: import('vite').ConfigEnv<'build'>) => Record<string, any>} */
const getBuildDefine = (env) => {
const { command, forgeConfig } = env;

const names = forgeConfig.renderer.filter(({ name }) => name != null).map(({ name }) => name);
const defineKeys = getDefineKeys(names);
const define = Object.entries(defineKeys).reduce((acc, [name, keys]) => {
const { VITE_DEV_SERVER_URL, VITE_NAME } = keys;
const def = {
[VITE_DEV_SERVER_URL]:
command === "serve" ? JSON.stringify(process.env[VITE_DEV_SERVER_URL]) : undefined,
[VITE_NAME]: JSON.stringify(name),
};
return { ...acc, ...def };
}, {});

return define;
};

export default defineConfig((env) => {
return mergeConfig(getBuildConfig(env), {
/** Vite configuration for the main process */
export default defineConfig((env) =>
mergeConfig(getBuildConfig(env), {
build: {
lib: {
// Pulls the entries from forge.config.js
entry: env.forgeConfigSelf.entry,
// The files are built in CJS format, update the extensions
fileName: () => "[name].cjs",
fileName: () => "[name].js",
formats: ["cjs"],
},
rollupOptions: { external },
Expand All @@ -39,5 +18,21 @@ export default defineConfig((env) => {
// Load the Node.js entry.
mainFields: ["module", "jsnext:main", "jsnext"],
},
});
});
})
);

/** @type {(env: import('vite').ConfigEnv<'build'>) => Record<string, any>} */
export const getBuildDefine = ({ command, forgeConfig }) => {
const names = forgeConfig.renderer.filter(({ name }) => name != null).map(({ name }) => name);
const defineKeys = getDefineKeys(names);

return Object.entries(defineKeys).reduce((acc, [name, keys]) => {
const { VITE_DEV_SERVER_URL, VITE_NAME } = keys;
const def = {
[VITE_DEV_SERVER_URL]:
command === "serve" ? JSON.stringify(process.env[VITE_DEV_SERVER_URL]) : undefined,
[VITE_NAME]: JSON.stringify(name),
};
return { ...acc, ...def };
}, {});
};
Loading
Loading