Skip to content

Commit

Permalink
feat: Support Next.js v13 - v14 :)
Browse files Browse the repository at this point in the history
  • Loading branch information
saltyshiomix committed Jun 16, 2024
1 parent 6f85605 commit 0e47ca5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
56 changes: 56 additions & 0 deletions lib/configs/useExportCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fs from 'fs-extra'
import path from 'path'
import { getNextronConfig } from './getNextronConfig'
import * as logger from '../logger'

const cwd = process.cwd()
const pkgPath = path.join(cwd, 'package.json')
const nextConfigPath = path.join(
getNextronConfig().rendererSrcDir || 'renderer',
'next.config.js'
)

export const useExportCommand = async (): Promise<boolean> => {
const { dependencies, devDependencies } = await fs.readJSON(pkgPath)

let nextronVersion: string
nextronVersion = dependencies.nextron
if (nextronVersion) {
logger.info(
'To reduce the bundle size of the electron app, we recommend placing nextron in devDependencies instead of dependencies.'
)
}
if (!nextronVersion) {
nextronVersion = devDependencies.nextron
if (!nextronVersion) {
logger.error(
'Nextron not found in both dependencies and devDependencies.'
)
process.exit(1)
}
}

const majorVersion = ~~nextronVersion
.split('.')
.filter((v) => v.trim() !== '')[0]
if (majorVersion < 13) {
return true
}
if (majorVersion === 13) {
const { output } = await fs.readJSON(nextConfigPath)
return output !== 'export'
}
if (majorVersion > 13) {
const { output } = await fs.readJSON(nextConfigPath)
if (output !== 'export') {
logger.error(
'We must export static files so as Electron can handle them. Please set next.config.js#output to "export".'
)
process.exit(1)
}
return false
}

logger.error('Unexpected error occerred')
process.exit(1)
}
13 changes: 8 additions & 5 deletions lib/nextron-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chalk from 'chalk'
import execa from 'execa'
import * as logger from './logger'
import { getNextronConfig } from './configs/getNextronConfig'
import { useExportCommand } from './configs/useExportCommand'

const args = arg({
'--mac': Boolean,
Expand Down Expand Up @@ -39,11 +40,13 @@ const execaOptions: execa.Options = {

logger.info('Building renderer process')
await execa('next', ['build', path.join(cwd, rendererSrcDir)], execaOptions)
await execa(
'next',
['export', '-o', appDir, path.join(cwd, rendererSrcDir)],
execaOptions
)
if (await useExportCommand()) {
await execa(
'next',
['export', '-o', appDir, path.join(cwd, rendererSrcDir)],
execaOptions
)
}

logger.info('Building main process')
await execa(
Expand Down

0 comments on commit 0e47ca5

Please sign in to comment.