diff --git a/lib/configs/useExportCommand.ts b/lib/configs/useExportCommand.ts new file mode 100644 index 00000000..218b3740 --- /dev/null +++ b/lib/configs/useExportCommand.ts @@ -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 => { + 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) +} diff --git a/lib/nextron-build.ts b/lib/nextron-build.ts index 6a7383d8..6a8373b7 100755 --- a/lib/nextron-build.ts +++ b/lib/nextron-build.ts @@ -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, @@ -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(