Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/saltyshiomix/nextron into f…
Browse files Browse the repository at this point in the history
…eat/wait-for-renderer
  • Loading branch information
saltyshiomix committed Nov 9, 2024
2 parents 26540d3 + 8afa351 commit a203319
Show file tree
Hide file tree
Showing 11 changed files with 1,038 additions and 344 deletions.
6 changes: 0 additions & 6 deletions .eslintignore

This file was deleted.

15 changes: 0 additions & 15 deletions .eslintrc.json

This file was deleted.

2 changes: 0 additions & 2 deletions dev.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-var-requires */

const fs = require('fs-extra')
const path = require('path')
const execa = require('execa')
Expand Down
85 changes: 85 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import typescript from '@typescript-eslint/eslint-plugin'
import js from '@eslint/js'
import nextPlugin from '@next/eslint-plugin-next'
import reactPlugin from 'eslint-plugin-react'
import reactHooksPlugin from 'eslint-plugin-react-hooks'
import parserTypescript from '@typescript-eslint/parser'
import globals from 'globals'
import configPrettier from 'eslint-config-prettier'

/**
* @type {import('eslint').Linter.Config[]}
*/
const config = [
{
ignores: ['**/node_modules', 'pnpm-lock.yaml', '**/bin', '**/workspace'],
},
{
languageOptions: {
ecmaVersion: 2022,
globals: {
...globals.browser,
...globals.es2021,
...globals.node,
document: 'readonly',
navigator: 'readonly',
window: 'readonly',
},
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2022,
sourceType: 'module',
},
sourceType: 'module',
},
rules: {
...js.configs.recommended.rules,
'no-unused-vars': [
'error',
{
args: 'none',
caughtErrors: 'none',
ignoreRestSiblings: true,
vars: 'all',
},
],
},
},
{
files: ['**/*.ts', '**/*.tsx'],
plugins: {
react: reactPlugin,
'react-hooks': reactHooksPlugin,
'@next/next': nextPlugin,
},
rules: {
...reactPlugin.configs['jsx-runtime'].rules,
...reactHooksPlugin.configs.recommended.rules,
...nextPlugin.configs.recommended.rules,
...nextPlugin.configs['core-web-vitals'].rules,
'@next/next/no-img-element': 'error',
},
},
{
files: ['**/*.ts', '**/*.tsx'],
plugins: {
'@typescript-eslint': typescript,
},
languageOptions: {
parser: parserTypescript,
},
rules: {
...typescript.configs.recommended.rules,
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-explicit-any': 'error',
'@next/next/no-html-link-for-pages': 'off',
},
},
{
...configPrettier,
},
]

export default config
13 changes: 2 additions & 11 deletions examples/with-ant-design/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
Expand All @@ -19,10 +15,5 @@
"isolatedModules": true,
"jsx": "preserve"
},
"exclude": [
"node_modules",
"renderer/next.config.js",
"app",
"dist"
]
"exclude": ["node_modules", "renderer/next.config.js", "app", "dist"]
}
3 changes: 1 addition & 2 deletions examples/with-next-i18next/next-i18next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
reloadOnPrerender: process.env.NODE_ENV === 'development',
localePath:
typeof window === 'undefined'
? // eslint-disable-next-line @typescript-eslint/no-var-requires
require('path').resolve('./renderer/public/locales')
? require('path').resolve('./renderer/public/locales')
: '/locales',
}
168 changes: 87 additions & 81 deletions lib/nextron-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ if (args['--inspect']) {
const nextronConfig = getNextronConfig()

const rendererPort = args['--renderer-port'] || 8888
const startupDelay = nextronConfig.startupDelay || args['--startup-delay'] || 10_000
const startupDelay =
nextronConfig.startupDelay || args['--startup-delay'] || 10_000

let electronOptions = args['--electron-options'] || ''
if (!electronOptions.includes('--remote-debugging-port')) {
Expand All @@ -59,93 +60,98 @@ const execaOptions: execa.Options = {
stdio: 'inherit',
}

; (async () => {
let firstCompile = true
let watching: webpack.Watching
let mainProcess: ChildProcess
let rendererProcess: ChildProcess // eslint-disable-line prefer-const

const startMainProcess = () => {
logger.info(
`Run main process: electron . ${rendererPort} ${electronOptions}`
)
mainProcess = execa(
'electron',
['.', `${rendererPort}`, `${electronOptions}`],
{
detached: true,
...execaOptions,
}
)
mainProcess.unref()
}
;(async () => {
let firstCompile = true
let watching: webpack.Watching
let mainProcess: ChildProcess
let rendererProcess: ChildProcess

const startMainProcess = () => {
logger.info(
`Run main process: electron . ${rendererPort} ${electronOptions}`
)
mainProcess = execa(
'electron',
['.', `${rendererPort}`, `${electronOptions}`],
{
detached: true,
...execaOptions,
}
)
mainProcess.unref()
}

const startRendererProcess = () => {
logger.info(
`Run renderer process: next -p ${rendererPort} ${
nextronConfig.rendererSrcDir || 'renderer'
}`
)
const child = execa(
'next',
['-p', rendererPort, nextronConfig.rendererSrcDir || 'renderer'],
execaOptions
)
child.on('close', () => {
process.exit(0)
})
return child
}

const startRendererProcess = () => {
logger.info(
`Run renderer process: next -p ${rendererPort} ${nextronConfig.rendererSrcDir || 'renderer'
}`
)
const child = execa(
'next',
['-p', rendererPort, nextronConfig.rendererSrcDir || 'renderer'],
execaOptions
)
child.on('close', () => {
process.exit(0)
})
return child
const killWholeProcess = () => {
if (watching) {
watching.close(() => {})
}

const killWholeProcess = () => {
if (watching) {
watching.close(() => { })
}
if (mainProcess) {
mainProcess.kill()
}
if (rendererProcess) {
rendererProcess.kill()
}
if (mainProcess) {
mainProcess.kill()
}
if (rendererProcess) {
rendererProcess.kill()
}
}

process.on('SIGINT', killWholeProcess)
process.on('SIGTERM', killWholeProcess)
process.on('exit', killWholeProcess)

rendererProcess = startRendererProcess()

// wait until renderer process is ready
await waitForPort(rendererPort, {
delay: 500,
retries: startupDelay / 500,
}).catch(() => {
logger.error(
`Failed to start renderer process with port ${rendererPort} in ${startupDelay}ms`
)
killWholeProcess()
process.exit(1)
})

// wait until main process is ready
await new Promise<void>((resolve) => {
const compiler = webpack(config)
watching = compiler.watch({}, (error) => {
if (error) {
console.error(error.stack || error)
}

process.on('SIGINT', killWholeProcess)
process.on('SIGTERM', killWholeProcess)
process.on('exit', killWholeProcess)

rendererProcess = startRendererProcess()

// wait until renderer process is ready
await waitForPort(rendererPort, { delay: 500, retries: startupDelay / 500 })
.catch(() => {
logger.error(`Failed to start renderer process with port ${rendererPort} in ${startupDelay}ms`)
killWholeProcess()
process.exit(1)
})

// wait until main process is ready
await new Promise<void>((resolve) => {
const compiler = webpack(config)
watching = compiler.watch({}, (error) => {
if (error) {
console.error(error.stack || error)
if (!args['--run-only']) {
if (!firstCompile && mainProcess) {
mainProcess.kill()
}
startMainProcess()

if (!args['--run-only']) {
if (!firstCompile && mainProcess) {
mainProcess.kill()
}
startMainProcess()

if (firstCompile) {
firstCompile = false
}
if (firstCompile) {
firstCompile = false
}
}

resolve()
})
resolve()
})
})

if (args['--run-only']) {
startMainProcess()
}
})()
if (args['--run-only']) {
startMainProcess()
}
})()
2 changes: 0 additions & 2 deletions lint-staged.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable import/no-anonymous-default-export */

export default {
'**/*.(ts|tsx|js)': (filenames) => [
`npx eslint --fix ${filenames.join(' ')}`,
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,23 @@
"webpack-merge": "5.10.0"
},
"devDependencies": {
"@next/eslint-plugin-next": "^15.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"@types/eslint": "^9.6.1",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.14.2",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"concurrently": "^8.2.2",
"eslint": "^9.5.0",
"eslint": "^9.14.0",
"eslint-config-next": "^14.2.4",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-hooks": "^5.0.0",
"globals": "^15.12.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.7",
"prettier": "^3.3.2",
Expand Down
Loading

0 comments on commit a203319

Please sign in to comment.