Skip to content

Commit

Permalink
Merge pull request #22 from caoxiemeihao/v0.4.3
Browse files Browse the repository at this point in the history
V0.4.3
  • Loading branch information
caoxiemeihao authored Mar 29, 2023
2 parents b7e047c + 7c2d18a commit 99e3a8d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.3 (2023-03-29)

- 12d8c58 refactor: cleanup

## 0.4.2 (2023-03-12)

#### Main Changed
Expand Down
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuxt-electron",
"version": "0.4.2",
"version": "0.4.3",
"description": "Nuxt Integration with Electron",
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
Expand All @@ -10,11 +10,7 @@
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./*": {
"types": "./dist/*.d.ts",
"import": "./dist/*.mjs",
"require": "./dist/*.cjs"
}
"./*": "./*"
},
"repository": {
"type": "git",
Expand Down
81 changes: 49 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from 'path'
import { fileURLToPath } from 'url'
import { type AddressInfo } from 'net'
import { defineNuxtModule } from '@nuxt/kit'
import {
Expand All @@ -9,12 +7,10 @@ import {
startup,
} from 'vite-electron-plugin'

const cjs__dirname = typeof __dirname === 'undefined'
? path.dirname(fileURLToPath(import.meta.url))
: __dirname
const isProduction = process.env.NODE_ENV === 'production'

// Fix tsc build error
import { NuxtModule } from '@nuxt/schema'
import { NuxtModule, Nuxt } from '@nuxt/schema'

export interface ElectronOptions extends Partial<Configuration> {
/**
Expand All @@ -36,36 +32,19 @@ export default defineNuxtModule<ElectronOptions>({
outDir: 'dist-electron',
},
async setup(options, nuxt) {
const isProduction = process.env.NODE_ENV === 'production'
adaptElectronConfig(options, nuxt)

// Force to SPA mode always since we don't need SSR for a desktop app.
nuxt.options.ssr = false

if (isProduction) {
// Fix path to make it works with Electron protocol `file://`
nuxt.options.app.baseURL ??= './'
if (nuxt.options.app.baseURL.startsWith('/')) {
nuxt.options.app.baseURL = '.' + nuxt.options.app.baseURL
}
nuxt.options.runtimeConfig.app.baseURL ??= './'
if (nuxt.options.runtimeConfig.app.baseURL.startsWith('/')) {
nuxt.options.runtimeConfig.app.baseURL = '.' + nuxt.options.runtimeConfig.app.baseURL
}
nuxt.options.router.options.hashMode ??= true // Avoid 404 errors
}

// Use Node.js in Renderer process
if (options.renderer) {
// For Vite
nuxt.options.vite.plugins ??= []
nuxt.options.vite.plugins.push((await import('vite-plugin-electron-renderer')).default(options.renderer))

// TODO: For Webpack

nuxt.options.nitro.plugins ??= []
nuxt.options.nitro.plugins.push(path.join(cjs__dirname, 'cjs-shim')) // #14
nodeIntegration(options, nuxt)
}

nuxt.hook('build:manifest', (manifest) => {
for (const key in manifest) {
// or other logic
manifest[key].dynamicImports = []
}
})

nuxt.hooks.addHooks({
// For development
listen(server, listener) {
Expand Down Expand Up @@ -94,3 +73,41 @@ export default defineNuxtModule<ElectronOptions>({
})
}
})

/** Opinionated config for Electrono */
function adaptElectronConfig(options: ElectronOptions, nuxt: Nuxt) {
// Force to SPA mode always since we don't need SSR for a desktop app.
nuxt.options.ssr = false
nuxt.options.app.buildAssetsDir = '/' // #16
nuxt.options.app.baseURL = './'
nuxt.options.runtimeConfig.app.baseURL = './'

if (isProduction) {
// TODO: calculate correct `require(id)`

// Fix path to make it works with Electron protocol `file://`
/*
nuxt.options.app.baseURL ??= './'
if (nuxt.options.app.baseURL.startsWith('/')) {
nuxt.options.app.baseURL = '.' + nuxt.options.app.baseURL
}
nuxt.options.runtimeConfig.app.baseURL ??= './'
if (nuxt.options.runtimeConfig.app.baseURL.startsWith('/')) {
nuxt.options.runtimeConfig.app.baseURL = '.' + nuxt.options.runtimeConfig.app.baseURL
}
*/

nuxt.options.router.options.hashMode ??= true // Avoid 404 errors
}
}

/** Use Node.js in Renderer process */
async function nodeIntegration(options: ElectronOptions, nuxt: Nuxt) {
// For Vite
nuxt.options.vite.plugins ??= []
nuxt.options.vite.plugins.push((await import('vite-plugin-electron-renderer')).default(options.renderer))

// TODO: For Webpack

nuxt.options.nitro.plugins ??= []
}

0 comments on commit 99e3a8d

Please sign in to comment.