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

support preload.ts #3155

Merged
merged 1 commit into from Mar 22, 2022
Merged

support preload.ts #3155

merged 1 commit into from Mar 22, 2022

Conversation

sminf
Copy link
Contributor

@sminf sminf commented Mar 5, 2022

closes #3058

@UserJHansen
Copy link
Contributor

lgtm, looks like it might be able to be done better though, using more out files in the original build

@sminf
Copy link
Contributor Author

sminf commented Mar 6, 2022

lgtm, looks like it might be able to be done better though, using more out files in the original build

Where is the original build located? Is release/app/dist/? If so, I use .erb/dll directory intentionally to store development preload.js without affecting release build.

Copy link
Member

@amilajack amilajack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! This has been a long awaited feature of the boilerplate. Here's a few minor comments.

.erb/configs/webpack.config.renderer.dev.ts Outdated Show resolved Hide resolved
src/main/preload.ts Show resolved Hide resolved
@sminf sminf requested a review from amilajack March 18, 2022 16:02
@anasrar
Copy link

anasrar commented Mar 19, 2022

issue that I found is webpack keep watch preload.ts after electron close in npm start and also nice to have preload.d.ts for better type checking in react side.

src/preload.d.ts

declare global {
  interface Window {
    electron: {
      ipcRenderer: {
        myPing(): void;
        on(channel: string, func: (...args: any[]): void;
        once(channel: string, func: (...args: any[]): void;
      };
    };
  };
};

export {};

@amilajack
Copy link
Member

@anasrar great feedback and good catch. @anniekatlin can you also incorporate @anasrar's feedback

@anasrar
Copy link

anasrar commented Mar 20, 2022

my current solution is polling check the parent PID using webpack hook

.erb/configs/webpack.config.preload.dev.ts

class CheckParentPIDProcess {
  interval: number;

  constructor(interval: number) {
    this.interval = interval;
  }

  check() {
    try {
      process.kill(parseInt(process.env.PARENT_PID), 0);
    } catch (e) {
      process.exit(0);
    }
    setTimeout(() => {
      this.check();
    }, this.interval);
  }

  apply(compiler: Compiler) {
    if (!process.env.hasOwnProperty('PARENT_PID')) {
      console.log('PARENT_PID not found on environment');
      return;
    }
    compiler.hooks.entryOption.tap('CheckPIDProcess', (context, entry) => {
      console.log(`Start check parent PID process: ${process.env.PARENT_PID}`);
      // recommend using promise instead of setTimeout
      setTimeout(() => {
        this.check();
      }, this.interval);
    });
  }
}

const configuration: webpack.Configuration = {
  // ...
  plugins: [
    // ...
    new CheckParentPIDProcess(3000),
  ]
}

and passing PARENT_PID to environment in .erb/configs/webpack.config.renderer.dev.ts before spawn npm run start:preload

process.env.PARENT_PID = process.pid.toString();

@sminf sminf closed this Mar 20, 2022
@sminf sminf reopened this Mar 20, 2022
@sminf
Copy link
Contributor Author

sminf commented Mar 20, 2022

I rebased my fork causing this PR being closed, and then reopen this PR after pushing latest commit, now it still request changing now. Please review this, thanks. @amilajack

@amilajack amilajack merged commit 5b4fa97 into electron-react-boilerplate:main Mar 22, 2022
@amilajack
Copy link
Member

@anniekatlin thanks for the contribution 🥳 this is been a long awaited feature!

@shyamkumaryadav
Copy link
Contributor

Thanks @anniekatlin

@dddreee
Copy link

dddreee commented Mar 28, 2022

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support preload.ts
6 participants