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

Allow dynamic specification of "main" field in package.json #270

Closed
4 tasks done
undergroundwires opened this issue Sep 3, 2023 · 4 comments
Closed
4 tasks done
Labels
enhancement New feature or request

Comments

@undergroundwires
Copy link

undergroundwires commented Sep 3, 2023

Clear and concise description of the problem

As a developer using electron-vite, I would like the ability to dynamically set the "main" field in package.json or have electron-vite handle this automatically. This will make the build process more flexible and centralized.

Take electron-builder for instance, which allows us to dynamically set the "main" field:

// electron-builder.cjs
module.exports {
  ...
  extraMetadata: {
    main: join(dynamicLogicToGetDistPath(), 'main/index.cjs')

This functionality allows for centralized management of distribution directories, which is crucial for complex projects. In contrast, package.json forces us to hardcode this information, which limits flexibility.

Suggested solution

Alternative 1:

electron-vite already knows the main entry file's location and where to copy it. Can it patch package.json or implement a similar mechanism to point to the main file from the distribution directory? This would enable a zero-config approach.

Alternative 2:

Allow developers to specify the main file path in one of the configuration files. This could even be an additional feature on top of Alternative 1.

Alternative

No response

Additional context

First of all, thank you for this excellent plugin. The TypeScript configuration files are a great help, and the support for ESM modules is impressive. The documentation is clear and comprehensive; I never had to look at the code. This project now empowers desktop builds of privacy.sexy.

What I'm trying to solve:

This need arose when I attempted to centralize all distribution directory configurations into a single dist-dirs.json file. The goal is to eliminate hardcoded paths that lead to unstable builds. While I can easily manage this in electron.vite.config.ts, electron-builder.cjs, vite.config.ts, or in CI/CD pipelines/other commands through a utility I build (scripts/print-dist-dir.js), I cannot do so in package.json. This issue aims to bridge that gap.

Validations

@undergroundwires undergroundwires added the enhancement New feature or request label Sep 3, 2023
@undergroundwires undergroundwires changed the title Allow dynamic specification of "main" Field in package.json Allow dynamic specification of "main" field in package.json Sep 3, 2023
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Sep 3, 2023
This commit makes the build process more robust, simplifies
configurations and reduce the risk of incomplete or erroneous
deployments.

- Centralize output directory definitions by introducing
  `dist-dirs.json`.
- Add `verify-build-artifacts` utility to ensure correct build outputs
  and `print-dist-dir` to determine distribution directory.
- Add steps in CI/CD pipeline to verify build artifacts.
- Migrate Electron Builder config from YAML to CJS for capability to
  read JSON.
- Fix `release-site.yaml` failing due to pointing to wrong distribution
  directory, change it to use `print-dist-dir`.
- Improve `check-desktop-runtime-errors` to verify build artifacts for
  more reliable builds. Ensure tests fail and succeed reliably.
- Update `.gitignore` and configure ESLint to use it to define and
  ignore build artifact directories from one place, remove
  `.eslintignore` that does not add anything after this change.
- Keep `"main"` field in `package.json` as `electron-vite` depends on it
  (alex8088/electron-vite#270).
- Improve documentation
@alex8088
Copy link
Owner

alex8088 commented Sep 3, 2023

@undergroundwires

Maybe adding a cli option to dynamically specify the electron entry file would be a good way, e.g.

electron-vite dev --entry out/main/xxx.js

@undergroundwires
Copy link
Author

Thanks for the quick response @alex8088 .

Config file option would simplify my setup with power of TypeScript but this can be only specific to me. CLI option would still solve it for me but I will need to build some cross-platform command wrappers so I can read distribution directory from JSON file. Even though I prefer config file option if I had the choice, CLI option would be much appreciated (it should work with electron-vite build too).

Out-of-topic appreciation/feedback

I really like how clean your code is here.

I also see perfect balance of the project where it just does enough "magic" that eliminates common problems without abstracting too much.

I decided on this when I was doing deep dive through lack of ESM support in electron and got it working with completely different and much simpler config than provided there. I recommend the plugin to all vue-cli-plugin-electron-builder refugees, nklayman/vue-cli-plugin-electron-builder#1982.

I know how much effort it takes to maintain open-source, often with little to no gains. Your efforts are appreciated 👍️.

@alex8088
Copy link
Owner

alex8088 commented Sep 4, 2023

@undergroundwires

This feature should be added in the next version, and of course it will support all CLI commands.

Thank you for your appreciation and recommendation for this project. Providing faster and cleaner DX for Electron was the motivation behind this project. It's great to help Electron developers solve some difficult problems.

undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Oct 22, 2023
This commit refactors configuration to use centrally defined Electron
entry file path to improve maintainability and reduce duplication.

- Replace the hardcoded file path in the `main` field of `package.json`
  with a reference to the `ELECTRON_ENTRY` environment variable, managed
  by `electron-vite`.
- Update `electron-vite` to version 1.0.28, enabling the use of
  `ELECTRON_ENTRY` environment variable feature (details in
  alex8088/electron-vite#270).
@undergroundwires
Copy link
Author

Works great, environment variable was a life saver, thank you for listening, your efforts and quick action @alex8088 👍️

undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue Oct 22, 2023
This commit refactors configuration to use centrally defined Electron
entry file path to improve maintainability and reduce duplication.

- Replace the hardcoded file path in the `main` field of `package.json`
  with a reference to the `ELECTRON_ENTRY` environment variable, managed
  by `electron-vite`.
- Update `electron-vite` to version 1.0.28, enabling the use of
  `ELECTRON_ENTRY` environment variable feature (details in
  alex8088/electron-vite#270).
LarrMarburger added a commit to LarrMarburger/privacy.sexy that referenced this issue Nov 16, 2023
This commit makes the build process more robust, simplifies
configurations and reduce the risk of incomplete or erroneous
deployments.

- Centralize output directory definitions by introducing
  `dist-dirs.json`.
- Add `verify-build-artifacts` utility to ensure correct build outputs
  and `print-dist-dir` to determine distribution directory.
- Add steps in CI/CD pipeline to verify build artifacts.
- Migrate Electron Builder config from YAML to CJS for capability to
  read JSON.
- Fix `release-site.yaml` failing due to pointing to wrong distribution
  directory, change it to use `print-dist-dir`.
- Improve `check-desktop-runtime-errors` to verify build artifacts for
  more reliable builds. Ensure tests fail and succeed reliably.
- Update `.gitignore` and configure ESLint to use it to define and
  ignore build artifact directories from one place, remove
  `.eslintignore` that does not add anything after this change.
- Keep `"main"` field in `package.json` as `electron-vite` depends on it
  (alex8088/electron-vite#270).
- Improve documentation
LarrMarburger added a commit to LarrMarburger/privacy.sexy that referenced this issue Nov 16, 2023
This commit refactors configuration to use centrally defined Electron
entry file path to improve maintainability and reduce duplication.

- Replace the hardcoded file path in the `main` field of `package.json`
  with a reference to the `ELECTRON_ENTRY` environment variable, managed
  by `electron-vite`.
- Update `electron-vite` to version 1.0.28, enabling the use of
  `ELECTRON_ENTRY` environment variable feature (details in
  alex8088/electron-vite#270).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants